Mapping tty ports by serial number

When you have several devices between ARS, Stacks, etc. that are connected to a single host (computer Linux such as Raspberry Pi), you have the disadvantage of not knowing exactly what the name of the device because Linux will assign their names as they are connected, so sometimes one can be ttyACM00, ttyACM01, …

Example of 4 ARS-USB units connected into a Raspberry Pi (Aka RsPi)

pi@raspberrypi:~$ dir -1 -A -l /dev/ttyA*
crw-rw—T 1 root dialout 166,  0 Sep  9 14:02 /dev/ttyACM0
crw-rw—T 1 root dialout 166,  1 Sep  9 14:03 /dev/ttyACM1
crw-rw—T 1 root dialout 166,  2 Sep  9 14:07 /dev/ttyACM2
crw-rw—T 1 root dialout 166,  3 Sep  9 14:07 /dev/ttyACM3

When we need to map ports to serial devices in almost essential to have predetermined their names to establish routes, as is done with the ser2net service, so we’ll use Udev which does the basic device identification.

What is Udev?

Udev is the device manager for the Linux 2.6 kernel that creates/removes device nodes in the /dev directory dynamically. It is the successor of devfs and hotplug. It runs in userspace and the user can change device names using Udev rules.

Udev will use the Product ID (Pid) + Vendor ID (Vid) + Serial number to identifier a device and will assign a name for it. So remember in case you want to use for instance 2 ARS-USB units, as they will use the same Pid + Vid, you need a different serial number.

The latest versions of the firmware of the ARS units, Stacks, etc. (i.e. in the ARS from V2.3A) include a serial number that can be used by Udev to recognize each device, using this feature to create a serial association.

If you have an earlier version of the ARS software (or other EA4TX devices) that does not include this functionality, you must update it and remember you have to use a different serial number for each unit.

Each serial firmwares are easily identified by the last name of the binary name, example:
–    rci-usb-boot_23e_00001.hex
–    rci-usb-boot_23e_00002.hex

The first file uses serial 00001 and the second 00002

For instance, if you have a ARS-USB device with serial number and you want to know it, it’s simple. Power off the ARS, connects it to a Linux computer and run the following command:

    udevadm monitor –-environment

Then power on the ARS and you will read as follows:

UDEV  [3740.322132] add      /devices/platform/soc/20980000.usb/usb1/1-1/1-1.4 (usb)

ACTION=add

BUSNUM=001

DEVNAME=/dev/bus/usb/001/013

DEVNUM=013

DEVPATH=/devices/platform/soc/20980000.usb/usb1/1-1/1-1.4

DEVTYPE=usb_device

ID_BUS=usb

ID_MODEL=USB_Serial

ID_MODEL_ENC=USB\x20Serial

ID_MODEL_ID=000a

ID_REVISION=0100

ID_SERIAL=EA4TX_USB_Serial_00001

ID_SERIAL_SHORT=00001

ID_USB_INTERFACES=:020201:0a0000:

ID_VENDOR=EA4TX

ID_VENDOR_ENC=EA4TX

ID_VENDOR_ID=04d8

MAJOR=189

MINOR=12

PRODUCT=4d8/a/100

SEQNUM=920

SUBSYSTEM=usb

TYPE=2/0/0

UDEV_LOG=3

USEC_INITIALIZED=3740321035

Note: This is a summary output.

In this example ID_SERIAL is 00001

Remember to upgrade the firmware in case your unit doesn’t include a serial number with the ARS_Loader tool. The firmware will include a serial number since ARS v.2.3
 
Configuration

Knowing the serial number of a device, we can create a rule by Udev to assigned to a symlink or persistent name.

Now with the list of serial numbers in hand let’s create a UDEV rule set that’ll make a nice symbolic link for each of these devices. UDEV rules are usually scattered into many files in /etc/udev/rules.d. Create a new file called 99-ars-usb.rules and put the following lines in there.
Example:
sudo nano /lib/udev/rules.d/99-ars-usb.rules

Add a line peer each device you want to identify, in this example we’ll use 6 ARS-USB and 4 stacks:

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”00001″ , SYMLINK+=”ars01″

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”00002″ , SYMLINK+=”ars02″

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”00003″ , SYMLINK+=”ars03″

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”00004″ , SYMLINK+=”ars04″

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”00005″ , SYMLINK+=”ars05″

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”00006″ , SYMLINK+=”ars06″

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”S0010″ , SYMLINK+=”stack1”

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”S0015″ , SYMLINK+=”stack2”

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”S0020″ , SYMLINK+=”stack3”

SUBSYSTEM==”tty”, ATTRS{idVendor}==”04d8″, ATTRS{idProduct}==”000a”, ATTRS{serial}==”S0040″ , SYMLINK+=”stack4”

By now it should be obvious what these lines mean. Udev will assign symlink = ars01 to the device:
    IdVendor:  04d8
    IdProduct: 000A
    Serial:       00001
And so on with the rest.

Note: Once saved the file, you should probably reboot the machine.

From now on whenever the ARS (serial 00001) is detected, Linux will assign ars01 device name (and similarly with the rest)

You can check doing an “ls” or “dir” on the \dev folder, example:
    ls /dev

And with these names, let ser2net service and create the routes or routing TCP serial port, as follows:

6000:raw:600:/dev/ars01:38400 8DATABITS NONE 1STOPBIT banner

6001:raw:600:/dev/ars02:38400 8DATABITS NONE 1STOPBIT banner

6002:raw:600:/dev/ars03:38400 8DATABITS NONE 1STOPBIT banner

6003:raw:600:/dev/ars04:38400 8DATABITS NONE 1STOPBIT banner

6004:raw:600:/dev/ars05:38400 8DATABITS NONE 1STOPBIT banner

6005:raw:600:/dev/ars06:38400 8DATABITS NONE 1STOPBIT banner

6010:raw:600:/dev/stack1:38400 8DATABITS NONE 1STOPBIT banner

6011:raw:600:/dev/stack2:38400 8DATABITS NONE 1STOPBIT banner

6012:raw:600:/dev/stack3:38400 8DATABITS NONE 1STOPBIT banner

6013:raw:600:/dev/stack4:38400 8DATABITS NONE 1STOPBIT banner

   
Thus the ser2net associated service incoming connections to port 6000 with ars01 (having serial 00001) or 6001 with ars02 and so on.

Remember to restart the service ser2net with the command:   
    sudo /etc/init.d/ser2net restart