Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 从套接字读取部分数据,但仅读取一次_Sockets - Fatal编程技术网

Sockets 从套接字读取部分数据,但仅读取一次

Sockets 从套接字读取部分数据,但仅读取一次,sockets,Sockets,我想从uClinux上的串行端口获取数据。工作原理:我有一个外围设备,我想进入引导加载程序模式。为此,我必须发送数据i2500$,这意味着它处于引导加载模式。不幸的是,我可以只读>i2,如果我使用我的方法,它将不再返回数据,或者如果我重置设备并重复开始跳转到引导加载程序 int TEnforaUpdate::ReadFromComport (unsigned long timeouta, unsigned long size) { FD_SET(fdCom, &read_fds);

我想从uClinux上的串行端口获取数据。工作原理:我有一个外围设备,我想进入引导加载程序模式。为此,我必须发送数据i2500$,这意味着它处于引导加载模式。不幸的是,我可以只读>i2,如果我使用我的方法,它将不再返回数据,或者如果我重置设备并重复开始跳转到引导加载程序
int TEnforaUpdate::ReadFromComport (unsigned long timeouta, unsigned long size)
{
  FD_SET(fdCom, &read_fds);
  int retValue = 0;
  // Set timeout to x microseconds
  struct timeval timeout;
  timeout.tv_sec = 0;
  timeout.tv_usec = 1000 * timeouta;
  // Wait for input to become ready or until the time out; the first parameter is
  // 1 more than the largest file descriptor in any of the sets
  if ((select (fdCom + 1, &read_fds, &write_fds, &except_fds, &timeout) == 1)
      && (FD_ISSET(fdCom,&read_fds)))
  {
    //read max
    retValue = read (fdCom, RxBuffer, RX_BUFFER_SIZE);
    printf ("Read %d bytes: ", retValue);
    int i;
    for (i = 0; i < retValue; i++)
      printf ("[%02x]", RxBuffer[i]);
    printf ("\n");   
  }
  else
    return 1;
  if (retValue > 2)
  {
    strcpy (answer, (char*) RxBuffer);
    //remove trashes from buffer
    memset (RxBuffer, 0x00, RX_BUFFER_SIZE);
    printf ("Comport answers: %s \n", answer);
  }
  FD_CLR(fdCom, &read_fds);
  tcflush (fdCom, TCIFLUSH);
  return retValue;
}

我只能读取>i2这意味着什么?我只能从串行端口读取响应>i2的一部分,但完整响应>i2500$,并且它是通过逻辑分析仪验证的。您假设read一次读取整个消息。Read可以读取任何数量,包括一个字节。我知道,我可以通过RX\u BUFFER\u SIZE=256定义它,但在读取数据后,我仍然无法读取其余的数据。例如,如果我在这里调用ReadFromComport,它将读取数据并等待5毫秒,然后再次在这里调用ReadFromComport,它将不读取数据。它不会在5毫秒后读取其余数据。我将以不同的方式解释,如果我的函数只能读取数据的开头部分,那么如何读取其余部分?我需要轮询机制或类似的东西。另一个论坛上有人建议,它可能与阻塞和非阻塞功能有关。您是否使用非阻塞套接字?使用阻塞IO或异步IO epoll。这样你根本不需要投票。非阻塞套接字通常不是一个好主意。