9

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elliot
  • 5,211
  • 10
  • 42
  • 70
  • 1
    What operating system, Python version, and pyserial version? What have you tried? What isn't working? Details, man! – jathanism Sep 23 '11 at 20:01
  • Added stuff about the version and the OS, but all that any version of pyvisa or pyserial needs as a location input is a text string like 'COM3'. – Elliot Sep 24 '11 at 01:48

3 Answers3

2

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
Julien
  • 1,810
  • 1
  • 16
  • 34
2

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.

Rachit Kyte.One
  • 1,767
  • 21
  • 28
0

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.

Ray
  • 106
  • 1
  • 6
  • 2
    This would disrupt devices on other ports, as they would get input that they did not know the meaning of. One of the devices I am working with is a crappy piece of equipment, but there is nothing better to do the job. If it gets a bad signal, it locks up and the only way to reset it is to completely unplug it. – Elliot Sep 28 '11 at 01:18