serial Port configuration process
1, save the original serial port information, using tcgetattr () function;
struct Termios newtio, oldtio;tcgetattr (FD, &oldtio);
2, activation options are clocal and cread, for local connection and receive enable;
Newtio.c_cflag |= clocal | Cread;
3, set the baud rate, using the function cfsetispeed () and Cfsetospeed ();
Cfsetispeed (&newtio, B115200); Cfsetospeed (&newtio, B115200);
4, set the data bits, you need to use mask settings.
Newtio.c_cflag &= ~csize;newtio.c_cflag |= CS8
5, set parity bit, use C_cflag and C_iflag.
Parity check: Newtio.c_cflag |= parenb;newtio.c_cflag |= parodd;newtio.c_iflag |= (INPCK | Isrip); Parity check: Newtio.c_iflag |= (INPCK | Isrip); Newtio.c_cflag |= parenb;newtio.c_cflag &= ~parodd; no calibration: Newtio.c_cflag &= ~PARENB;
6, set the stop bit, by activating C_cflag in the CSTOPB implementation. If the stop bit is 1, the CSTOPB is cleared, and if the stop bit is 2, the CSTOPB is activated.
Newtio.c_cflag &= ~CSTOPB;
7, set the minimum character and wait time, for the receiving character and wait time without special requirements, can be set to 0;
Newtio.c_cc[vtime] = 0;newtio.c_cc[vmin] = 0;
8. Handle the Reference object to be written
The Tcflush function refreshes the input cache (the terminal driver has received but the user program has not yet been read) or the output cache (the user program has been written but not yet sent) int tcflush (int fileds, int queue); The queue number is one of the following 3 constants: Tciflush: Refresh input queue Tcoflush: Refresh output queue Tcioflush: Refresh input/output queue
9, activate the configuration, after the configuration is completed, you need to activate the configuration to take effect. Use Tcsetattr ();
Serial Port configuration Process