termios VMIN VTIME and blocking/non-blocking read operations

后端 未结 3 1698
眼角桃花
眼角桃花 2021-01-12 18:29

I am trying to write a simple C serial communication program for Linux. I am confused about the blocking/non-blocking reads and VMIN/VTIME relationships.

My question

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-12 19:14

    I'd recommend using vmin and vtime both 0 if you're using nonblocking reads. That will give you the behavior that if data is available, then it will be returned; the fd will be ready for select, poll, etc whenever data is available.

    vmin and vtime are useful if you're doing blocking reads. For example if you're expecting a particular packet size then you could set vmin. If you want to make sure you get data every half second you could set vtime.

    Obviously vmin and vtime are only for non-canonical mode (non-line mode)

    My suspicion is that in nonblocking mode if you set vmin say to 5, then the fd will not be read-ready and read will return EWOULDBLOCK until 5 characters are ready. I don't know and don't have an easy test case to try, because all the serial work I've done has been either blocking or has set both to 0.

提交回复
热议问题