-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialUtils.py
More file actions
99 lines (87 loc) · 3.08 KB
/
serialUtils.py
File metadata and controls
99 lines (87 loc) · 3.08 KB
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
# coding: utf-8
"""serialUtils.py: Python serial port utils"""
__author__ = "David Leval"
__copyright__ = "Copyright 2020, DLE-Dev"
__license__ = "GPL"
__version__ = "1.1"
__email__ = "dleval@dle-dev.com"
import sys
import glob
import serial
def list_serial_ports():
"""
Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def listUSBSerialPorts():
"""
Lists USB serial port available on the system
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
USB serial ports list with VID, PID, Serial ...
"""
if sys.platform.startswith('win'):
import serial.tools.list_ports
ports = list(serial.tools.list_ports.comports())
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
import serial.tools.list_ports_linux
ports = list(serial.tools.list_ports_linux.comports())
elif sys.platform.startswith('darwin'):
import serial.tools.list_ports_osx
ports = list(serial.tools.list_ports_osx.comports())
else:
raise EnvironmentError('Unsupported platform')
result = []
for port_no, description, address in ports:
if 'USB' in address:
result.append([port_no, description, address])
return result
def USBSerialFind(*args):
"""
Search for USB serial device with information (PID, VID, Serial, Description ...)
:raises SystemError:
(No USB serial device detected) or
(No USB serial device with all args detected)
:returns:
Port corresponding to the serial USB device
"""
port_ret = []
serialPortsAvailable = listUSBSerialPorts()
if bool(serialPortsAvailable):
for port in serialPortsAvailable:
port_no, description, address = port
if all(x in (description + address) for x in [*args]):
# print("Find Device:", description)
port_ret.append(port_no)
if not port_ret:
raise SystemError(f"No USB serial device with {args} detected")
return port_ret
else:
raise SystemError('No USB serial device detected')
def connectUSBSerialFind(*args, baudrate):
devicePort = USBSerialFind(*args)
deviceCom = serial.Serial(devicePort[0], baudrate, timeout=2)
print("Open USB serial port :", devicePort[0])
return deviceCom