List all serial port in linux

UNIX — list all existing serial ports

I’m writing a C program that’s automatically logging in to some connected Linux device (via Serial Port) and reading all of its logfiles etc.
So here’s the problem: I don’t want to hardcode the serial port (in my case /dev/ttyS0 ) into my code, but give some kind of prompt at the beginning, listing all physically existing devices from which I can choose from and pass it as a parameter.
Is there a way to distinguish between logical and physical devices in the /dev folder? I don’t want to use ls in the /dev folder and have all the (in my case) unnecessary information displayed; I only want the actually existing serial ports to be shown.

Please consider not restricting the user to a port from the list. For certain applications it might be useful to have your program operate on a file that is not really a serial port.

1 Answer 1

You can enum all the ttys in the system by reading the symlinks in directory /sys/class/tty/ .

Then you can read the type pseudofile to check if it is a real serial port or a virtual one. The possible values are in :

#define PORT_UNKNOWN 0 #define PORT_8250 1 #define PORT_16450 2 #define PORT_16550 3 #define PORT_16550A 4 #define PORT_CIRRUS 5 /* usurped by cyclades.c */ #define PORT_16650 6 #define PORT_16650V2 7 #define PORT_16750 8 #define PORT_STARTECH 9 /* usurped by cyclades.c */ #define PORT_16C950 10 /* Oxford Semiconductor */ #define PORT_16654 11 #define PORT_16850 12 #define PORT_RSA 13 /* RSA-DV II/S card */ 

Most virtual ports will not even have a type file. Anyway, a 0 will probably mean a virtual or emulated port.

Источник

How do I know which serial port to use on Linux?

Solution: USB port information (free or occupied) can be shown by: or Which shows output including (not taken on an RPi): bus info: usb@3 bus info: usb@4 bus info: usb@4:1 bus info: usb@1 bus info: usb@1:1 bus info: usb@1:1.2 bus info: usb@1:1.4 bus info: usb@2 bus info: usb@2:1 bus info: usb@2:1.5 Solution: will immediately and specifically tell you if the port is not available ( ). Solution 3: Check out — you should see an uart like instead of and should be > 0 for existing ports.

How do I know which serial port to use on Linux?

The command dmesg will show you the kernel message when the module is plugged in which will give it’s device name.

The /proc file system is like the device manager on windows — somewhere in there will be a list of tty devices

Two things that I have used: (a) look for control lines (DTR, DSR, etc) and (b) open ‘all’ of the ports and find out which one(s) appear to be active. In the latter case it helps if you can send a message to the serial device and have it respond; this obviously works only if the device will respond to a message.

Читайте также:  Linux увеличить размер файла подкачки

Check out /proc/tty/driver/serial — you should see an uart like 16550A instead of unknown and rx should be > 0 for existing ports. If you must guess which port will be used, open all available ports. After that, you need to set up the port for your needs (baudrate, parity, bits etc) or try to guess the incoming baudrate etc.

How do I know which serial port to use on Linux?, Two things that I have used: (a) look for control lines (DTR, DSR, etc) and (b) open ‘all’ of the ports and find out which one(s) appear to be active. In the latter case it helps if you can send a message to the serial device and have it respond; this obviously works only if the device will respond to a message. Share …

How to check if port is available

According to strace -o trace.out netstat -at

netstat does this by looking at

The used ports are in hex in the second field of the entries in that file.

You can get the state of the connection by looking at the 4th field, for example 0A is LISTEN and 01 is ESTABLISHED.

The holy portable BSD socket API won’t allow you to know whether the port is in use before you try to allocate it. Don’t try to outsmart the API. I know how tempting it is, I’ve been in that situation before. But any superficially smart way of doing this (by e.g. the proc filesystem) is prone to subtle errors, compatibility problems in the future, race conditions and so forth.

Grab the source of the netstat command and see how it sees. However, you will always have a race. Also, SO_REUSEADDR won’t let you use a port someone else is actively using, of course, just one in close-wait.

How can I check which ports are busy and which ports, Usage: option -l for listening ports, option -n to bypass DNS resolution, and the filter on source port NN: src :NN (replace NN by the port you want to monitor). For more options, see man ss ss -ln src :NN Examples: Code samplefi# output:Port 81 is NOT in use (result == 0)Feedback

List all available USB-ports Linux

USB port information (free or occupied) can be shown by:

$ sudo lshw | grep -i -B5 -A3 'logical name: usb' 
$ sudo lshw | grep -i -B4 -A3 'usb@' 

Which shows output including (not taken on an RPi):
bus info: usb@3
bus info: usb@4
bus info: usb@4:1
bus info: usb@1
bus info: usb@1:1
bus info: usb@1:1.2
bus info: usb@1:1.4
bus info: usb@2
bus info: usb@2:1
bus info: usb@2:1.5

How to get list of port which are in use on the server, TCPView is a Windows program that will show you detailed listings of all TCP and UDP endpoints on your system, including the local and remote addresses and state of TCP connections. On Windows Server 2008, Vista, NT, 2000 and XP TCPView also reports the name of the process that owns the endpoint. TCPView …

Best perl method to determine if a TCP port is available

bind will immediately and specifically tell you if the port is not available ( EADDRINUSE ). The other options don’t make any sense.

Since it sounds like you don’t actually care to what port you are bound, it seems to me that a better approach would be to bind to port 0 , which binds to an available port. You can determine the port to which your socket was bound using sockaddr_in .

Читайте также:  Archiving in linux tar

If, on the other hand, the desired port is within 1..1023, then it’s simply up to you to reserve the port for your program. The system won’t automatically assign a service to those ports. (I’m not sure if that’s true on Windows.)

Efficiently test if a port is open on Linux?, netstat -an |grep 445 |grep LISTEN (Takes seconds) 3. telnet (it doesn’t return) 4. nmap, netcat are not available on the server It will be nice to know of a way that doesn’t enumerate first and greps after that. linux bash shell port Share Improve this question edited Mar 21, 2019 at 8:42 jww 92.2k 84 378 832 asked Mar 7, …

Источник

How to list all serial ports on Linux

Feature image for the article about 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:

CuteCom screenshot highlighting how you can use it to list the Linux serial port devices.

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.
Читайте также:  Linux debian обновить драйвера

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:

Terminal screenshot that show you how to list all serial devices detected by the Linux kernel.

  • 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:

Terminal screenshot that shows you how to list all serial devices detected by the Linux kernel, exluding the virtual and pseudo terminals.

  • 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:

Final terminal output of listing the serial port devices on Linux. This time it outputs the correct device names, needed to connect to the serial port device.

  • 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:

  1. Bookmark this page in your web browser and visit it whenever you need to list the serial ports from the terminal.
  2. 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:

Editing .bashrc to add the

  • 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:

Terminal screenshot showing the output of the newly added

  • lsserial

Wrap up

This article presented you with two different methods for listing the available serial ports on your Linux PC.

  1. Using the CuteCom GUI application.
  2. 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:

Источник

Оцените статью
Adblock
detector