Linux: Serial Communication

Source: Internet
Author: User
Tags vmin

I used to develop VxWorks in the past. The main communication mode is the serial port. Because the underlying BSP package is ready, the serial port communication is very simple. Later, when I came into contact with Linux and ran LINUX serial port communication on ok6410, I found that the original naive thought that the simple serial port was not so simple.

# Include <termios. h>

1. Serial Port Operations

1.1 open: FD = open ("/dev/ttysac1", o_rdwr | o_noctty | o_ndelay );
O_rdwr read/write mode enabled;
O_noctty does not allow the process to manage the serial port (not easy to understand, generally choose );
O_ndelay is non-blocking (the default value is blocking. You can use fcntl () to re-set it after enabling it)

1.2 write: N = write (FD, "Linux", 5 );
N actual number of bytes written;

1.3 read: res = read (FD, Buf, Len );
Number of bytes read by Res;

1.4 settings: fcntl (FD, f_setfl, fndelay); // non-blocking
Fcntl (FD, f_setfl, 0); // Blocking

1.5 close: Close (FD );

2. Serial Port Configuration

Struct termios options; // Serial Port Configuration Structure
Tcgetattr (FD, & options); // get the current settings
Bzero (& options, sizeof (options ));
Options. c_cflag | = b115200 | clocal | cread; // set the baud rate, connect locally, and enable receiving
Options. c_cflag & = ~ Csize; // mask the data bit
Options. c_cflag | = cs8; // The data bit is 8, and cs7 for 7
Options. c_cflag & = ~ Cstopb; // One Stop bit. The two stop bits are | = cstopb
Options. c_cflag & = ~ Parenb; // no verification
// Options. c_cflag | = parenb; // check
// Options. c_cflag & = ~ Parodd // parity check
// Options. c_cflag | = parodd // odd check
Options. c_cc [vtime] = 0; // wait time, in milliseconds (read ). Detailed instructions
Options. c_cc [Vmin] = 0; // minimum number of bytes (read ). Detailed instructions
Tcflush (FD, tcioflush); // tciflush clears the input queue.
Tcoflush clears the output queue.
Tcioflush clears input and output queues.
Tcsetattr (FD, tcsanow, & options); // tcsanow takes effect immediately;
Tcsadrain: wait until everything has been transmitted;
Tcsaflush: flush input and output buffers and make the change

3. vtime and Vmin

Vtime defines the value of zero to several hundred milliseconds to wait (usually an 8-bit unsigned char variable ).
Vmin defines the minimum number of bytes to wait, which may be 0.
These two parameters are valid only when they are set as blocking. They are only applicable to read operations.
It is complicated to say. For example, set it to blocking and write operations are not tested. Here we only discuss read operations,
Read (FD, & Buf, 8); // read the serial port

3.1
Options. c_cc [vtime] = 0;
Options. c_cc [Vmin] = 0;
Vmin = 0. When the number of bytes in the buffer zone is greater than or equal to 0, the read serial port is not blocked because the conditions are always met.

3.2
Options. c_cc [vtime] = 0;
Options. c_cc [Vmin] = 1;
Vmin = 1. When the number of bytes in the buffer zone is greater than or equal to 1, the read operation is blocked when no data is available.

3.3
Options. c_cc [vtime] = 0;
Options. c_cc [Vmin] = 4;
Vmin = 4. When the number of buffer bytes is greater than or equal to 4, read operations are performed. Otherwise, the serial read operation is blocked. The maximum number of bytes read each time is determined by the third parameter in the READ function. Until the remaining data in the buffer <read the third parameter and <4 (if the third parameter of read is 1, perform 4 read operations until the buffer is read. For example, if the third parameter of read is 2, consecutive read operations until the buffer zone is empty or one character is left ). Vtime is not set, and the remaining characters do not have a fixed term until the next time the read conditions are met.

---------------------------------- Consider vtime -----------------------------

3.4
Options. c_cc [vtime] = 10; // unit: Hundred milliseconds
Options. c_cc [Vmin] = 4;
The difference of 3.3 is that the remaining data in the read buffer will be read in 1 second (10 hundred milliseconds) if the conditions are not met. In addition, when vtime is set, if the third read parameter is smaller than Vmin, Vmin is changed to the third read parameter, that is, read (FD, & Buf, 2). The preceding settings are changed:
Options. c_cc [vtime] = 10;
Options. c_cc [Vmin] = 2;

 

 

Related Article

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.