How do I know which /dev/ttyS* is my serial port?
Running that on my own Linux box (which only has 1 Serial port) produces a single ttyS0 output line. Try it on your own, you will see what I mean.
Does that mean that after running that command, if the response is . console [tty0] enabled. and no other, any device is connected to /dev/tty0 (sorry for the apparent silliness of the question)?
The only problem is that dmesg output can be cleared — so if you run this too late, you’re out of luck. Looking at /proc/tty/driver/serial seems the more robust answer and then check for rx interrupts increasing in count as you write data to that port
if you’re currently in a terminal over said serial line, you can just type tty to get the name of your tty. I assume that’s what OP meant since they did not pose the question as «which serial port(s) have getty running?»
See which UARTs where detected in /proc/tty/driver/serial . A line with uart:unknown means: nothing detected (and likely not existent).
# cat /proc/tty/driver/serial serinfo:1.0 driver revision: 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0 1: uart:16550A port:000002F8 irq:3 tx:111780 rx:1321 RTS|DTR|DSR 2: uart:unknown port:000003E8 irq:4 3: uart:unknown port:000002E8 irq:3
If you see any of the CTS, DSR, (D)CD or RI flags (these are input signals), like on UART no. 1 above, you can even be pretty sure that there actually is something connected and driving these lines. Same is true for the rx-byte-count.
Seeing a positive tx-byte-count, RTS and/or DTR only reveals that some software accessed the device and ordered it to set those signals or send bytes here, but not if something was listening.
Note: you might see more ports available in hardware than ports reaching the outside of your computer in form of a connector.
Федоренко на связи
В этой заметке я расскажу про то, как можно проверить COM порт в Linux.
Для этого нам понадобятся:
- права на чтение и запись в COM порт
- программа minicom
- кусок толстого многожильного кабеля
Права доступа
проверяем какие у нас в системе есть COM порты, а также наши права на них:
# ls -l /dev/ttyS* /dev/ttyUSB*
crw-rw—- 1 root dialout 4 , 64 Дек 21 11 : 46 / dev / ttyS0
crw-rw—- 1 root dialout 4 , 65 Дек 8 15 : 43 / dev / ttyS1
crw-rw—- 1 root dialout 4 , 66 Дек 8 15 : 43 / dev / ttyS2
crw-rw—- 1 root dialout 4 , 67 Дек 8 15 : 43 / dev / ttyS3
crw-rw—- 1 root dialout 188 , 0 Дек 21 07: 27 / dev / ttyUSB0
/dev/ttyS0 это первый COM порт
/dev/ttyS1 соответственно второй COM порт
/dev/ttyUSB0 специальный кабель-переходник из USB в COM (Gembird UAS111)
Как мы видим для работы с COM портом необходимы права root либо пользователь должен входить в группу dialout
Провод для связи
Берём многожильный кабель в оплётке длиной 4-6 см, и соединяем 2 и 3 выводы COM порта перемычкой из этого кусочка провода. Оплётка будет удерживать провод на контактах, а металлические жилы внутри будут проводниками.
Для любителей паять можно предложить спаять полный loop-back тестер. В нём соединить не только контакты приёма передачи, но и аппаратный контроль потока и готовность терминала.
Работа с программой minicom
Для начала необходимо настроить программу minicom
+————[конфигурация]————+
| Каталоги и файлы
| Протоколы
| Настройка последовательного порта
| Модем и дозвон
| Экран и клавиатура
| Сохранить настройки как dfl
| Сохранить настройки как.
| Выход
| Выход из Minicom
+————————————+
Выбираем «Настройка последовательного порта»
+————————————————————————+
| A — Последовательный порт : /dev/ttyS0
| B — Размещение lock-файла : /var/lock
| C — Программа при выходе :
| D — Программа при запуске :
| E — Скорость/Чётность/Биты : 115200 8N1
| F — Аппаратное управление потоком : Да
| G — Программное управление потоком : Нет
|
| Какую настройку изменить?
+————————————————————————+
Если нужно, меняем СОМ порт, нажимая «A»
Отключаем аппаратное управление потоком, нажимая «F»
Выходим из настроек «Enter»
Сохраняем настройки по умолчанию «Сохранить настройки как dfl»
Выходим из minicom
Собственно проверка СОМ порта
Запускаем minicom без параметров.
Пробуем нажимать на клавиши. В ответ на экране должны появляться символы.
Проверка COM порта под linux завершена.
Эта запись была сделана 21.12.2010, 13:50 под рубрикой Разное. Вы можете подписаться на эту запись с помощью RSS 2.0. Both comments and pings are currently closed.
How to list all serial ports on Linux
Linux comes with serial communication functionality built-in. Before you can communicate with a device, connected to your PC’s serial port, you just need to know the name of the serial port. On Windows it is typically COM1 , COM2 , etc. On Linux it is essentially a filename in the format of /dev/ttyS0 , /dev/ttyUSB0 or /dev/ttyACM0 . This article explains how to list all serial ports on a Linux based system.
Background
Especially when developing software for or interacting with microcontroller based systems, you often communicate using RS232 serial communication. Typically by means of an RS232-to-USB adapter. To communicate with an RS232 device, you need to know the name of the PC’s serial port to connect to. On Windows, you open up the device manager and look up the COM-port number. Listing the available serial ports on Linux is a bit different and desktop environments do not offer a graphical user interface to quickly identify the available serial ports. This article presents two methods for listing the serial ports on Linux. One with the help of a graphical user interface program and another one for working directly in the terminal.
What do you need
To complete the steps to list the available serial ports on Linux, you just need two things:
- A Linux based PC. It can be a desktop, server, virtual machine or a single board computer such as a Raspberry PI.
- A serial device connected to the PC. For example an RS232-to-USB adapter, an Arduino or a Raspberry Pico.
For this article I’ll be using my main openSUSE Tumbleweed system. It’s a Lenovo ThinkCentre with one actual physical serial port. Additionally, I connected two serial devices: An RS232-to-USB adapter and an Arduino Uno.
Listing the serial ports using a Linux GUI application
Unfortunately, Linux desktop environments do not offer a GUI program to quickly and conveniently show all the available serial ports. However we can simply install one that does. Chances are you want a GUI program for monitoring the serial port in Linux anyway. My recommendation: CuteCom. Installation instructions for CuteCom on popular Linux distributions:
- Debian and Ubuntu: sudo apt install cutecom
- Fedora: sudo dnf install cutecom
- openSUSE: sudo zypper install cutecom
Once installed, open up the CuteCom program. It shows a Device drop-down list on the top of the main window. And voilà, it lists all serial ports on your Linux system:
As you can see in the screenshot, CuteCom lists all three serial ports currently connected to my Linux system:
- /dev/ttyS0 → The physical serial port on my PC.
- /dev/ttyUSB0 → The RS232-to-USB adapter.
- /dev/ttyACM0 → The Arduino Uno board connected via USB cable.
Listing the serial ports using the Linux terminal
The past section showed that Linux assigns a file name to serial devices with the acronym TTY in it. TTY stands for teletypewriter. With these historical devices you could send typed text messages to a remote location, using serial communication. Eventually leading to the invention of the fax machine. Hardly anyone still uses these devices. Nevertheless, the term TTY stuck around.
When it comes to using the terminal to list the serial ports, I prefer the approach that looks for TTY devices that the Linux kernel detected. Basically those that it found and managed to assign a driver to. The following command lists all of these:
- ls -l /sys/class/tty/*/device/driver
That’s a long list. I expected just three devices. The command output includes all so-called virtual and pseudo terminals. We just want to list the actual hardware serial ports. We can filter out the ones we don’t want by removing all lines that contain platform/drivers/serial8250 . The command then becomes:
- ls -l /sys/class/tty/*/device/driver | grep -v «platform/drivers/serial8250»
Alright, that looks a lot better. The output lists the three TTY devices that I expected: ttyACM0 , ttyS0 and ttyUSB0 . Ideally, the output shows just the actual device filename, needed to connect to the serial port. With a little akw magic, we can clean the output up for this:
- ls -l /sys/class/tty/*/device/driver | grep -v «platform/drivers/serial8250″ | awk » | awk -F’/’ »
Bash alias for listing the serial ports
I enjoy making one-liners for the terminal to get stuff done. However, the length of this one is such that I will never remember it. And I bet that’s the thought that crossed your mind too. Two options to solve this:
- Bookmark this page in your web browser and visit it whenever you need to list the serial ports from the terminal.
- Create a Bash alias.
With a Bash alias you essentially add a new command to your user’s shell with a name of your choice. Aliases are stored in the .bashrc file inside your home directory. You can edit it with whatever text editor you prefer. I’ll just use good old Nano:
Once in the text editor, add the following line. Note that escape characters were added where needed, to make it work. That’s before every double-quote and dollar sign:
- alias lsserial=»ls -l /sys/class/tty/*/device/driver | grep -v \»platform/drivers/serial8250\» | awk » | awk -F’/’ »»
Once done, save your .bashrc file and type this command to reload it:
Alternatively, you can just close and reopen your terminal program.
From now on you can list the available serial ports on your Linux PC, by simply typing this command in the terminal:
- lsserial
Wrap up
This article presented you with two different methods for listing the available serial ports on your Linux PC.
- Using the CuteCom GUI application.
- Piping several commands together for a terminal one-liner.
Going with method two, this article explained how you can add a Bash alias for the constructed terminal one-liner. Afterwards you can list the serial devices by simply typing the lsserial command in the terminal.
If you work with serial ports in Linux, you might be interested in one of the other serial port related articles: