The basic process of setting serial port properties in Linux system

  
                

The serial port settings under Linux include the baud rate, the number of data bits, the number of stop bits, etc. The serial port settings are mainly to set the struct termios structure body member values. The following small series will give you a detailed introduction to the serial port settings under Linux. Let's go.

substantially common user data communication can be divided into serial communication and parallel communication.

Parallel communication refers to the simultaneous transmission of each bit of a data by using multiple data transmission lines. It is characterized by fast transmission speed and is suitable for short-distance communication, but requires applications with high transmission speed.

Serial communication refers to the transmission of one bit of data in a sequence using a transmission line. The utility model has the advantages that the communication line is simple, the communication can be realized by using a simple cable, the cost is reduced, and the application is suitable for long-distance communication, but the transmission speed is slow. The commonly used serial port has an RS-232-C interface (full name is "the serial binary data exchange interface technology standard between data terminal equipment (DTE) and data communication equipment (DCE)").

UART Controller: Can work in Interrupt mode or DMA (Direct Memory Access) mode. According to the 16-byte FIFO (first-in, first-out register), the maximum baud rate can be up to 230. 4Kbps.

UART operation: data transmission, data reception, interrupt generation, baud rate generation, loopback mode, infrared mode and automatic flow control mode.

The serial port settings include: baud rate, number of start bits, number of data bits, number of stop bits, and flow control protocol. Here, the baud rate is 115200, the start bit is 1b, the data bit 8b, the stop bit 1b, and the no-flow control protocol.

The serial port 1 and serial port 2 corresponding device names are “/dev/ttyS0”, “/dev/ttyS1”.

The reading and writing of the serial port under Linux can be done by using the simple "read" protocol and the "write" function. The difference is that other parameters of the serial port need to be set.

6.4.2 Serial Port Settings Details

The serial port settings are mainly to set the struct termios structure member values:

#include“termios.h”

Struct Termio

{

unsigned short c_iflag; /*Input mode flag*/

unsigned short c_oflag; /*output mode flag*/

unsigned short C_cflag; /* control mode flag */

unsigned short c_lfag; /*local mode flag*/

unsigned short c_line; /*line discipline*/

unsigned short C_cc[NCC]; /*control characters*/

};

By assigning c_cflag, you can set the baud rate, character size, data bits, stop bits, parity bits. And hardware flow control.

Set the basic flow of serial port properties:

1. Save the original serial port configuration

For the sake of security and convenience of debugging the program, you can save the original serial port configuration first, use the function tcgetattr (fd,&oldtio). This function gets the relevant parameters of the object pointed to by fd and saves them in the termios structure referenced by lodtio. This function can test whether the configuration is correct, whether the serial port is available, and so on. The debugging succeeds, the function returns 0, the failure, the function returns -1.

if(tcgetattr(fd,&oldtio)!=0)

{

perror(&ldquo ;SetupSerial 1”);

return -1;

}

2. The activation options are CLOCAL and CREAD

CLOCAL and CREAD are used locally Connect and accept enable, both options are activated by a bit mask.

Newtio.c_cflag

Copyright © Windows knowledge All Rights Reserved