Clearing the serial port's buffer

后端 未结 3 601
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 07:31

This is what my function looks like to open the serial port (using Ubuntu 12.04):

int open_port(void)
{
  int fd; /* File descriptor for the port */

  fd =          


        
3条回答
  •  粉色の甜心
    2020-12-13 07:46

    I have been experiencing similar symptoms with an Arduino Uno board that resets on open(). I was receiving data after the open() call that was generated before the Arduino board had been reset and thus before open() had been called.

    Tracking down the issue with ioctl() calls I learned that the data had simply not yet arrived in the input buffer by the time tcflush() was called. So the tcflush() did work but there was no data to flush. A sleep of 1000 us after the open() call seemed to solve the issue. This is becasue the delay allowed the data to arrive before tcflush() was called and therefore tcflush() did indeed flush the input buffer.

    You might be experiencing the same issue.

提交回复
热议问题