Cookie
Electronic Team, Inc. uses cookies to personalize your experience on our website. By continuing to use this site, you agree to our cookie policy. Click here to learn more.

Linux Serial Ports - Full Guide

Editorial Team Editorial Team
Updated: Dec 24, 2024

Here, you can learn how to connect to a Linux serial device, as well as a simple way to connect attached serial devices to a remote computer. We will be using an ESC/POS compliant receipt printer as an example.

14-day Free Trial available
License price starts at $259.95
Available for
usb to remote desktop

Checking Your Ports

Serial ports are a type of serial device. The dmesg command will help you see if a serial device was detected and recognized by the operating system. If it was, it will be listed in the output of this command.

Since dmesg lists all of the system’s devices, we will filter the output through grep. It searches text for lines that contain the provided keyword. The names of serial ports on Linux start with “ttyS”, so we can use that as the query. Output from dmesg can be passed to grep by connecting the two commands with a pipe (the | symbol). It will also require superuser privileges, so sudo needs to be added. The resulting command looks like this:

sudo dmesg | grep tty
screen 1
The devices will be listed along with their corresponding device file names. These are files in /dev, such as ttyS0 and ttyS1, that we can later use as arguments for other commands, to specify which serial port should be interacted with. In addition, dmesg can indicate a technical problem if the device doesn’t show up at all.

Interacting with Serial Devices

Using standard utilities

Since serial ports are represented by files on Linux, standard utilities such as echo and cat can be used to interact with them. stty can be used to change the port’s settings. Let’s see how each of these applications is used.

With stty, you can set the port’s baud rate, amount of data bits, parity, handshake type (also known as flow control), and other parameters. This may be necessary to interact with a device or an application, since most of the time, the parameters have to match for the connection to work.

Use the following command to list your port’s current settings:

sudo stty -a -F <your port>
screen 2
And this is an example command that sets the ttyS0 port’s baud rate to 9600, data bits to 8, and enables RTS/CTS handshaking:

sudo stty -F /dev/ttyS0 9600 cs8 crtscts

With the proper settings selected, we can send data to the device with echo. It’s possible to send plain text to a serial port with echo, like this:

echo ‘TEXT’ > /dev/ttyS0

However, to do this, you need to log in as a superuser. This can be done with the “su -” command.

In our case, special ASCII symbols, such as ESC and Carriage Return, are needed to make full use of the connected printer. One way to do this is using echo’s -e argument and escape sequences. An escape sequence for ASCII characters is started with \x, followed by a hexadecimal number that corresponds to the character.
For example, this is how the word “TEXT” can be printed with escape sequences:

echo -e '\x54\x45\x58\x54'‘

We will send the following messages:

1B 40 (ESC @, Initialize)
1B 6D (ESC m, Full Cut)
4C 49 4E 45 20 31 ("LINE 1")
1B 21 80 (Enable Underline)
4C 49 4E 45 20 32 ("LINE 2")
0D 0A (CR FF, New Line)
1B 40 (Full Cut)


The -e argument is needed to allow the “\x” hexadecimal escape sequences. This is what it looks like in the terminal, and on the resulting print:
screen 3
While echo can be used to send data over a serial connection, cat can be used to receive it. In our case, the printer will respond to standard ESC/POS requests for status and ID information. This means that we'll need to catch the responses. First, we will launch cat, listening to the ttyS0 device and redirecting the output to a text file:

cat /dev/ttyS0 > examplefile

The application must keep running to receive data, so a new Terminal window or tab needs to be opened to send requests to our device. We will use echo to send the following:

1D 49 31 (Request model ID)
1D 49 32 (Request type ID)
Since the responses will be provided in hexadecimal format, direct cat output, which was recorded to “examplefile”, will not be human-readable. Instead, we will run the file through hexdump:
screen 4

Using gtkterm

Linux also provides serial terminals like minicom and gtkterm that can comprehensively interact with serial ports, handling both input and output, providing more formatting options, and making things easier overall.

As an example, we’ll try gtkterm. Similarly to stty, gtkterm needs to be launched with command line arguments to specify the connection settings, if the defaults are not suitable.
screen 5
Some useful options include local echo (to duplicate the data you send on your side), logging, hexadecimal view, and sending hex data. Also, note the indicators in the bottom right - they show the states of DTR, RTS, CD, DSR, and RI signal lines - it's another way to see that the device is connected and prepared to receive data.
screen 6

Connecting to a Serial Port on Your Local Network

Do you have a serial device that needs to be shared between multiple computers? Or to establish a serial connection between two remote pieces of software? Serial to Ethernet Connector for Linux is an easy solution. In addition to connecting physical ports, the application can create virtual ports that can be accessed by applications.

Easy-to-use and brimming with options, Serial to Ethernet Connector can be set up in minutes. Detailed documentation, user guides, and customer support experts will be there to help. Furthermore, there is a 15-day Free Trial available, so you risk nothing by giving our solution a try.

How to Use Serial to Ethernet Connector

1
Sign up for a free trial at this page. Wait for the activation key to arrive to your mailbox.
Sign up for a free trial
2
Head to the Download Center. Choose the right installer for your Linux distribution and architecture.
Head to the Download Center
3
Open your Terminal, navigate to the file, and use the following commands on Debian-based systems (will need superuser access):

dpkg -i serial_ethernet_connector_64.deb
apt-get install -f


Or this command on Fedora or CentOS:

sudo rpm -i serial_ethernet_connector_64.rpm

Once the installation is finished, restart your computer.
4
Use your activation code by running the following command:

evesecli register <your activation code>

Activation can be verified by running:

evesecli license

These installation and activation steps will need to be completed on both of the machines that will be connected.
Use your activation code
5
Since we will be sending messages between two applications, we will create virtual ports for both the server and the client. Start by using the following command on the server:

evesecli add -v /tmp/ttyLS0 server --local 5000

The “start” command will open the connection:

evesecli start /tmp/ttyLS0

In addition, you will need to know your computer’s IP address on the local network. Run “hostname -I” (capital i).
create virtual ports for both the server and the client
6
On the client computer, run the following:

evesecli add -v /tmp/ttyRS0 client --remote <your IP address>:5000
evesecli start /tmp/ttyRS0


You can verify whether the connection was successful by using “evesecli list”. It should show that there is 1 connection.
verify whether the connection was successful
7
Finally, since the connected ports are both virtual, they can be tested, as previously, with cat/echo.
since the connected ports are both virtual, they can be tested
Don’t forget that you can use “man evesecli” to get more information about the available options and commands. Additionally, there is a user guide that can be consulted for examples and overall information about the use of Serial to Ethernet Connector.
Serial to Ethernet Connector
Access remote serial port over IP Network for Windows
14-day Free Trial available
License price starts at $259.95
Available for