1. Overview

In computing, a serial port is a serial communication interface through which data can be transmitted or received one bit at a time. Baud rate, in this context, refers to the number of signal symbols transmitted per second.

In this tutorial, we’ll learn how to determine the baud rate of a serial port in Linux. This usually entails utilizing the stty or setserial commands.

2. Getting the Baud Rate With stty

The stty command is a tool for displaying and modifying terminal line characteristics like the baud rate.

To get the baud rate of a particular serial port, we can use it as input to stty. Let’s see how we can use stty to display the baud rate of a particular serial port:

$ stty < /dev/ttyS0 
speed 115200 baud; line = 0;
-brkint -imaxbel

As we can observe above, the baud rate of the serial port device /dev/ttyS0 is 115200.

We can also get the baud rate information by specifying a serial port with the -F option instead of redirecting the input:

$ stty -F /dev/ttyS0
speed 115200 baud; line = 0;
-brkint -imaxbel

Here, -brkint and -imaxbel are port-specific settings that aren’t related to the baud rate. Hence, we can specify the speed setting in the command to make stty only print the baud rate:

$ stty -F /dev/ttyS0 speed 
115200

As can be seen above, the output is now only the baud rate of 115200.

3. Using setserial to Determine the Baud Rate

In order to determine the baud rate, we can also use the setserial tool. This tool allows us to manage and manipulate serial port configurations including reporting information such as the baud base, hardware I/O port, and much more.

Let’s get the baud rate of the serial port device from before using setserial:

$ setserial -a /dev/ttyS0
/dev/ttyS0, Line 0, UART: 16550A, Port: 0x03f8, IRQ: 4
	Baud_base: 115200, close_delay: 50, divisor: 0
	closing_wait: 3000
	Flags: spd_normal skip_test

We can see that this output verifies the baud rate of /dev/ttyS0 is 115200, as we observed previously.

Above, we used the -a option to get more detailed data including the baud rate. We can also use the -G option to print the same information in a different form:

$ setserial -G /dev/ttyS0
/dev/ttyS0 uart 16550A port 0x03f8 irq 4 baud_base 115200 spd_normal skip_test

Additionally, this output form can be fed back to setserial as command-line arguments to configure the serial port. This option allows for a rudimentary backup-and-restore functionality.

Moreover, we can add the -g option to get the data of several ports at once:

$ setserial -Gg /dev/ttyS0 /dev/ttyS1 /dev/ttyS2 /dev/ttyS3
/dev/ttyS0 uart 16550A port 0x03f8 irq 4 baud_base 115200 spd_normal skip_test
/dev/ttyS1 uart 16550A port 0x02f8 irq 3 baud_base 115200 spd_normal skip_test
/dev/ttyS2 uart 16550A port 0x03e8 irq 4 baud_base 115200 spd_normal skip_test
/dev/ttyS3 uart 16550A port 0x02e8 irq 3 baud_base 115200 spd_normal skip_test

Above, we get the baud rates of /dev/ttyS0, /dev/ttyS1, /dev/ttyS2, and /dev/ttyS3 together.

4. Configuring the Baud Rate

In addition to getting the baud rate, we can also set it for a serial port with stty by directly passing the new value:

$ stty -F /dev/ttyS0 38400

After this command, the baud rate is updated to 38400. We can verify this new value:

$ stty -F /dev/ttyS0 speed
38400

Now, let’s change back the baud rate with setserial:

$ sudo setserial /dev/ttyS0 baud_base 115200

Notably, we need superuser privileges for setserial. Finally, we can check the updated baud rate:

$ setserial -G /dev/ttyS0
/dev/ttyS0 uart 16550A port 0x03f8 irq 4 baud_base 115200 spd_normal skip_test

The baud rate is now 115200, as expected.

5. Conclusion

In this article, we learned how to determine the baud rate of a serial port using stty and setserial. We observed that we can get the baud rate in several ways and in different forms. Also, we learned that we can use additional options to our advantage in order to leverage the abilities of advanced tools. Finally, we saw how to set the baud rate.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.