Detect Arduino port in Python [duplicate]
I am using an Arduino for sensing u开发者_StackOverflow社区sing 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
I recommend a handshaking signal and scanning all the ports. For example, send "whoru" from your python script to the arduiono and have code on the arduiono that responds with "arduino" when it detects "whoru" on the serial port. This way, you scan the ports, send the handshake, and when you get the proper response you know which port the arduino is on.
精彩评论