目录
一、用途和前提
在进行诊断的高低压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);
}
以上就是两种实现方法,较为简易,如有需要可自行扩展。