开发者

Detect Arduino port in Python [duplicate]

开发者 https://www.devze.com 2023-04-07 07:17 出处:网络
This question already has answers here: Python to automatically select serial ports (for Arduino) (4 answers)
This question already has answers here: Python to automatically select serial ports (for Arduino) (4 answers) Closed 6 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号