Page 1 of 1

udev rules, having a permanent usb-serial device name [opencpn]

Posted: 20 Aug 2020 17:30
by juliusse
note: This post is specific to gps-usb devices, and usage with navigation software like opencpn in this example, but this can be potentially adapted to every usb-serial device, like a mp3, serial-to-usb converter plug. For more info, udev is your friend, and a bit of RTFM :D ;)

Just a small trick. I write it there for record.
Using usb-serial devices, udev generally attributes the name /dev/ttyUSB* to the device. On certain cases,( I don't know if Slackel got this attitude), udev change the device name on sleep/wakup/boot/reboot, or when closing/opening the screen. To avoid this, I've got an udev rule file. It must be placed in /etc/udev/rules.d, and called for example in my case 70-persistent-usb-gps.rules
In the case you use this rule, when adjusting opencpn "connection" parameters:
1- if you're using the serial connection to send NMEA data you have to manually write the device name(in my case /dev/gps-bu-353).
like this
Image
2- if you're using gpsd, do it as usual (because gpsd is cleaver, and will automatically do this)
like this
Image
image compte google

here is: /etc/udev/rules.d/70-persistent-usb-gps.rules, you can copy it and use it as you want, feel free to use it and transform it

Code: Select all

# 
# Author: juliusse
# Date:   20-nov-2008
# Update: 15-aug-2020
# Object: make sure GPS dev (ex: /dev/gps-usb) don't change name on sleep/wakeup
# -----------------------------------------------------------------------------
#
# 1) place this file in /etc/udev/rules.d and name it for example 70-persistent-usb-gps.rules
# 2) use default config or update with your own vendor:product ID (use "lsusb" to find them)
# 3) reload udev with "sudo service restart udev"
#
# Device alias can be:
#  (default) - by path  ==> SYMLINK="serial-$env(ID_PATH)"   /dev/gps-pci-0000:00:1d.1-usb-0:1:1.0
#            - static   ==> SYMLINK="gps-usb"                /dev/gps-usb
#            - custom   ==> RUN+="/usr/local/bin/myscript"   /dev/any-thing-you-want
#
# DEFAULT CONFIG: you can use this file "as it is", you should then see a /dev/gps-pci* 
# that will be created for any of the serial/usb you hot-plug. The name is fixe but
# depend on the USB port you use. As a result the name is fix until you keep the same socket.
#
# -----------------------------------------------------------------------
# check "man 7 udev" for further syntax. (search for %n)
# -----------------------------------------------------------------------

# Examples
# -----------------------------------------------------------------------
# Your own script:     SUBSYSTEM=="tty", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", RUN+="/usr/local/bin/myscript"
# Static device name:  SUBSYSTEM=="tty", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", SYMLINK="gps-usb"
# Path dependent name: SUBSYSTEM=="tty", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", SYMLINK="gps-$env{ID_PATH}"
# In first case you do what ever you want, script receive the information about context in environement variables
# In second case you name is fixe (ex: /dev/gps-usb) this is working if your usb/serial ID(vendor:product) is unique
# In third case your device name depend on the port it is plugged in.


# ========================================================================================
#                          update YOUR CONFIG here after
# ========================================================================================

# Default rules is applied for any unspecified vendor:product devices
# -----------------------------------------------------------------------------------------
SUBSYSTEM=="tty", ATTRS{idVendor}=="?*", ATTRS{idProduct}=="?*", SYMLINK="gps-$env{ID_PATH}"

# Well known device may get a static device name 
# -----------------------------------------------------------------------
SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SYMLINK="gps-usb"
SUBSYSTEM=="tty", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", SYMLINK="gps-bu-353"
SUBSYSTEM=="tty", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="5740", SYMLINK="gps-ait2000"




Re: udev rules, having a permanent usb-serial device name [opencpn]

Posted: 21 Aug 2020 11:40
by djemos
This is really very useful for all using gps-usb devices. Thanks.