Serial Port configuration Process

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.