CAPL或Pyhon实现GPIB控制程控电源

目录

一、用途和前提

二、CAPL实现

三、Python实现及capl调用pyhton

1.python实现

2、CAPL 调用


一、用途和前提

在进行诊断的高低压DTC,刷写逆向测试、网络管理的状态切换,上电第一帧报文时间等等的测试中,电源电压的切换是非常频繁的,这就需要对电源电压进行较为精准的控制。在用CAPL或Python实现控制功能之前,首先需要安装NI提供的驱动NI-488.2。

二、CAPL实现

1.这里面主要的内容是这段

gDeviceID = GPIBDevOpen(GPIB_Board_Index, // Board Index
                          GPIB_Primary_Address, // Primary Address
                          GPIB_Secondary_Address,  // Secondary Address
                          cTimeout,                // Timeout
                          cEot,                    // Eot
                          cEos);                   // Eos

 :GPIBDevOpen函数是canoe已经封装好的,具体用法可直接看help文档

2.以下是CAPL实现的控制电压设置的简单脚本

variables
{
  // Connection parameters
  const long cEot = 1; // GPIB EOI line is set at the end of the write operation
  const long cEos = 0; // End-pf-String behavior
  const long cTimeout = 13; // Timeout of the ddevice: 10

  // Status variables
  long gDeviceID = 0;
  int  gConnected = 0;
  long GPIB_Board_Index = 0;
  long GPIB_Primary_Address = 13;
  long GPIB_Secondary_Address = 0;
  
}

on preStart
{
  Connect();
}

Connect ()
{
  long retVal;

  // Opens the GPIB device
  gDeviceID = GPIBDevOpen(GPIB_Board_Index, // Board Index
                          GPIB_Primary_Address, // Primary Address
                          GPIB_Secondary_Address,  // Secondary Address
                          cTimeout,                // Timeout
                          cEot,                    // Eot
                          cEos);                   // Eos

  if( gDeviceID < 0 )
  {
    Write("ERROR: Call to GPIBDevOpen failed (Code: %d)!", gDeviceID);
    return;
  }

  // Activates the GPIB device
  retVal = GPIBDevOnline(gDeviceID, 1);

  if( retVal < 0 )
  {
    Write("ERROR: Call to GPIBDevOnline failed (Code: %d)!", retVal);
    return;
  }
            
  Write("Connection to GPIB device succeeded...");
  gConnected = 1;                            
}

Power_ON()
{
   GPIBWriteStr(gDeviceID, "USET 12.2");
  write("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}

Power_OFF()
{
   GPIBWriteStr(gDeviceID, "USET 0");
  
}


Power_High()
{
   GPIBWriteStr(gDeviceID, "USET 16.0");
 
}

Power_Low()
{
   GPIBWriteStr(gDeviceID, "USET 9.0");
 
}
on key 'A'
{
 Power_ON(); 
}

on key 'B'
{
 Power_OFF(); 
}

三、Python实现及capl调用pyhton

1.python实现

通过pip安装相应模块直接引用,以下是纯python实现

import pyvisa as visa
import sys
import os.path
import time
#visa.log_to_screen()

class Voltage_Ctr:
    def __init__(self,addr):
        '''self.voltage = voltage
        self.current = current'''
        self.rm = visa.ResourceManager()
        self.my = self.rm.open_resource(addr)
    def Voltage_set(self,voltage):
        #print(voltage)
        a=str(voltage)
        #print(a)
        uset='USET *U*'
        uset=uset.replace('*U*',a)
        #print(uset)
        self.my.write(uset)
        self.my.write("ISET 2")
        self.my.write("OUTP ON")
    def Voltage_get(self):

        p=self.my.query('UOUT?')
        out=float(p[7:12])
        return out
        print(out)
        print(len(p))
        print(type(out))
        #print(rm.list_resources())
        print("------------",p)

if __name__ == '__main__':
    V=Voltage_Ctr("GPIB0::13::INSTR")
    V.Voltage_set(12.3)
    outV = V.Voltage_get()
    print(outV)

2、CAPL 调用

2.1、CAPL调用时首先需要将python的mian函数做一下更改为如下,其次将python文件通过Pyinstaller模块打包成.exe文件,如何打包方法可直接在CSDN搜索,步骤详细。

if __name__ == '__main__':
    for i in sys.argv:
        print(i)
    if len(sys.argv) == 3 :
        c = Voltage_Ctr(sys.argv[1])
        c.Voltage_set(sys.argv[2])

2.2、需要注意的是要确定自己设备的地址需要匹配,其中的核心函数是:sysExeC()是canoe自带的可看帮助文档

variables
{
  char InputAddr[256] = "GPIB0::13::INSTR";
  int voltage;
  
}

on key 'c'
{
  char parameter[256];
  char scriptPath[256]="D:\\Capl_python\\Voltage_Control\\voltage.exe";
  voltage = 15;
  snprintf(parameter,elCount(parameter),"%s %d",InputAddr,voltage);
  write("-----------%s",parameter);
  sysExeC(scriptPath,parameter);
 
}

以上就是两种实现方法,较为简易,如有需要可自行扩展。

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

博学之~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值