简体   繁体   English

当端口上有字符时,select()调用未返回

[英]select() call not returning when chars on port

I have a select call that doesn't seem to detect characters on the serial port. 我有一个选择呼叫,它似乎无法检测到串行端口上的字符。 Is there anything I am missing? 我有什么想念的吗?

If I remove the read() code that is in the block above the select will return for a while until the existing port buffer is emptied and then nothing more is detected. 如果删除上面块中的read()代码,则select将返回一会儿,直到清空现有的端口缓冲区,然后再检测不到。

I am streaming characters to the port, and running minicom shows the continuous input stream on the port. 我正在向端口传输字符,运行minicom会在端口上显示连续的输入流。

Can anybody see anything wrong with this code? 有人可以看到这段代码有什么问题吗?

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;



  int fd1;
  fd1 = open("/dev/ttyXR6", O_RDWR | O_NOCTTY | O_NONBLOCK);

  fcntl(fd1, F_SETFL, 0);

  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);

  // test to make sure the characters are coming in on the port

  for (short i =0; i < 60; i++)
  {
    ret = read(fd1, buf, 32);
    buf[ret] = '\0';
    cout << buf;
    usleep(500);
  }

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

  FD_ZERO(&rfds);

  FD_SET(fd1, &rfds);


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

  while (1)
  {
    tv.tv_sec = 0;

    tv.tv_usec = 1000;

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


    if (ret > 0)

    {
      ret = read(fd1, buf, 127);
      buf[ret] = '\0';
      cout << buf;
    }
    usleep(500);
  }

  return 0;

}

Before it returns, select() modifies rfds to indicate which descriptors have data ready for reading. 在返回之前,select()修改rfds以指示哪些描述符具有准备读取的数据。

In your case, the first time it returns after the 1ms timeout is reached and no data is available on your descriptor, it will remove your descriptor from the rfds set to indicate that no data is available. 对于您而言,它是在1ms超时后第一次返回并且描述符上没有可用数据时,它将从rfds集中删除您的描述符,以指示没有可用数据。 Then, when you call select() again the next time through the loop, rfds is an empty set, and so after that it will not even bother to check your descriptor anymore. 然后,当您下次在循环中再次调用select()时,rfds是一个空集,因此在此之后,它甚至都不再需要检查描述符了。

You need to call FD_SET(fd1, &rfds) before select each time you go through the loop. 每次循环时,都需要先调用FD_SET(fd1,&rfds)才能进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回字符数组或指向char literal的指针时的区别 - Difference when returning an array of chars or a pointer to char literal 重载字符向量和字符串 c++ 时,调用成员 function 不明确 - Call to member function is ambigous when overloading both vector of chars and string c++ 通过使用std :: copy而不是std :: memcpy复制字符时避免了libc函数调用? - avoiding a libc function call when copying chars by using std::copy instead of std::memcpy? 调用 std::to_chars 的正确方法是什么? - What is the correct way to call std::to_chars? 串口打开返回EAGAIN - Serial port open returning EAGAIN QMetaObject :: invokeMethod()用于调用QListWidget :: addItem()时返回false - QMetaObject::invokeMethod() returning false when used to call QListWidget::addItem() 在C ++中返回结构的向量时,没有用于调用[class]的匹配函数 - no matching function for call to [class] when returning a vector of structures in c++ 从指针返回对象时发生意外的析构函数调用 - Unexpected destructor call when returning object from a pointer 为什么select()返回1但recv()返回0? - Why is select() returning 1 but recv() returning 0? 有没有办法获得套接字描述符,因为哪个select()调用返回EBADF? - Is there any way to get the socket descriptor due to which select() call is returning EBADF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM