Linux serial port blocking and non-blocking

In the serial port setting, there are the following two parameters to decide whether to block or not.

  • O_NONBLOCK
  • O_NDELAY

The result of O_NONBLOCK and O_NDELAY is to make the I/O become non-blocking mode, and it will return immediately when no data can be read or the write buffer is full, and the program action will not be put on hold until There is data or writing is complete.

The difference between them is that setting O_NDELAY will cause the I/O function to return 0 immediately, but a problem arises, because when the end of the file is read, the return is also 0, so it is impossible to know what the situation is; therefore, O_NONBLOCK is generated, it will return -1 when no data can be read, and set errno to EAGAIN.

However, it should be noted that in GNU C, O_NDELAY is only for compatibility with BSD programs. In fact, O_NONBLOCK is used as a macro definition, and O_NONBLOCK can be set at open in addition to being used in ioctl.
If O_NODELAY is not added when opening the serial port, the second method below can be used to set blocking/non-blocking

Something else to pay attention to

options.c_cc[VTIME] = X;   //设置从获取到1个字节后开始计时的超时时间

options.c_cc[VMIN] = Y;     //设置要求等待的最小字节数

Effects on the read() function in raw mode:

1、X=0,Y!=0。函数read()只有在读取了Y个字节的数据或者收到一个信号的时候才返回;

2、X!=0,Y=0。即使没有数据可以读取,read()函数等待X时间量后返回;

3、X!=0,Y!=0。第一个字节数据到时开始,最先满足收到Y个字节或达超时时间X任意一个条件,read()返回;

4、X=0,Y=0。即使读取不到任何数据,函数read也会立即返回。

c_cc[VTIME] The timeout time when reading in non-standard mode (unit: 100 milliseconds), it can be understood that it starts timing from the receipt of the last byte, and if it times out, exits READ

c_cc[VMIN] The minimum number of characters when reading in non-canonical mode. If set to 0, it is non-blocking. If it is set to other values, it will block until the corresponding data is read.

key code

iFd = open(cSerialName, O_RDWR | O_NOCTTY);

opt.c_cc[VMIN]   =   0;//DATA_LEN;                                     
opt.c_cc[VTIME]  =   30;//每个单位是0.1秒  20就是2秒

Through the above settings, if there is no data in len=read(fd, tmp,124);, it will exit READ after 3 seconds, but if the serial port has read data, it will return immediately

c_cc[VMIN], like a threshold, for example, set to 8. If only 3 data are received, then it will not return. Only after 8 data are collected, READ will return and it will be blocked there.

opt.c_cc[VMIN]   =   8;//DATA_LEN;                                     
opt.c_cc[VTIME]  =   30;//每个单位是0.1秒  20就是2秒

If it is set in this way, it will be completely blocked. Only when the serial port receives at least 8 data will it return to READ immediately, or if it is less than 8 data, it will also return after a timeout of 3 seconds.

Let's talk about read(fd, tmp, xx); if xx is set to 1, as long as the serial port has one data, even if opt.c_cc[VMIN]=8, it will return immediately, no need to make up 8bytes, if this The number is 1024. As long as the actual received data exceeds the value set by opt.c_cc[VMIN], it will return immediately instead of 1024. I personally feel that it is better to set this value to 1, which is used for blocking mode reading. Take, if non-blocking, of course 0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324441170&siteId=291194637