刷新排队的GPIB响应

时间:2012-07-20 14:14:36

标签: c++ linux embedded gpib

架构 - >来自外部接口的GBIP通过gpib总线连接到目标(linux)系统。 在Linux机箱内,有从GPIB到主板的以太网电缆。

外部接口上的PIC_GPIB卡是IEEE 488.2

我正在从外部接口向linux box发送查询。

几个场景

1)如果我发送一个不期望回复的查询,那么下一个查询发送将起作用。

2)如果我发送一个期望回复的查询,当我收到回复并阅读它然后触发下一个查询时,它可以正常工作。

3)但是如果我从外部接口发送查询并得到响应,我忽略了读取响应,那么Next查询失败。 我正在请求方案3的帮助。

编码在linux端完成,它是一个socket编程,它使用unistd.h中的linux内置函数进行读写。

我的调查:我发现外部接口上的gbib卡上有一个内部存储器,它存储了之前响应的值,直到我们读取为止。通常我使用IEEE字符串实用程序软件编写进入linux框的命令,并通过读取按钮读取reposne。

有人可以指导我如何清理存储值的输入缓冲区或内存,以便从外部命令写入而不必费心阅读它。

我在linux端的代码是用C ++和socket编程开发的。我用bulit写入和读取函数来写入和读取gpib和json服务器。

示例代码如下所示

    bool GpibClass::ReadWriteFromGPIB()
{
  bool check = true;
  int n = 0;
  char buffer[BUFFER_SIZE];
  fd_set read_set;
  struct timeval lTimeOut;

  // Reset the read mask for the select
  FD_ZERO(&read_set);
  FD_SET(mGpibFd, &read_set);
  FD_SET(mdiffFd, &read_set);

  // Set Timeout to check the status of the connection
  // when no data is being received
  lTimeOut.tv_sec = CONNECTION_STATUS_CHECK_TIMEOUT_SECONDS;
  lTimeOut.tv_usec = 0;

  cout << "Entered into this function" << endl;

  // Look for sockets with available data
  if (-1 == select(FD_SETSIZE, &read_set, NULL, NULL, &lTimeOut))
  {
    cout << "Select failed" << endl;

    // We don't know the cause of select's failure.
    // Close everything and start from scratch:
    CloseConnection(mGpibFd);
    CloseConnection(mdifferntServer); // this is different server

    check = false;
  }

  // Check if data is available from GPIB server,
  // and if any read and push it to gpib
  if(true == check)
  {
    cout << "Check data from GPIB after select" << endl;

    if (FD_ISSET(mGpibFd, &read_set))
    {
      n = read(mGpibFd, buffer, BUFFER_SIZE);

      cout << "Read from GPIB" << n << " bytes" << endl;

      if(0 < n)
      {
        // write it to different server and check if we get response from it
      }
      else
      {
        // Something failed on socket read - most likely
        // connection dropped. Close socket and retry later
        CloseConnection(mGpibFd);

        check = false;
      }
    }
  }

  // Check if data is available from different server,
  // and if any read and push it to gpib
  if(true == check)
  {
    cout << "Check data from diff server after select" << endl;

    if (FD_ISSET(mdiffFd, &read_set))
    {
      n = read(mdiffFd, buffer, BUFFER_SIZE);

      cout << "Read from diff servewr " << n << " bytes" << endl;

      if (0 < n)
      {
        // Append, just in case - makes sure data is sent.
        // Extra cr/lf shouldn't cause any problem if the json
        // server has already added them
        strcpy(buffer + n, "\r\n");

        write(mGpibFd, buffer, n + 2);
        std::cout <<" the buffer sixze  = " << buffer << std::endl;

      }
      else
      {
        // Something failed on socket read - most likely
        // connection dropped. Close socket and retry later
        CloseConnection(mdiffFd);

        check = false;
      }
    }
  }

  return check;
}

1 个答案:

答案 0 :(得分:0)

在任何可能产生反应的操作之后,您通常应该阅读回复。

如果你没有这样做,一个简单的解决方案就是在循环中读取响应,直到你把队列耗尽为止。

您可以重置乐器(可能是* RST),但您可能也会松开其他状态。您必须检查它的文档以查看是否存在仅重置响应队列的命令。检查文档总是一个好主意,因为精确符合规范的仪器数量与以独特方式增加或省略部件的数量相比相形见绌。