Newer
Older
AutomaticVerification / softwareDirectory / AutoVerScheme / visacommonengine.cpp
陈实 on 22 Mar 2024 3 KB 自动检定修改
#include "visacommonengine.h"
#include <QDebug>


VisaCommonEngine::VisaCommonEngine()
{

}

/*VisaCommonEngine::~VisaCommonEngine()
{
    closeDevice();
}*/
bool VisaCommonEngine::getState()
{
    return commState;
}
bool VisaCommonEngine::openDevice(QString sAddr,QString sType)
{
    if(getState())
    {
        closeDevice();
    }
    remoteAddr = sAddr.toLatin1().data();
    commType = sType;
    status = viOpenDefaultRM(&defaultRM);
    if (status < VI_SUCCESS)
    {
        qDebug() << "Could not open a session to the VISA Resource Manager!\n";
        return false;
    }

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

    //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, 10000);

    /* 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));
    //}
    commState = true;
    return true;
}

void VisaCommonEngine::closeDevice()
{
    status = viClose(instr);
    status = viClose(defaultRM);
    commState = false;
}

QString VisaCommonEngine::queryData(QString sCmd)
{
    ViConstRsrc sQuery = (sCmd+"\n").toLatin1().data();
    status = viQueryf( instr, sQuery, "%s", buffer);

    if (status < VI_SUCCESS){
        qDebug() << "Error reading a response from the device.\n";
        return "";
    }else{
        qDebug() << "Data read:" << retCount << QString::fromLocal8Bit((char*)buffer);
        return QString::fromLocal8Bit((char*)buffer);
        //ui->textBrowser->setText(QString::fromLocal8Bit((char*)buffer,retCount));
    }
    return "";
}

bool VisaCommonEngine::sendData(QString sCmd)
{
    //strcpy(stringinput, strData.toLatin1().data());
    //strcpy(stringinput, "*IDN?\n");
    char *strBuf = (sCmd+"\n").toLatin1().data();
    status = viWrite(instr, (ViBuf)strBuf, (ViUInt32)strlen(strBuf), &writeCount);
    if (status < VI_SUCCESS){
        return false;
    }
    return true;
}