0

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

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

#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_REQviWaitOnEvent()?我原以为香草viWrite()会为我检测到这一点??

4

1 回答 1

2

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

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

于 2013-07-16T11:11:39.467 回答