I'm connecting a USB-to-RS-485 adapter to my Raspberry Pi.
How do I know which /dev/ttyXXX it corresponds to? I'm using Raspberry Pi model B+
Update with goldilock's answer:
I'm connecting a USB-to-RS-485 adapter to my Raspberry Pi.
How do I know which /dev/ttyXXX it corresponds to? I'm using Raspberry Pi model B+
Update with goldilock's answer:
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.
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.
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
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.
dmesg | grep "dev/tty"you have to be careful. In my experience thedmesgoutput is typically without thedev. – oh.dae.su Aug 14 '18 at 12:06/dev/ttyUSB0. – scruss Aug 14 '18 at 16:26