Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Arduino port in Python [duplicate]

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?

like image 506
Elliot Avatar asked Sep 23 '11 19:09

Elliot


2 Answers

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.

like image 89
Rachit Magon Avatar answered Nov 08 '22 14:11

Rachit Magon


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
like image 39
Julien Avatar answered Nov 08 '22 14:11

Julien