Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 25, 2025
In the realm of Linux administration, serial ports, also referred to as COM ports, serve as a vital link between the system and various devices.
From communicating with embedded systems to programming microcontrollers, a solid understanding of serial port configuration is often important.
In this tutorial, we’ll walk through the process of configuring a serial port on a Linux system, ensuring a seamless and error-free experience. Furthermore, we’ll also delve into additional options and considerations. Thus, even individuals new to the subject should be able to easily follow the instructions and grasp the concepts right from the beginning.
Unlike more contemporary interfaces, serial ports transmit data one bit at a time, sequentially over a single wire or circuit.
In particular, the importance of serial ports is highlighted in various aspects:
Understanding how to configure and harness the capabilities of serial ports is important for us, who are involved in hardware interfacing, programming, or working with interconnected systems.
In this section, we’ll identify the available serial ports we have in our system. Moreover, we’ll learn how to configure serial ports.
To begin with, we start by identifying the available serial ports on our system.
To that end, the dmesg command remains a valuable tool for the purpose as it displays kernel messages, providing information about hardware, drivers, and other system-related events:
$ dmesg | grep ttyS
[ 0.789456] serial8250: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
[ 1.234567] serial8250: ttyS1 at I/O 0x2f8 (irq = 3, base_baud = 115200) is a 16550A
Here, we pipe the output of dmesg to grep. grep tty filters the output of the dmesg command, showing only the lines that contain the term ttyS, which typically indicates a serial port.
Finally, the output informs us of two serial ports, ttyS0 and ttyS1, along with their corresponding I/O addresses and interrupt request values.
Once we identify the serial ports, we can inspect their current configurations using the stty command:
$ stty -F /dev/ttyS0 -a
speed 115200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
...
In the above snippet, we utilized the stty command to change and print terminal line settings. Moreover, we used the -F option for the target serial port. In this case, the target port is /dev/ttyS0. Consequently, the -a option provides a full display of all the current settings.
From the output, we observe various details:
Now that we checked the serial port’s configuration, we’ll apply some changes.
In this section, we’ll see what options we can utilize with the ssty command to customize our configuration and tailor it to our needs and environment.
To adjust the baud rate of a serial port, ensuring compatibility with connected devices, we use the stty command:
$ stty -F /dev/ttyS0 9600
$ stty -F /dev/ttyS0 -a
speed 9600 baud; rows 0; columns 0; line = 0;
From the output above, we conclude that we changed the baud rate or the operating speed of the serial port from 115200 to 9600 bits per second.
In real situations, a value of 115200 is considered very good so we shouldn’t have to lower our operating speed. However, this is just an example showing how to set our baud rate to our preferred value depending on our architecture and environment.
In addition, some compatibility options might necessitate a lower speed.
In serial communication, data bits, stop bits, and parity are widely used parameters defining how information is formatted and transmitted between devices:
Let’s check out an example of how to configure these parameters using the stty command:
$ stty -F /dev/ttyS0 cs8 -cstopb -parenb
In the above example we configured the data bit, stop bit, and parity respectively:
Understanding and appropriately configuring these parameters enables us to establish accurate communication between devices over serial ports, ensuring compatibility and data integrity in serial transmission.
There are other handy options we can use in combination with the stty command to craft its output based on our needs.
Let’s check out two of them:
In the context of the stty command, opost and onlcr are options that deal with the output processing of characters.
When we enable opost, it instructs the system to perform output post-processing on characters. This is before sending them to the terminal or device. On the other hand, enabling onlcr ensures that line breaks are formatted with both carriage return and newline characters. In essence, this option is particularly focused on the newline character and how it’s presented in the output.
Let’s check how to enable each of them:
$ stty -F /dev/ttyS0 opost
$ stty -F /dev/ttyS0 onlcr
Next, let’s test the effect of enabling both options:
$ echo -e "This is the first line\nThis is the second line\nThis is the third line" > /dev/ttyS0
Here is the first line
This is the second line
This is third line
In the above snippet, we used the echo command to print a statement and redirect it using the > operator to our serial device. As a result of enabling opost and onlcr, we can see an extra carriage return (\r) added when a newline (\n) byte is output.
In any case, we can enable and disable the options based on our needs.
In this article, we’ve discussed the identification of serial ports, checking their configurations, and making necessary adjustments.
Finally, we understood how to adapt these configurations based on the specific requirements of our projects and devices by changing specific parameters.