基于VS2019 利用MFC编程控制 keysight 及大华电源

       在测试过程中 不可避免的要用到电源,如果要搭建自动测试环境,则需要对电源进行程控;本文对工作中用到的keysight 和 大华电源 以及其他电源进行了远程控制,供大家参考。

        实现程控时,需要用到VISA库和SCPI命令,这个大家可以参考各自的手册。另外,至于控制语句可以阅读 每个仪器的手册,均有远程控制 编程介绍。

        笔者在调试时是先试用command expert 进行调试,然后生成C语句,再整合到源码里的,建议初学者借用。

一、扫描 电源

ViStatus open()
{
    ViStatus status;
    ViSession defaultRM, instr;
    ViFindList fList;
    ViChar desc[VI_FIND_BUFLEN];
    ViUInt32 numInstrs;
    ViUInt16 iManf = 0, iModel = 0;
    int iDevIdn = 0;

    status = viOpenDefaultRM(&defaultRM);
    if (status < VI_SUCCESS) {
        /* Error initializing VISA ... exiting */
        return status;
    }

    /* Find all instruments in the system */
    status = viFindRsrc(defaultRM, "[USB]?*INSTR", &fList, &numInstrs, desc);
    if (status < VI_SUCCESS) {
        /* Error finding resources ... exiting */
        viClose(defaultRM);
        return status;
    }

    m_videfaultRM = defaultRM;
    m_nVcsmNumUsed = 0;

    int idn_size = 200; // Set this variable to the maximum size of the array.
    ViChar* idn = new char[idn_size];

    /* Open a session to each and determine if it matches */
    while (numInstrs--) {
        status = viOpen(defaultRM, desc, VI_NULL, VI_NULL, &instr);
        if (status < VI_SUCCESS) {
            viFindNext(fList, desc);
            continue;
        }

        status = viSetAttribute(instr, VI_ATTR_TERMCHAR_EN, TRUE);
        if (status < VI_SUCCESS) {
            viClose(instr);
            viFindNext(fList, desc);
            continue;
        }

        /* Set the timeout for message-based communication*/
        status = viSetAttribute(instr, VI_ATTR_TMO_VALUE, 5000);
        if (status < VI_SUCCESS) {
            viClose(instr);
            viFindNext(fList, desc);
            continue;
        }

        printf("scan dev : %s \n", desc);
        memset(idn, 0, idn_size);
        viQueryf(instr, "*IDN?\n", "%#t", &idn_size, idn);
        printf("scan dev idn:%s \n", idn);
        Sleep(1000);

        CString strTmp(idn);
        if ((iDevIdn = DeviceIdentification(instr, strTmp)) < 0)
        {
            viClose(instr);
            viFindNext(fList, desc);
            continue;
        }

        /*
                iManf = 0;
                status = viGetAttribute(instr, VI_ATTR_MANF_ID, &iManf);
                printf("device manufacture is 0x%x \n", iManf);
                if (status < VI_SUCCESS) {
                    viClose(instr);
                    viFindNext(fList, desc);
                    continue;
                }

                iModel = 0;
                status = viGetAttribute(instr, VI_ATTR_MODEL_CODE, &iModel);
                printf("device model is 0x%x \n", iModel);
                if (status < VI_SUCCESS) {
                    viClose(instr);
                    viFindNext(fList, desc);
                    continue;
                }
        */

        /* We have a match, return the session without closing it */
        
        //已打开的设备保存句柄,加入打开设备列表,不需要关闭。
        //viClose(instr);
        viFindNext(fList, desc);
    }

    delete[] idn;
    viClose(fList);

    if (m_nVcsmNumUsed > 0)
    {
        return VI_SUCCESS;
    }
    else
    {
        /* No match was found, return an error */
        viClose(defaultRM);
        return VI_ERROR_RSRC_NFOUND;
    }
}

二、 电源设置

_eVCSM_TYPE_T_ getVcsmType(ViSession session)
{
    _eVCSM_TYPE_T_ vcsmType;

    int idn_size = 200; // Set this variable to the maximum size of the array.
    char* idn = new char[idn_size];

    viQueryf(session, "*IDN?\n", "%#t", &idn_size, idn);
    //printf("VCSM IDN : %s \n", idn);
    Sleep(500);
    CString strDev(idn);
    if (strDev.Find(_T("DH1766")) >= 0)
        vcsmType = DAHUA_DEV;
    else if (strDev.Find(_T("E36311A")) >= 0)
        vcsmType = KEYSIGHT_DEV;
    else if (strDev.Find(_T("TH6412")) >= 0)
        vcsmType = TH6400_DEV;
    else
        vcsmType = TH6400_DEV;

    delete[] idn;

    return vcsmType;
}

void setup(ViSession viSession, vcsm_info_T* pDataArray)
{
    // Input parameter. 
    double voltage1 = pDataArray[0].volt;
    // Input parameter. 
    char chanlist1[] = "@1";
    // Input parameter. 
    double voltage2 = pDataArray[1].volt;
    // Input parameter. 
    char chanlist2[] = "@2";
    // Input parameter. 
    double voltage3 = pDataArray[2].volt;
    // Input parameter. 
    char chanlist3[] = "@3";
    // Input parameter. 
    unsigned short state = 1;
    // Input parameter. 
    double current1 = pDataArray[0].curr;
    // Input parameter. 
    double current3 = pDataArray[2].curr;
    // Input parameter. 
    double current2 = pDataArray[1].curr;

    if (getVcsmType(viSession) == KEYSIGHT_DEV)
    {
        viPrintf(viSession, ":SOURce:VOLTage:LEVel:IMMediate:AMPLitude %@3lf,(%s)\n", voltage1, chanlist1);
        viPrintf(viSession, ":SOURce:VOLTage:LEVel:IMMediate:AMPLitude %@3lf,(%s)\n", voltage2, chanlist2);
        viPrintf(viSession, ":SOURce:VOLTage:LEVel:IMMediate:AMPLitude %@3lf,(%s)\n", voltage3, chanlist3);
        viPrintf(viSession, ":SOURce:CURRent:LEVel:IMMediate:AMPLitude %@3lf,(%s)\n", current1, chanlist1);
        viPrintf(viSession, ":SOURce:CURRent:LEVel:IMMediate:AMPLitude %@3lf,(%s)\n", current2, chanlist2);
        viPrintf(viSession, ":SOURce:CURRent:LEVel:IMMediate:AMPLitude %@3lf,(%s)\n", current3, chanlist3);
        viPrintf(viSession, ":OUTPut:STATe %@1d,(%s)\n", state, chanlist1);
        viPrintf(viSession, ":OUTPut:STATe %@1d,(%s)\n", state, chanlist2);
        viPrintf(viSession, ":OUTPut:STATe %@1d,(%s)\n", state, chanlist3);
    }
    else if (getVcsmType(viSession) == DAHUA_DEV)
    {
        viPrintf(viSession, ":INSTrument:SELect CH1\n");
        viPrintf(viSession, ":VOLTage %@3lf\n", voltage1);
        viPrintf(viSession, ":CURRent %@3lf\n", current1);
        //viPrintf(viSession, ":OUTPut %@1d\n", state);

        viPrintf(viSession, ":INSTrument:SELect CH2\n");
        viPrintf(viSession, ":VOLTage %@3lf\n", voltage2);
        viPrintf(viSession, ":CURRent %@3lf\n", current2);
        //viPrintf(viSession, ":OUTPut %@1d\n", state);

        viPrintf(viSession, ":INSTrument:SELect CH3\n");
        viPrintf(viSession, ":VOLTage %@3lf\n", voltage3);
        viPrintf(viSession, ":CURRent %@3lf\n", current3);
        //viPrintf(viSession, ":OUTPut %@1d\n", state);

        viPrintf(viSession, ":APPLy:OUTPut:STATe ON, ON, ON\n");
    }
    else if (getVcsmType(viSession) == TH6400_DEV)
    {
        viPrintf(viSession, ":INSTrument:NSELect 1\n");
        viPrintf(viSession, ":VOLTage %@3lf\n", voltage1);
        viPrintf(viSession, ":CURRent %@3lf\n", current1);
        //viPrintf(viSession, ":OUTPut ON\n");

        viPrintf(viSession, ":INSTrument:NSELect 2\n");
        viPrintf(viSession, ":VOLTage %@3lf\n", voltage2);
        viPrintf(viSession, ":CURRent %@3lf\n", current2);
        //viPrintf(viSession, ":OUTPut ON\n");

        viPrintf(viSession, ":INSTrument:NSELect 3\n");
        viPrintf(viSession, ":VOLTage %@3lf\n", voltage3);
        viPrintf(viSession, ":CURRent %@3lf\n", current3);
        //viPrintf(viSession, ":OUTPut ON\n");

        viPrintf(viSession, ":APPL:OUT ON, ON, ON\n");
    }
}

三、电源关闭

void poweroff(ViSession viSession)
{
    if (getVcsmType(viSession) == KEYSIGHT_DEV)
        viPrintf(viSession, "*RST\n");
    else if (getVcsmType(viSession) == DAHUA_DEV)
        viPrintf(viSession, ":APPLy:OUTPut:STATe OFF, OFF, OFF\n");
    else if (getVcsmType(viSession) == TH6400_DEV)
        viPrintf(viSession, ":APPL:OUT OFF, OFF, OFF\n");
}

上述就是对电源的控制软件的编写,已经调试通过,可以完全控制电源。

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值