I am using an Arduino for sensing using Python 2.7 on Windows XP, but the non-static nature of the USB-to-serial port translation is giving me a headache. With a physical serial port there is no issue hard coding the port position, but the Arduino is moving around based on what is or is not plugged in at the time of object instantiation. Is there some way in Python for me to just get the port address during each object initialization and pass it to PyVISA or pySerial?
I also suggest a handshake but do it the other ay round. Just READ for input from the all Serial ports before starting your program. As you turn up the Device you can make it send something like an ON signal. when your code detects the ON signal on that port then do a handshake.
In pySerial there is a quite hidden way to check for VID/PID on all serial ports (at least on Windows). Just find the VID/PID of the Arduino in port properties adn put it into the python code.
Of course this won't work if you have multiple Arduino connected (same VID/PID)
import serial.tools.list_ports
for port in list(serial.tools.list_ports.comports()):
if port[2].startswith('USB VID:PID=1234:5678'):
#here you have the right port
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With