bobdx_cool
Posts: 39
Joined: Fri Apr 04, 2014 11:23 pm

Assigning same USBtty linux to a device every time

Tue Oct 28, 2014 5:36 am

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.

User avatar
rpdom
Posts: 15428
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Assigning same USBtty linux to a device every time

Tue Oct 28, 2014 8:58 am

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

[email protected]:~$ 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

[email protected]:~$ 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.

pyrotuc
Posts: 10
Joined: Sat Dec 14, 2013 10:15 pm
Location: texas
Contact: Website AOL

Re: Assigning same USBtty linux to a device every time

Tue Oct 28, 2014 3:14 pm

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.
/*
Kris Tucker
http://pyrotuc.com
http://pirhotech.com
alias webExposedEmail='[email protected]'
sudo echo "1" > /sys/class/awesome
*/

bobdx_cool
Posts: 39
Joined: Fri Apr 04, 2014 11:23 pm

Re: Assigning same USBtty linux to a device every time

Mon Dec 08, 2014 7:03 pm

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

[email protected]:~$ 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

[email protected]:~$ 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 ?

RolfBly
Posts: 19
Joined: Mon Jul 16, 2012 7:07 pm

Re: Assigning same USBtty linux to a device every time

Sat Feb 07, 2015 2:29 pm

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.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Assigning same USBtty linux to a device every time

Sat Feb 07, 2015 3:19 pm

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."
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

Return to “Interfacing (DSI, CSI, I2C, etc.)”