def find_serial_port():
"""
tries to connect to all available serial ports, returns of first successful connection.
exit if none works
:return: serial interface object
"""
if settings.debug_port_detection:
for port in list_ports.comports():
print('trying {}'.format(port[0]))
return serial.Serial(settings.forced_port, settings.baud_rate, timeout=1)
else:
for port in (list_ports.comports()):
print('trying {}'.format(port[0]))
try:
serial_interface = serial.Serial(port[0], settings.baud_rate, timeout=2)
sleep(2) # wait for the device to be ready
# send hello command
serial_interface.write(settings.handshake_challenge)
# check if this device knows what to reply
reply = serial_interface.read(len(settings.handshake_response))
print(reply)
if reply == settings.handshake_response:
return serial_interface
except serial.SerialException:
# print("opening serial failed")
pass
raise ConnectionError("Couldn't connect to any serial ports, exiting...")
评论列表
文章目录