Page 1 of 1

Assigning same USBtty linux to a device every time

Posted: Tue Oct 28, 2014 5:36 am
by bobdx_cool
I have 2 serial communication devices connected to pi, which tty/USB0 and tty/USB1. I have defined in my code tty/USB0 for one device serial initialization and tty/USB1 for another device serial initializtion. But sometimes, what happens is that USB0 and USB1 get interchanged and causes a problem for me. HOw can I fix this so that each device uses that particular tty/USB all the time even after a reboot.

Re: Assigning same USBtty linux to a device every time

Posted: Tue Oct 28, 2014 8:58 am
by rpdom
You may be able to do it with a udev rule if the devices have different serial numbers.

To find the serial numbers, plug in the devices and do this

Code: Select all

pi@raspberrypi:~$ sudo udevadm info --query=property --name=/dev/ttyUSB0 | grep SERIAL
ID_SERIAL=Generic_USB_to_Serial_Converter_OCB6NCRD
ID_SERIAL_SHORT=OCB6NCRD
Do the same for ttyUSB1.
Now create a rules file:

Code: Select all

pi@raspberrypi:~$ sudo nano /etc/udev/rules.d/99-usbserial.rules
(add these lines)
ACTION=="add",ENV{ID_BUS}=="usb",ENV{ID_SERIAL_SHORT}=="OCB6NCRD",SYMLINK+="ttyUSBPort1"
ACTION=="add",ENV{ID_BUS}=="usb",ENV{ID_SERIAL_SHORT}=="JHG7GJFF",SYMLINK+="ttyUSBPort2"
(save and exit)
(Put in the serial numbers that you get from the above info command).

Now when you plug in the USB devices they will still get ttyUSB0 and ttyUSB1 seemingly randomly, but they will also get ttyUSBPort1 and ttyUSBPort2 which will always be assigned to that particular device.

You may need a "sudo udevadm control --reload-rules" to get the new rule to be read, but I've never needed that.

Re: Assigning same USBtty linux to a device every time

Posted: Tue Oct 28, 2014 3:14 pm
by pyrotuc
this is great. ive got one of those x10 "firecracker" modules, through a usb->db9 rs232 adaptors, and i added a rc.local rule to create the "firecracker" dev symlink to the usb device, but sometimes other peripherals grabbed ttyUSB0 first, and it drove me crazy trying to figure out why my system randomly broke. i think ill add rules for every usb device i use, so thing are predictable.

Re: Assigning same USBtty linux to a device every time

Posted: Mon Dec 08, 2014 7:03 pm
by bobdx_cool
rpdom wrote:You may be able to do it with a udev rule if the devices have different serial numbers.

To find the serial numbers, plug in the devices and do this

Code: Select all

pi@raspberrypi:~$ sudo udevadm info --query=property --name=/dev/ttyUSB0 | grep SERIAL
ID_SERIAL=Generic_USB_to_Serial_Converter_OCB6NCRD
ID_SERIAL_SHORT=OCB6NCRD
Do the same for ttyUSB1.
Now create a rules file:

Code: Select all

pi@raspberrypi:~$ sudo nano /etc/udev/rules.d/99-usbserial.rules
(add these lines)
ACTION=="add",ENV{ID_BUS}=="usb",ENV{ID_SERIAL_SHORT}=="OCB6NCRD",SYMLINK+="ttyUSBPort1"
ACTION=="add",ENV{ID_BUS}=="usb",ENV{ID_SERIAL_SHORT}=="JHG7GJFF",SYMLINK+="ttyUSBPort2"
(save and exit)
(Put in the serial numbers that you get from the above info command).

Now when you plug in the USB devices they will still get ttyUSB0 and ttyUSB1 seemingly randomly, but they will also get ttyUSBPort1 and ttyUSBPort2 which will always be assigned to that particular device.

You may need a "sudo udevadm control --reload-rules" to get the new rule to be read, but I've never needed that.
Thank you. But are these assignments done at boot or after boot ?

Re: Assigning same USBtty linux to a device every time

Posted: Sat Feb 07, 2015 2:29 pm
by RolfBly
I had the same problem and found the syntax for the rule to be quite critical. Also, I never found a single document that explained everything I wanted to know about this topic. So I compiled one of my own, to be found here. I hope it's useful to some.

Re: Assigning same USBtty linux to a device every time

Posted: Sat Feb 07, 2015 3:19 pm
by Richard-TX
Welcome to udev hell.

Udev is what happens when someone does not think about the negative consequences of adding a feature and then burns their bridges behind them.

Udev should be something that is optional or at least provide an easy way to hard code device names. Adding device names via a udev entry is simply a work around to compensate for poor architectural planning.

The best I can say about udev is that it works mostly.

Richard

"The measure of success is not determined by how well a process is adhered to, but in how well the exceptions are handled."