error: x_wmi: Unexpected COM Error (-2147217406, 'OLE error 0x80041002', None, None)
I am trying to get the windows PC the detect the COM port of an Ardunio. The code sometimes works and sometimes doesn't, when it doesn't i get the error listed above.
testing the code by plugging in and removing the Ardunio (giving ample time for windows to recognise it being removed). I tried searching for the error but nothing came up.
EDIT: with some more searching i found its something to do with an Object Not Found Error. This happens when any of to COM ports change (when i plug in any USB device). The code can handle the list changing mid way though and so throws up this error. Im not sure how to handle the error in such a way hat it allows USB devices to change.
langpy:
import os
import time
import ctypes
import wmi
import thread
import pythoncom
import locale
def lang():
localelang = locale.getdefaultlocale()
language = str(localelang)[2:7]
#print (language)
return language
def port():
while True:
pythoncom.CoInitialize()
Ardport = ""
c = wmi.WMI()
wql = "Select * From Win32_USBControllerDevice"
for item in c.query(wql):
s = str(item.Dependent.Caption)
#print (item.Dependent.Caption)
strfind = "Arduino"
strignore = "bootloader"
if (s.find(strfind) >= 0 and s.find(strignore) == -1):
Ardport = item.Dependent.Caption
time.sleep(1)
return Ardport
main code:
# -*- coding: utf-8 -*-
import time
import thread
import os
import serial
import sys
import ctypes
import wmi
from langpy import lang
from langpy import port
import pythoncom
class service_test:
def __init__(self):
thread.start_new(self.check_for_Ardunio, tuple())
def check_for_Ardunio(self):
comp = ""
reply = ""
j1 = -1
while True:
plang = (str(lang()))
pport = (str(port()))
if (pport != ""):
j1 = pport[-2:-1]
comp = ('COM' + j1)
if (reply == "reply" and j1 < 0):
ser = serial.Serial(comp, 9600, timeout=1)
if(ser.isOpen()):
ser.write(lang)
print ("sent message")
else:
time.sleep(1)
reply = ""
j1 = -1
comp = 'COM'
#DEBUGGING
print(comp)
print(plang)
print(pport)
print (j1)
print (str(reply))
print ("")
if __name__ == "__main__":
tst = service_test()
full error report:
Unhandled exception in thread started by <bound method service_test.check_for_Ardunio of <__main__.service_test instance at 0x01FEE6C0>>
Traceback (most recent call last):
File "C:\Users\LeakDetectSys\Desktop\Keyboard\testing\application\test1wc.py", line 27, in check_for_Ardunio
pport = (str(port()))
File "C:\Users\LeakDetectSys\Desktop\Keyboard\testing\application\langpy.py", line 25, in port
s = str(item.Dependent.Caption)
File "C:\Python27\lib\site-packages\wmi.py", line 555, in __getattr__
return WMI (moniker=value)
File "C:\Python27\lib\site-packages\wmi.py", line 1290, in connect
handle_com_error ()
File "C:\Python27\lib\site-packages\wmi.py", line 241, in handle_com_error
raise klass (com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147217406, 'OLE error 0x80041002', None, None)>
User contributions licensed under CC BY-SA 3.0