{"id":3704,"date":"2023-12-11T16:07:16","date_gmt":"2023-12-11T16:07:16","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=3704"},"modified":"2023-12-11T16:07:17","modified_gmt":"2023-12-11T16:07:17","slug":"assign-fixed-port-name-to-a-usb-device","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/uart-serial-port\/assign-fixed-port-name-to-a-usb-device","title":{"rendered":"Assign fixed port name to a USB device"},"content":{"rendered":"\n<p>For example, to always refer to the same USB to serial adapter by the same port name, when you have more than one connected.<\/p>\n\n\n\n<p>This guide is based on the excellent guide <a href=\"https:\/\/www.freva.com\/assign-fixed-usb-port-names-to-your-raspberry-pi\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">1 &#8211; Get a list of your USB devices<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>dmesg | grep ttyUSB<\/code><\/pre>\n\n\n\n<p>Example result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUSB0\r<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2 &#8211; Get specific attributes for the device<\/h5>\n\n\n\n<p>For our example device we will use:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Its name &#8220;ttyUSB0&#8221;<\/li>\n\n\n\n<li>The numbers after &#8220;USB&#8221;, so &#8220;1-1.2&#8221;<\/li>\n<\/ul>\n\n\n\n<p>, use its attached to name like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>udevadm info --name=\/dev\/ttyUSB0 --attribute-walk<\/code><\/pre>\n\n\n\n<p>The results are for the complete chain of devices linked to the specified USB port., look for the section that ends with has our devices &#8220;1-1.2&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  looking at parent device '\/devices\/platform\/scb\/fd500000.pcie\/pci0000:00\/0000:00:00.0\/0000:01:00.0\/usb1\/1-1\/1-1.2':\r\n    KERNELS==\"1-1.2\"\r\n    SUBSYSTEMS==\"usb\"\r\n    DRIVERS==\"usb\"\r\n    ATTRS{speed}==\"12\"\r\n    ATTRS{bDeviceClass}==\"00\"\r\n    ATTRS{bMaxPower}==\"90mA\"\r\n    ATTRS{manufacturer}==\"FTDI\"\r\n    ATTRS{idProduct}==\"6001\"\r\n    ATTRS{avoid_reset_quirk}==\"0\"\r\n    ATTRS{bmAttributes}==\"a0\"\r\n    ATTRS{quirks}==\"0x0\"\r\n    ATTRS{bDeviceProtocol}==\"00\"\r\n    ATTRS{busnum}==\"1\"\r\n    ATTRS{ltm_capable}==\"no\"\r\n    ATTRS{bcdDevice}==\"0600\"\r\n    ATTRS{bDeviceSubClass}==\"00\"\r\n    ATTRS{configuration}==\"\"\r\n    ATTRS{removable}==\"unknown\"\r\n    ATTRS{urbnum}==\"300690\"\r\n    ATTRS{devnum}==\"3\"\r\n    ATTRS{product}==\"FT232R USB UART\"\r\n    ATTRS{serial}==\"B400LXUC\"\r\n    ATTRS{version}==\" 2.00\"\r\n    ATTRS{authorized}==\"1\"\r\n    ATTRS{tx_lanes}==\"1\"\r\n    ATTRS{rx_lanes}==\"1\"\r\n    ATTRS{bNumInterfaces}==\" 1\"\r\n    ATTRS{bNumConfigurations}==\"1\"\r\n    ATTRS{devspec}==\"(null)\"\r\n    ATTRS{bMaxPacketSize0}==\"8\"\r\n    ATTRS{maxchild}==\"0\"\r\n    ATTRS{devpath}==\"1.2\"\r\n    ATTRS{idVendor}==\"0403\"\r\n    ATTRS{bConfigurationValue}==\"1\"\r<\/code><\/pre>\n\n\n\n<p>You want to get some unique attributes from this so this device can be uniquely identified.<\/p>\n\n\n\n<p>These are typically good:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    ATTRS{idProduct}==\"6001\"\n    ATTRS{idVendor}==\"0403\"\r<\/code><\/pre>\n\n\n\n<p>This can also be used if you are using multiple of the same device type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> ATTRS{serial}==\"B400LXUC\"<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">3 &#8211; Update the USB device list file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/udev\/rules.d\/10-usb-serial.rules<\/code><\/pre>\n\n\n\n<p>The file will open, it will be empty if its not been used before. Create a line for your device, setting the attributes to match what you got above and giving it the unique name your want to use in the SYMLINK+ fireld.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SUBSYSTEM==\"tty\", ATTRS{idProduct}==\"6001\", ATTRS{idVendor}==\"0403\", SYMLINK+=\"ttyUSB_MYDEVICE1\"<\/code><\/pre>\n\n\n\n<p>Save the file and close it.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">4 &#8211; Enter this command to let the new rules take effect<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo udevadm trigger<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">5 &#8211; Verify your new name has been assigned<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l \/dev\/ttyUSB*<\/code><\/pre>\n\n\n\n<p>In our example we get this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crw-rw---- 1 root dialout 188, 0 Dec 11 16:03 \/dev\/ttyUSB0\r\nlrwxrwxrwx 1 root root         7 Dec 11 16:03 \/dev\/ttyUSB_MYDEVICE1 -> ttyUSB0\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For example, to always refer to the same USB to serial adapter by the same port name, when you have more than one connected. This guide is based on the excellent guide here. 1 &#8211; Get a list of your USB devices Example result: 2 &#8211; Get specific attributes for the device For our example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,47],"tags":[],"class_list":["post-3704","post","type-post","status-publish","format-standard","hentry","category-uart-serial-port","category-usb"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=3704"}],"version-history":[{"count":5,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3704\/revisions"}],"predecessor-version":[{"id":3709,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3704\/revisions\/3709"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=3704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=3704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=3704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}