Hello everyone, I am the Intelligence Guy~
If you have worked on high-speed serial communication projects, you must have encountered the issue where traditional <span>termios</span> cannot configure high baud rates for serial communication parameters. So how can we solve this problem?
In Linux systems, <span>struct termios2</span> is a data structure used for advanced serial communication control. It is an extended version of the traditional <span>termios</span> structure, designed to support non-standard baud rates and more precise terminal attribute control, which can solve this problem. Below is a detailed introduction:
1. Core Functions
-
Support for Non-Standard Baud Rates (Arbitrary Baud Rates)
- The traditional
<span>termios</span>can only set standard baud rates (such as 9600, 115200, etc.), while<span>termios2</span>allows setting any value for baud rates (such as 250000, 3Mbps, etc.). - This is achieved by directly manipulating the
<span>c_ispeed</span>(input speed) and<span>c_ospeed</span>(output speed) fields.
Extended Control Capabilities add features on top of <span>termios</span>, such as more refined flow control, timeout control, and other advanced serial communication features.
2. Key Fields (Comparison with <span><span>termios</span></span>)
#include <asm/termbits.h> // Header file location
struct termios2 {
tcflag_t c_iflag; // Input mode flags
tcflag_t c_oflag; // Output mode flags
tcflag_t c_cflag; // Control mode flags
tcflag_t c_lflag; // Local mode flags
cc_t c_line; // Line discipline
cc_t c_cc[NCCS]; // Control character array
speed_t c_ispeed; // Input baud rate (non-standard value)
speed_t c_ospeed; // Output baud rate (non-standard value)
};
- Core Extension:
<span>c_ispeed</span>and<span>c_ospeed</span>directly store the actual baud rate values (such as<span>250000</span>), rather than traditional predefined constants (such as<span>B115200</span>).
3. Use Cases
-
High-Speed/Non-Standard Serial Devices such as industrial equipment, custom hardware (Arduino/embedded development), drone communication, special sensors, etc., which may require precise non-standard baud rates.
-
Scenarios Requiring Precise Timing Control such as communication protocols that require precise timing (e.g., high-speed variants of DMX512, Modbus).
4. Operation Method (via <span><span>ioctl</span></span>)
It is necessary to use the <span>ioctl()</span> system call in conjunction with special commands to operate on <span>termios2</span>:
| Operation | <span>ioctl</span> Command |
Description |
|---|---|---|
| Read Current Settings | <span>TCGETS2</span> |
Get current <span>termios2</span> configuration |
| Apply Settings Immediately | <span>TCSETS2</span> |
Effective unconditionally |
| Effective After Output Completion | <span>TCSETSW2</span> |
Effective after flushing the output buffer |
| Effective After Input/Output Completion | <span>TCSETSF2</span> |
Effective after flushing the input/output buffer |
5. Code Example (Setting Non-Standard Baud Rate)
#include <fcntl.h>
#include <asm/termbits.h>
#include <sys/ioctl.h>
int main() {
int fd = open("/dev/ttyS0", O_RDWR); // Open serial device
struct termios2 tio;
ioctl(fd, TCGETS2, &tio); // Read current configuration
// Set non-standard baud rate 250000
tio.c_cflag &= ~CBAUD; // Clear standard baud rate flags
tio.c_cflag |= BOTHER; // Enable non-standard baud rate mode
tio.c_ispeed = 250000; // Input baud rate
tio.c_ospeed = 250000; // Output baud rate
// Other configurations (8N1)
tio.c_cflag &= ~PARENB; // No parity
tio.c_cflag &= ~CSTOPB; // 1 stop bit
tio.c_cflag |= CS8; // 8 data bits
ioctl(fd, TCSETS2, &tio); // Apply configuration
close(fd);
return 0;
}
6. Notes
- Linux Specific
<span>struct termios2</span>is an extension specific to Linux and is not part of the POSIX standard. - Kernel Support requires kernel version ≥ 2.6.31 (some features depend on driver support).
- Header File Path is usually located in
<span><asm/termbits.h></span>or<span><sys/ioctl.h></span>.

END
Author: Mr. Deng
Source: Embedded Intelligence BureauCopyright belongs to the original author. If there is any infringement, please contact for removal..▍Recommended ReadingHow much does a genuine Keil cost?Microcontrollers can output PWM like thisSharing a powerful tool for embedded development debugging!→ Follow for more updates ←