def serial_ports():
if sys.platform.startswith('win'): # windows COM port enumerations
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'): # linux and cygwin support
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'): # OSX support
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports: # for every possible port name
try:
s = serial.Serial(port) # open it and close it
s.close()
result.append(port) # if nothing went wrong, the port exists on the system
except (OSError, serial.SerialException): # if something went wrong, the port does not exist
pass
return result # return the list of ports that currently actually exist on the system
评论列表
文章目录