-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectUsb.py
199 lines (157 loc) · 6.94 KB
/
detectUsb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from pyvisa import attributes, ResourceManager
import threading
from pysetupdi import setupdi
import serial.tools.list_ports as portsList
import time
import equipment
import re
class initDevice:
devices = {}
connectedDevices = []
resourceManager = ResourceManager()
@staticmethod
def updateDevicesDB(device,devName="",visaHandle="", present=False):
try:
if present:
if "COM" in initDevice.devices[device]['Model Name']:
comPort = initDevice.get_friendly_name(device)
time.sleep(0.2)
initDevice.devices[device]['Model Name'] = 'Device ' + '(' + comPort + ')'
initDevice.devices[device]['Resource'].portNumber = comPort
initDevice.devices[device]['Resource'].reconnect()
else:
initDevice.devices[device]['Resource'].open()
return
if "COM" in devName:
dev = equipment.EquipmentRS232(*['COM'+ str(visaHandle).rsplit("::")[0][4:],'9600',0.1])
initDevice.devices.update({device: {
'Model Name': 'Device' + ' (COM' + str(visaHandle).rsplit("::")[0][4:] + ')',
'Device Type': 'COM' + str(visaHandle).rsplit("::")[0][4:],
'Connection Type': 'Serial',
'Visa Handle': visaHandle,
'Script Status': 'Inactive',
'Resource': dev
}})
return
if "TCPIP" in device:
try:
dev = initDevice.resourceManager.open_resource(device)
initDevice.devices.update({devName[11:].strip(): {
'Model Name': devName,
'Device Type': "Ethernet",
'Connection Type': '',
'Visa Handle': device,
'Script Status': 'Inactive',
'Resource': dev
}})
return
except Exception as e:
initDevice.devices.update({devName[11:].strip(): {
'Model Name': devName,
'Device Type': "Ethernet",
'Connection Type': '',
'Visa Handle': device,
'Script Status': 'Inactive',
'Resource': "NOT FOUND"
}})
return
dev = initDevice.resourceManager.open_resource(device)
initDevice.devices.update({str(dev.resource_name)[8:12] + str(dev.resource_name)[16:20]: {
'Model Name': dev.get_visa_attribute(attributes.constants.VI_ATTR_MODEL_NAME),
'Device type': dev.get_visa_attribute(attributes.constants.VI_ATTR_RSRC_CLASS),
'Connection Type': str(dev.interface_type)[14:],
'Visa Handle': device,
'Script Status': 'Inactive',
'Resource': dev
}})
except Exception as e:
print(str(e) + ' {{initDevice, updateDeviceDB, line 56}}')
def detectDevices(self):
for device in self.resourceManager.list_resources():
try:
if "ASRL" in str(device):
devs = setupdi.devices(enumerator="USB")
x = [port[0] for port in list(portsList.comports())]
for COM in devs:
if 'COM'+ str(device).rsplit("::")[0][4:] in str(COM):
h = COM.hardware_id
h= h[1][8:12] + h[1][17:]
break
dev = equipment.EquipmentRS232(*['COM'+ str(device).rsplit("::")[0][4:],'9600',0.1])
initDevice.devices.update({h: {
'Model Name': 'Device' + ' (COM'+ str(device).rsplit("::")[0][4:] + ')',
'Device Type': 'COM'+ str(device).rsplit("::")[0][4:] if 'COM' + str(device).rsplit("::")[0][4:] in x else 'PORT_ERROR',
'Connection Type': 'Serial',
'Visa Handle': device,
'Script Status': 'Inactive',
'Resource': dev
}})
elif "USB" in str(device):
dev = self.resourceManager.open_resource(device)
initDevice.devices.update({str(device)[8:12] + str(device)[16:20]: {
'Model Name': dev.get_visa_attribute(attributes.constants.VI_ATTR_MODEL_NAME),
'Device type': dev.get_visa_attribute(attributes.constants.VI_ATTR_RSRC_CLASS),
'Connection Type': str(dev.interface_type)[14:],
'Visa Handle': device,
'Script Status': 'Inactive',
'Resource': dev
}})
except Exception as e:
print(str(e) + ' {initDevice, detectDevices, line 85}')
def startThread(self, function, arguments):
self.threadx = threading.Thread(target=function, args=arguments)
self.threadx.daemon = True
self.threadx.start()
def vidValidator(self):
devices = []
devices_VID_PID = []
devNames = []
devInfo = {}
devs = setupdi.devices(enumerator="USB")
for dev in devs:
try:
if "USB Test and Measurement" in str(dev):
devices.append(dev)
elif "Ports" in dev.device_class:
devices.append(dev)
except (KeyError, AttributeError,):
continue
for x in devices:
try:
h = x.hardware_id
devices_VID_PID.append( h[1][8:12] + h[1][17:])
devNames.append(x.friendly_name)
devInfo.update({h[1][8:12] + h[1][17:]: x.friendly_name})
except AttributeError:
devInfo.update({h[1][8:12] + h[1][17:]: str(x)})
pass
return devInfo
@staticmethod
def get_friendly_name(VID_PID):
devices = []
devInfo = ""
devs = setupdi.devices(enumerator="USB")
for dev in devs:
try:
if "Ports" in dev.device_class:
devices.append(dev)
except (KeyError, AttributeError,):
continue
for x in devices:
try:
h = x.hardware_id
if h[1][8:12] + h[1][17:] == VID_PID:
devInfo = re.sub('[()]', '', re.search('\((.*?)\)', x.friendly_name).group(0))
except AttributeError:
devInfo = str(x)
pass
return devInfo
class device:
def __init__(self, *args):
self.deviceName = args[0]
self.VID_PID = args[1]
self.modelName = args[2]
pass
def open(self):
initDevice.devices.update({"first": "Two"})
pass