选择API以9600波特率超时

时间:2019-02-02 08:29:44

标签: linux select serial-port intel uart

我正在从串行/ UART读取数据,波特率为9600(我的115200波特率工作正常)

我写了下面的代码,但是select API每次都给出超时,它需要超过2秒的超时,这是不希望的。因为我有2048个字节的数据,所以在100毫秒内我应该可以获取一些数据,但是我认为即使串行的Rx缓冲区有一些数据要处理,select API也不接收中断。

注意:相同的代码在beaglebone black上运行正常,而在intel atom-3900 /内核上运行的不是Linux-intel 4.9。

代码:

int main(void)
{
  int   ret;
  char  buf[1280];
  fd_set m_Inputs;  // fd_set for the select call for com input
  int   m_maxFD;   // max FD for select() call.

  struct timeval tv;
  /*Open Serial port in non blocking mode*/
  int fd1;
  fd1 = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NONBLOCK);

  /* get the termios structure */
  struct termios options;

  tcgetattr(fd1, &options);  // Get the current options for the port...

  // Set the baud rates...
  cfsetispeed(&options, B9600);
  cfsetospeed(&options, B9600);

  // Enable the receiver and set local mode...
  options.c_cflag |= (CLOCAL | CREAD | CS8);
  options.c_cflag &= ~PARENB;  // ignore parity
  options.c_cflag &= ~CSTOPB;  // 1 stop bit (2 if set)
  options.c_cflag &= ~CSIZE;   // clear the size bits
  options.c_cflag &= ~CRTSCTS; //No hard flow control
  options.c_cflag &= ~HUPCL;   //Hang Up on last Close
  options.c_cflag |= CS8;      // reset the size to 8 bits / char
  options.c_cc[VMIN]=1;
  options.c_cc[VTIME] = 1;
  options.c_oflag = 0;
  options.c_lflag = 0;       //ICANON;
  // Set the new options for the port...
  tcsetattr(fd1, TCSANOW, &options);

  cout << endl << "FD1 = " << fd1 << endl;

  while (1)
  {

        fd_set rfds;  // fd_set for the select call for com input

        FD_ZERO(&rfds);
        FD_SET(fd1, &rfds);

        tv.tv_sec = 0;
        tv.tv_usec = 100000;

        ret = select(fd1 + 1, &rfds, NULL, NULL, &tv);

        if (ret > 0)
        {
          ret = read(fd1, buf, 127);
          buf[ret] = '\0';
          cout << buf;

        }else{

            cout << "Time OUT" << endl;
            break;
        }
    }

  return 0;

}

0 个答案:

没有答案