Assign fixed device name to USB port

From Domoticz
Jump to: navigation, search

This page strongly overlas with PersistentUSBDevices.
If you have multiple USB devices connected, it could happen that after a reboot the device order is changed (ttyUSB0 is ttyUSB1, or the other way around)

The good news is, that there is a solution for this. This page explains how to 'lock' the serial ports into place.

Introduction

When you plug in multiple USB devices that emulate a terminal (for instance the P1 cable and RFXcom tranceiver) the Raspberry randomly allocates ttyyUSBx device names. This means that if Domoticz is configured to use ttyUSB0 for the P1 cable, the device may show up at ttyUSB1 after the next reboot. This means the P1 connection no longer works. You can assign a fixed device name for a given USB device.

Procedure

From a terminal window:

1. find out what's on ttyUSB:

[email protected]:~ $ dmesg | grep ttyUSB 
[4.074363] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUSB0
[4.075590] usb 1-1.3: FTDI USB Serial Device converter now attached to ttyUSB1

Take not of the node numbers: In this case 1-1.2 and 1-1.3

2. list all attributes of the devices (in this case USB0 and USB1):

[email protected]:~ $ udevadm info --name=/dev/ttyUSB0 --attribute-walk
...
[email protected]:~ $ udevadm info --name=/dev/ttyUSB1 --attribute-walk
...
 looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2':
   KERNELS=="1-1.2"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb"
   ATTRS{bDeviceSubClass}=="00"
   ATTRS{bDeviceProtocol}=="00"
   ATTRS{devpath}=="1.2"
   ATTRS{idVendor}=="0403"
   ATTRS{speed}=="12"
   ATTRS{bNumInterfaces}==" 1"
   ATTRS{bConfigurationValue}=="1"
   ATTRS{bMaxPacketSize0}=="8"
   ATTRS{busnum}=="1"
   ATTRS{devnum}=="4"
   ATTRS{configuration}==""
   ATTRS{bMaxPower}=="90mA"
   ATTRS{authorized}=="1"
   ATTRS{bmAttributes}=="a0"
   ATTRS{bNumConfigurations}=="1"
   ATTRS{maxchild}=="0"
   ATTRS{bcdDevice}=="0600"
   ATTRS{avoid_reset_quirk}=="0"
   ATTRS{quirks}=="0x0"
   ATTRS{serial}=="A1LPXWF"
   ATTRS{version}==" 2.00"
   ATTRS{urbnum}=="179474001"
   ATTRS{ltm_capable}=="no"
   ATTRS{manufacturer}=="RFXCOM"
   ATTRS{removable}=="removable"
   ATTRS{idProduct}=="6001"
   ATTRS{bDeviceClass}=="00"
   ATTRS{product}=="RFXtrx433"
...

Look at tree leaf matching the node name from the prior step. I this case ..../usb1/1-1/1-1.3 and ..../usb1/1-1/1-1.2 You have to look at a key differentiator between the devices. In most cases you can use the idVendor field (0403 in this case) or the idProduct field (6001 in this case) as differentiator between the two devices. In case the vendor ID is the same, you can use the serial number of the two devices.

3. We are now going to create a new USB device linked to the unique information we have. Create a file /etc/udev/rules.d/10-usb-serial.rules with rules for all USB devices in it:

[email protected]:~ $ sudo nano /etc/udev/rules.d/10-usb-serial.rules

Type the following in the editor and then save the file (ctrl-o) and (ctrl-x):

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTR{serial}=="P11234567",  SYMLINK+="ttyUSB_P1"
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTR{serial}=="RFX1234567",  SYMLINK+="ttyUSB_RFX"

Note: the serial numbers, idVendor and idProduct shown need to be replaced by your ids In case you have devices that have different vendor ids you can leave out the Serial attribute part:

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SYMLINK+="ttyUSB_P1"
SUBSYSTEM=="tty", ATTRS{idVendor}=="0406", ATTRS{idProduct}=="6002", SYMLINK+="ttyUSB_RFX"


4. Load the new rule:

[email protected]:~ $ sudo udevadm trigger

5. Verify what happened:

[email protected]:~ $ ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 Nov 11 10:29 /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 1 Nov 11 10:00 /dev/ttyUSB1
lrwxrwxrwx 1 root root         7 Nov 11 10:00 /dev/ttyUSB_P1 -> ttyUSB1
lrwxrwxrwx 1 root root         7 Nov 11 10:00 /dev/ttyUSB_RFX1 -> ttyUSB0

As you see we still have the existing USB0 and USB1 devices, but also have 2 symbolic links to devices that are match the right hardware. These symbolic links will be created at each reboot or when the USB device is plugged into an USB port.

6. Now in Domoticz below the Hardware menu change the device for the P1 cable to ttyUSB_P1 and the one for RFXCOM to ttyUSB_RFX1

Background

Article with more background on Stackexchange: [1]