I have multiple Arduinos that I want to control using Python. My problem is that when I plug my Arduinos into the computer, they get assigned the port /dev/ttyUSBx
where x
increments with each one I connect.
Is there any way to reserve a certain number for a certain Arduino so that I can hard code that into the Python script? If not, can I scan the port to figure out which Arduino it is?
1 Answer 1
Yes. You need to create new unique names for them using udev rules.
By using the unique serial number, along with the VID and PID of the board you can use udev to create a symbolic link to a device name of your choice. Best to keep it to something that the Arduino IDE recognises if you want to access the link from within there for programming (otherwise you can choose what you like).
Rules are stored in /etc/udev/rules.d and take the form of .rules
files with various instructions in. One example, that should do what you want, is:
SUBSYSTEMS=="usb", ATTRS{product}=="Arduino Uno", ATTRS{serial}=="6493832323135111C0A1", KERNEL=="ttyACM*", SYMLINK+="ttyACM50", MODE="0666", GROUP="plugdev"
Of course, tweak it to your personal requirements.
Once you have crafted your rules be sure to restart udev:
sudo systemctl restart udev
or
sudo /etc/init.d/udev restart
and plug your board in.
Genuine Arduino devices usually use /dev/ttyACM* not /dev/ttyUSB*. If you have /dev/ttyUSB* then you are either on a very old device (with an FT232 chip) or a cheap Chinese clone.
If the latter then your mileage may vary somewhat.
-
Stepping on standard namespaces is probably not the best idea though. Naming the symlink something like "arduinoNN" might be a better idea.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2016年04月28日 01:09:13 +00:00Commented Apr 28, 2016 at 1:09
-
Then the IDE won't see it. You notice I chose a name way up in the numbers - one not likely to be used under normal circumstances.Majenko– Majenko2016年04月28日 01:09:45 +00:00Commented Apr 28, 2016 at 1:09
/dev/ttyUSBx
?lsusb -v
.