c++ - 刷新排队的 GPIB 响应

标签 c++ linux embedded gpib

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

外部接口(interface)上的PIC_GPIB卡是IEEE 488.2

我正在从外部接口(interface)向 Linux 盒子发送查询。

一些场景

1) 如果我发送的查询不希望得到响应,则下一个查询发送将起作用。

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

3)但是如果我从外部接口(interface)发送查询并收到响应,并且我忽略读取响应,则下一个查询将失败。 我请求针对场景 3 的帮助。

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

我的调查:我发现外部接口(interface)上的 GBIB 卡上有一个内部存储器,它存储先前响应的值,直到我们读取为止。一般来说,我使用 IEEE 字符串实用软件来编写进入 linux box 的命令并通过 read 按钮读取 reposne 。

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

我在linux端的代码是用C++和socket编程开发的。我已经使用bulit write和read函数来写入和读取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;
}

最佳答案

您通常应该在任何可能生成响应的操作之后读取响应。

如果您无法做到这一点,一个简单的解决方案是循环读取响应,直到将队列排空。

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

关于c++ - 刷新排队的 GPIB 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11581160/

相关文章:

Linux 文件默认权限

javascript - 可以使用 JQuery/JavaScript 编写大量的客户端应用程序吗?

c - 排序插入到具有重复项的固定大小数组中

c++ - 运行时错误 [Ubuntu 15.10 上的 abi :cxx11] when compile with g++-4. 9

c++ - sqlite:检查读写器锁

c++ - 成员函数指针 - 如何做到这一点?

c++ - boost::phoenix 相当于什么?

linux - 收到以下错误 : 13279:can't find self in the replset config when configuring replica sets

python - 设计多进程守护进程

c - (嵌入式)C : void function() or #define for low level register handling