Home  >  Article  >  Operation and Maintenance  >  Configuring Linux systems to support serial communication programming

Configuring Linux systems to support serial communication programming

PHPz
PHPzOriginal
2023-07-04 11:42:142829browse

Configuring the Linux system to support serial communication programming

Serial communication is a common hardware communication method used for data transmission between the computer and external devices. In the Linux system, we can implement support for the serial port through configuration, and then perform serial communication programming. This article will introduce how to configure the serial port in a Linux system and provide relevant code examples.

1. Check the serial port device

In the Linux system, the serial port device is called a TTY device. We can use the terminal command ls /dev/ttyS* to view the serial devices existing in the system. Usually, if there is a serial port device in the system, output similar to /dev/ttyS0 or /dev/ttyS1 will be displayed. Among them, /dev/ttyS0 represents the first serial port device, /dev/ttyS1 represents the second serial port device, and so on.

2. Configure serial port parameters

Before programming serial communication, we need to configure the parameters of the serial port, including baud rate, data bits, check bits, stop bits, etc. You can configure the serial port parameters through the terminal command stty. The following is an example command:

stty -F /dev/ttyS0 9600 cs8 -cstopb -parenb

In the above command, "-F /dev/ttyS0" specifies the serial port device to be configured as /dev/ttyS0, 9600 is the specified baud rate, cs8 means the data bit is 8 bits, -cstopb means the stop bit is 1 bit, -parenb means no parity check test. If necessary, these parameters can be adjusted according to the actual situation.

3. Open the serial port device

Before performing serial communication programming, you need to open the serial port device to operate. You can use the open() function to open the serial device. The following is a simple code example:

#include<unistd.h>
#include<fcntl.h>
#include<errno.h>

int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);  
if (fd == -1) {
    perror("打开串口设备失败");
    return -1;
}

In the above code, the open() function is opened by passing in the serial device path /dev/ttyS0 and some flags Serial device. O_RDWR means opening the device in a read-write mode, O_NOCTTY means not using the open serial port as a control terminal, O_NONBLOCK means opening the device in a non-blocking way. After successful opening, a file descriptor fd will be returned for subsequent use.

4. Set serial port parameters

After opening the serial port device, we need to use the tcgetattr() function to obtain the original parameters of the serial port, and then modify these parameters to perform the serial port Configuration of parameters. The following is a simple code example:

#include<termios.h>

struct termios options;
tcgetattr(fd, &options);

cfsetispeed(&options, B9600);   // 设置输入波特率为9600
cfsetospeed(&options, B9600);   // 设置输出波特率为9600
options.c_cflag |= CS8 | CLOCAL | CREAD;  // 设置数据位为8位,并开启本地连接和接收使能
options.c_cflag &= ~PARENB;    // 关闭奇偶校验
options.c_cflag &= ~CSTOPB;    // 设置停止位为1位

tcsetattr(fd, TCSANOW, &options);

In the above code, the tcgetattr() function is used to obtain the original parameters of the serial port and store them in struct termios Structure variable options. Then, set the input and output baud rate to 9600 through the cfsetispeed() and cfsetospeed() functions, and then set parameters such as data bits, parity and stop bits through bit operations. Finally, use the tcsetattr() function to write the modified parameters back to the serial port.

5. Serial communication

After configuring the serial port parameters, we can use the read() function to read data from the serial port and use write() Function writes data to the serial port. The following is a simple code example for receiving serial port data:

char buffer[255];
int bytes_read = read(fd, buffer, sizeof(buffer));
if (bytes_read == -1) {
    perror("读取串口数据失败");
    return -1;
}

printf("接收到的数据:%s
", buffer);

In the above code, we first define a buffer buffer to store the received data. Then, use the read() function to read data from the serial port and store the read data in the buffer. Next, use the printf() function to print out the received data.

6. Close the serial port device

After the program ends, we need to close the open serial port device. You can use the close() function to close the serial port device, as shown below:

close(fd);

The above code will close the previously opened serial port device and release related resources.

Through the above configuration and code examples, we can implement serial communication programming in the Linux system. Of course, more situations need to be considered in actual applications, such as exception handling, buffer management, etc. Hope this article can provide you with some help.

The above is the detailed content of Configuring Linux systems to support serial communication programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn