c++ - NI VISA 和 viWrite 未检测到命令错误

标签 c++ visa

我正在测试 vanilla viWrite() 函数,发现当我传入无效的命令字符串时,它不会返回错误代码。我觉得这有点奇怪……当然实现应该检测到这个事件。

这是我用来演示这个的一个小测试用例......(它只是测试代码,所以它不会是完美的:))

#include <visa.h>
#include <cstring>
#include <iostream>

#define VIBUF_LEN 255

static ViChar viBuf[VIBUF_LEN];

void MyWrite(ViSession sess, char const *cmd)
{
    ViStatus status;
    ViUInt32 rcount;

    strncpy_s(viBuf, cmd, VIBUF_LEN);
    status = viWrite(sess, (ViBuf)viBuf, strlen(viBuf), &rcount);
    if (status < VI_SUCCESS)
    {
        std::cout << "Failed to write!\n";
        exit(-1);
    }
}

std::string MyRead(ViSession sess)
{
    ViStatus status;
    ViUInt32 rcount;

    status = viRead(sess, (ViBuf)viBuf, VIBUF_LEN, &rcount);
    if (status < VI_SUCCESS)
    {
        std::cout << "Failed to read 1!\n";
        exit(-1);
    }
    else if (rcount >= VIBUF_LEN)
    {
        std::cout << "Failed to read 2!\n";
        exit(-1);
    }
    else if (!rcount)
    {
        std::cout << "Failed to read 3!\n";
        exit(-1);
    }

    viBuf[rcount] = NULL;
    return std::string(viBuf);
}

int _tmain(int argc, _TCHAR* argv[])
{
    ViStatus status;
    ViSession mVisaDefaultRM;
    ViSession mVisaInst;

    status = viOpenDefaultRM(&mVisaDefaultRM);
    if (status == VI_SUCCESS)
    {
        strncpy_s(viBuf, "GPIB0::1::INSTR", VIBUF_LEN);
        status = viOpen(mVisaDefaultRM, viBuf, VI_NULL, VI_NULL, &mVisaInst);
    }

    if (status < VI_SUCCESS)
    {
        std::cout << "Failed to initialise!\n";
        exit(-1);
    }

    viClear(mVisaInst);
    MyWrite(mVisaInst, "*CLS;");
    MyWrite(mVisaInst, "*RST;");
    MyWrite(mVisaInst, "*SRE 0;");
    MyWrite(mVisaInst, "*ESE 0;");
    MyWrite(mVisaInst, "CRAP;");   /* Wow really!? */
    MyWrite(mVisaInst, "*ESR?;");
    std::string str = MyRead(mVisaInst);
    std::cout << "ESR is " << str.c_str() << "\n";

    std::cout << "END\n";
    getchar();

    return 0;
}

程序输出如下:

ESR is +32
END

因此,写入 SCPI 命令“CRAP;”肯定被设备标记为错误。

这让我想,啊...我没有启用 ESE 位来使该位在 STB 中被标记。所以我这样做:

MyWrite(mVisaInst, "*ESE 255;");
//                       ^^^
//                       A bit of a sledge hammer but should do the job

仍然没有检测到错误的命令。

好的,所以可能需要启用 SRQ...也许 VISA 库需要启用这两个来处理这个...

所以我这样做:

MyWrite(mVisaInst, "*SRE 255;");
//                       ^^^
//                       A bit of a sledge hammer but should do the job

不,没有区别。它仍然没有检测到错误的命令。

这是标准签证吗?它应该像这样工作吗?这是否意味着如果我想检测这些错误,我总是必须启用事件 VI_EVENT_SERVICE_REQ 以及写入后的 viWaitOnEvent()?我原以为 Vanilla viWrite() 会为我检测到这个??

最佳答案

viWrite 只负责写入(请参阅 Ni Visa Programmers Reference ),这意味着它仅在存在真正的通信故障(例如超时或有人拔下电缆等)时返回错误。这是低级 I/O 功能的标准(套接字、串行端口……都以这种方式工作)。

这意味着为了查明是否存在远程设备错误,您必须以某种方式进行查询。我不熟悉 VISA,所以我不确定最好的方法是什么。它要么是你谈论的风格(事件),要么你可以直接查询设备? (也许你可以 viWrite 一个命令,上面写着“给我你的状态”,然后 viRead 响应?)

关于c++ - NI VISA 和 viWrite 未检测到命令错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17673617/

相关文章:

c++ - 使用命令行参数选择数据类型?

c++ - "using Time = cppClassDefinition<withT>"的 Cython 等价物

go - 在Linux中通过USB连接到USBTMC设备

Python 脚本调用不同的 python 脚本,然后从辅助脚本将字符串/结果带到主脚本

Python:具有 USB 连接的 PyVisa -> wait_for_srq() 还是 wait_on_event?

c++ - 如何在程序内存中隐藏纯文本?

c++ - 为什么我无法在 range-v3 中获取范围的大小?

C++ 找不到已安装的库

vb.net - Visual Basic 6.0 语句不在有效的命名空间中