4

I'm connecting a USB-to-RS-485 adapter to my Raspberry Pi.

Enter image description here

Enter image description here

How do I know which /dev/ttyXXX it corresponds to? I'm using Raspberry Pi model B+

Enter image description here

Update with goldilock's answer:

Enter image description here

8

A refinement to oh.dae.su's method:

dmesg | grep "tty"

Another trick would be to save the state of /dev before plugging it in:

ls -1 /dev > dev.txt

Plug it in, do it again, check what's changed:

ls -1 /dev > dev2.txt
diff dev.txt dev2.txt

When you're done, rm dev*.txt to delete the two files.

Serial device attached to USB ports will generally end up with that in their name. In this case it looks to be ttyUSB0.

By the way, how do I know which port (out of the 4) has a serial port? by trial and error?

All the physical USB ports on the pi are equivalent; none of them has any special properties relative to another.

The device node name is not based on a static numbering of the USB ports, such that if you plugged it into the port next over you would get ttyUSB2 instead. It's based on the order in which the interface is created by the kernel (notice, no serial devices plugged in, no ttyUSB entries in /dev).

So, if you plugged another serial device into another USB port with the first one still plugged in, that would be ttyUSB1. However, if you unplugged the original one first, that dev node interface would be removed, and the new one would probably end up as ttyUSB0. But if you removed the original one after plugging the second one in, ttyUSB0 would disappear, but ttyUSB1 would still exist (i.e., they don't get re-assigned as long as they remain plugged in).

Sometimes a USB interface dev node may end up remaining in existence for a bit after you remove the device, so, e.g, if you unplug the device and then plug it in right away again, it may end up as ttyUSB1 even though you never had two devices plugged in at the same time.

There are ways to make sure a device always has the same dev node name when you plug it in, a name you assign to it. However, this is a bit tricky (and the subject of a different question), so unless this really becomes a problem for you, don't worry about it. As long as you are only using one serial device at a time and aren't plugging it in and out quickly it will always be ttyUSB0.

5
  • 1
    Your second solution is also nice! For the dmesg | grep "dev/tty" you have to be careful. In my experience the dmesg output is typically without the dev.
    – oh.dae.su
    Aug 14 '18 at 12:06
  • Thanks @goldilocks, it's indeed ttyUSB0 thanks. Just wondering, I've 4 USB ports on raspberry pi model b+ , however there's only ttyUSB0. Why there're no ttyUSB1, ttyUSB2, ttyUSB3? Aug 14 '18 at 14:04
  • @user1872384 you've got 4 physical USB ports, but only one has a serial port (or tty) attached, so you only see /dev/ttyUSB0.
    – scruss
    Aug 14 '18 at 16:26
  • I see. By the way, how do I know which port (out of the 4) has a serial port? by trial and error? Aug 15 '18 at 2:15
  • I've added some stuff at the bottom about this.
    – goldilocks
    Aug 15 '18 at 15:01
4

After you have plugged in the device to the USB port call

dmesg

In one of the last lines of output normally you will see the dev/ttyxxx to which it is attached.

1
  • Thanks @oh.dae.su. However, I couldn't comprehend the output of dmesg. Aug 14 '18 at 14:07
4

If you've only got one device attached, it'll most likely always get /dev/ttyUSB0. Things get more complex when you have multiple adapters, and then the /dev/serial/by-id/* aliases become more useful. On my machine, they look like:

  • /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A10175UI-if00-port0 -> ../../ttyUSB0

  • /dev/serial/by-id/usb-FTDI_USB__-__Serial-if00-port0 -> ../../ttyUSB1

The first one is a real FTDI adapter (it has a serial number, A10175UI), the second a cheap clone of an FTDI (no serial number). If you access the port by its by-id location, it'll pick up the same physical adapter every time.

1

I have the following script (found somewhere on the Internet) which lists USB devices together with human-readable names:

#!/bin/bash

for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
    (
        syspath="${sysdevpath%/dev}"
        devname="$(udevadm info -q name -p $syspath)"
        [[ "$devname" == "bus/"* ]] && exit
        eval "$(udevadm info -q property --export -p $syspath)"
        [[ -z "$ID_SERIAL" ]] && exit
        echo "/dev/$devname - $ID_SERIAL"
    )
done

The output looks like this:

/dev/input/mouse0 - 0000_USB_OPTICAL_MOUSE
/dev/input/event0 - 0000_USB_OPTICAL_MOUSE
/dev/ttyUSB0 - 1a86_USB2.0-Serial
/dev/ttyACM0 - Arduino__www.arduino.cc__0042_55936343933351B030F0
/dev/input/event1 - Logitech_USB_Receiver
/dev/input/event2 - Logitech_USB_Receiver
/dev/input/mouse1 - Logitech_USB_Receiver
/dev/sda2 - WD_Elements_2620_-0:0
/dev/sda - WD_Elements_2620_-0:0
/dev/sda1 - WD_Elements_2620_-0:0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.