Newer
Older
AutomaticVerification / softwareDirectory / AutoVerScheme / visacommonengine.cpp
陈实 on 9 Mar 2024 2 KB add visa engine
#include "visacommonengine.h"
#include <QDebug>


VisaCommonEngine::VisaCommonEngine()
{

}

bool VisaCommonEngine::openDevice()
{
    status = viOpenDefaultRM(&defaultRM);
    if (status < VI_SUCCESS)
    {
        qDebug() << "Could not open a session to the VISA Resource Manager!\n";
    }

    status = viOpen(defaultRM, remoteAddr, VI_NULL, VI_NULL, &instr);
    if (status < VI_SUCCESS)
    {
        qDebug() << "Cannot open a session to the device.\n";
        goto Close;
    }

    //viQueryf(instr,"IDN  ?\n","%t",buffer);
    status = viQueryf( instr, "*IDN?\n", "%s", buffer);

    if (status < VI_SUCCESS){
        qDebug() << "Error reading a response from the device.\n";
    }else{
        qDebug() << "Data read:" << retCount << QString::fromLocal8Bit((char*)buffer);

        //ui->textBrowser->setText(QString::fromLocal8Bit((char*)buffer,retCount));
    }


    /* Set the timeout to 5 seconds (5000 milliseconds). */
    status = viSetAttribute(instr, VI_ATTR_TMO_VALUE, 5000);

    /* Set the baud rate to 4800 (default is 9600). */
    status = viSetAttribute(instr, VI_ATTR_ASRL_BAUD, 4800);

    /* Set the number of data bits contained in each frame (from 5 to 8).
         * The data bits for  each frame are located in the low-order bits of
         * every byte stored in memory.
         */
    status = viSetAttribute(instr, VI_ATTR_ASRL_DATA_BITS, 8);

    status = viSetAttribute(instr, VI_ATTR_ASRL_PARITY, VI_ASRL_PAR_NONE);

    status = viSetAttribute(instr, VI_ATTR_ASRL_STOP_BITS, VI_ASRL_STOP_ONE);

    status = viSetAttribute(instr, VI_ATTR_TERMCHAR_EN, VI_TRUE);

    /* Set the termination character to 0xA
         */
    status = viSetAttribute(instr, VI_ATTR_TERMCHAR, 0xA);


    strcpy(stringinput, ":MEASure:VOLTage:DC?\n");
    //strcpy(stringinput, "*IDN?\n");
    status = viWrite(instr, (ViBuf)stringinput, (ViUInt32)strlen(stringinput), &writeCount);
    if (status < VI_SUCCESS){
        qDebug() << "Error writing to the device.\n";
        goto Close;
    }

    status = viRead(instr, buffer, 100, &retCount);
    if (status < VI_SUCCESS){
        qDebug() << "Error reading a response from the device.\n";
    }else{
        qDebug() << "Data read:" << retCount << QString::fromLocal8Bit((char*)buffer,retCount);

        //ui->textBrowser->setText(QString::fromLocal8Bit((char*)buffer,retCount));
    }

Close:
    status = viClose(instr);
    status = viClose(defaultRM);
    return true;
}

QString VisaCommonEngine::queryData()
{
    return "";
}