I'm attempting to write a handshaking procedure to allow my python program to automatically detect which port the Arduino is on. It works, however sometimes it runs extremely slow. Here's the problematic part of my code:
import serial, serial.tools.list_ports, time
portList = list(serial.toools.list_ports.comports())
conn = 0
while conn == 0:
for port in portList:
print(port[0])
try:
arduino = serial.Serial(port[0], 4800, timeout=.1)
time.sleep(.2)
sig = arduino.read()
signum = int.from_bytes(sig, byteorder='little')
if signum == 7:
global comport
comport = port[0]
conn = 1
break
except:
print('Could not read from ' + str(port[0]))
Essentially I have the Arduino constantly sending the arbitrary number '7' to the serial port. The python script scans each port for a 7 until it finds it. What's happening is that when it gets to the port that the Arduino is on, the code seemingly pauses executing for about 10 seconds at the arduino = serial.Serial(...)
line right underneath the try statement. Since it's in a try loop, I know it's not throwing an error because it does eventually make it through. Is it just having trouble opening the port? How can this be fixed? I am on Python 3.4.3 and pySerial 2.7.