1、关于VTIME和VMIN

注意:控制符VTIME和VMIN之间有复杂的关系。VTIME定义要求等待的时间(百毫米,通常是unsigned char变量),而VMIN定义了要求等待的最小字节数(相比之下,read函数的第三个参数指定了要求读的最大字节数)。
如果VTIME=0,VMIN=要求等待读取的最小字节数,read必须在读取了VMIN个字节的数据或者收到一个信号才会返回。
如果VTIME=时间量,VMIN=0,不管能否读取到数据,read也要等待VTIME的时间量。
如果VTIME=时间量,VMIN=要求等待读取的最小字节数,那么将从read读取第一个字节的数据时开始计时,并会在读取到VMIN个字节或者VTIME时间后返回。
如果VTIME=0,VMIN=0,不管能否读取到数据,read都会立即返回。
参考:https://blog.csdn.net/wang_shuai_ww/article/details/54286046

2、清除串口接收缓冲区

  1. //清除串口接收缓冲区数据
  2. tcgetattr(portfd, &tios);
  3. tcflush(portfd, TCIFLUSH);
  4. tcsetattr(portfd, TCSANOW, &tios);

tcflush(int fd, int queue_selector) //对以写但未发出,或已接收但未读数据的flush.
discards data written to the object referred to by fd but not transmitted, or data received but not read, depending on the value of
queue_selector: TCIFLUSH
flushes data received but not read. TCOFLUSH
flushes data written but not transmitted.

   TCIOFLUSH<br />              flushes both data received but not read, and data written but not transmitted.<br />              <br />tcgetattr(int fd, struct termios *termios_p)   //get串口termios结构<br />gets the parameters associated with the object referred by fd and stores them in the termios structure referenced by termios_p. This function may be invoked from a background process; however, the terminal attributes may be subsequently changed by a foreground process.

tcsetattr(int fd, int optional_actions, const struct termios *termios_p) //设置串口termios结构
sets the parameters associated with the terminal (unless support is required from the underlying hardware that is not available) from the termios structure referred to by termios_p. optional_actions specifies when the changes take effect:
TCSANOW
the change occurs immediately.

TCSADRAIN
the change occurs after all output written to fd has been transmitted. This function should be used when changing parameters that affect output.

TCSAFLUSH
the change occurs after all output written to the object referred by fd has been transmitted, and all input that has been received but not read will be discarded before the change is made.