1. Overview

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.

2. Why Use Serial Ports?

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:

  • versatility and reliability: serial ports are preferred for connecting a diverse range of devices such as embedded systems, microcontrollers, networking equipment, and various peripherals due to their versatility and reliability
  • seamless data exchange: serial ports enable the seamless exchange of data between connected devices, facilitating tasks like programming, configuration, and communication
  • enduring relevance: despite technological advancements in connectivity, the enduring relevance of serial ports underscores their significance in ensuring robust and efficient communication across a multitude of computing and electronics applications

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.

3. Check Serial Port Configuration

In this section, we’ll identify the available serial ports we have in our system. Moreover, we’ll learn how to configure serial ports.

3.1. Identify Existing 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.

3.2. View Configuration

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:

  • baud rate or the defined speed of data transmission in bits per second (bps)
  • interrupt character (intr)
  • other configuration parameters

Now that we checked the serial port’s configuration, we’ll apply some changes.

4. Change Serial Port Options

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.

4.1. Baud Rate

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.

4.2. Data Bits, Stop Bits and Parity

In serial communication, data bits, stop bits, and parity are widely used parameters defining how information is formatted and transmitted between devices:

  • data bits determine the number of bits we use to encode each character, typically set to 5, 6, 7, or 8 bits
  • stop bits indicate the end of a data frame, commonly configured as one or two stop bits
  • parity, an error-checking mechanism, ensuring data integrity

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:

  • cs8: sets the character size to 8 bits
  • cstopb: configures for one stop bit
  • parenb: disables parity

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.

4.3. Output Crafting

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:

  • opost: output post-processing
  • onlcr: output new line carriage return

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.

5. Conclusion

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.