Добрый день! Прошу помощи в решении проблемы. Написал небольшой код запуска программы при подключении устройства, и завершения программы при отключении этого устройства. В целом код отрабатывает поставленную задачу, но в какой-то момент происходит сбой и в командной строке выдается следующая ошибка:
Traceback (most recent call last):
File "C:\Ezcad2.14.10\autostartstop\Lib\site-packages\wmi.py", line 1249, in __call__
event = self.wmi_event.NextEvent(timeout_ms)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMObject <unknown>>", line 2, in NextEvent
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemEventSource', 'Call cancelled ', None, 0, -2147217358), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Ezcad2.14.10\autostartstop\Start_stop.py", line 29, in <module>
disconnected = disconnected_watcher()
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Ezcad2.14.10\autostartstop\Lib\site-packages\wmi.py", line 1259, in __call__
handle_com_error()
File "C:\Ezcad2.14.10\autostartstop\Lib\site-packages\wmi.py", line 258, in handle_com_error
raise klass(com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147352567, 'Exception occurred.', (0, 'SWbemEventSource', 'Call cancelled ', None, 0, -2147217358), None)>
Traceback (most recent call last):
File "C:\Ezcad2.14.10\autostartstop\Lib\site-packages\wmi.py", line 1249, in __call__
event = self.wmi_event.NextEvent(timeout_ms)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMObject <unknown>>", line 2, in NextEvent
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemEventSource', 'Call cancelled ', None, 0, -2147217358), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Ezcad2.14.10\autostartstop\Start_stop.py", line 29, in <module>
disconnected = disconnected_watcher()
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Ezcad2.14.10\autostartstop\Lib\site-packages\wmi.py", line 1259, in __call__
handle_com_error()
File "C:\Ezcad2.14.10\autostartstop\Lib\site-packages\wmi.py", line 258, in handle_com_error
raise klass(com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147352567, 'Exception occurred.', (0, 'SWbemEventSource', 'Call cancelled ', None, 0, -2147217358), None)>
Python:
import wmi
import usb.core
import time
import subprocess
import os
command="taskkill /f /IM Ezcad2.exe"
device_connected_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_PnPEntity\'"
device_disconnected_wql = "SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_PnPEntity\'"
c = wmi.WMI()
connected_watcher = c.watch_for(raw_wql=device_connected_wql)
disconnected_watcher = c.watch_for(raw_wql=device_disconnected_wql)
while True:
device = usb.core.find(idVendor=0x9588, idProduct=0x9899)
if device is None:
print("Waiting for connection...")
connected = connected_watcher()
if connected:
time.sleep(2)
device = usb.core.find(idVendor=0x9588, idProduct=0x9899)
if device is not None:
print("Printer connected")
subprocess.Popen(['C:\\Ezcad2.14.10\\Ezcad2.exe', 'C:\\Ezcad2.14.10\\No.ezd'])
print("Waiting to disconnect...")
else:
disconnected = disconnected_watcher()
if disconnected:
time.sleep(2)
device = usb.core.find(idVendor=0x9588, idProduct=0x9899)
if device is None:
print("Printer disconnected")
os.system(command)
os.system('cls')