Python的扩展接口[0] -> VISA仪器控制

VISA仪器控制 / VISA Instrument Control


 1 VISA简介 / VISA Introduction

VISA(Virtual Instrument Software Architecture,简称为VISA),即虚拟仪器软件结构,是VXI plug&play联盟制定的I/O接口软件标准及其规范的总称。VISA提供用于仪器编程的标准I/O函数库,称为VISA库。VISA函数库驻留在计算机系统内,是计算机与仪器的标准软件通信接口,计算机通过它来控制仪器。

作为通用I/O标准,VISA提供了统一的设备资源管理、操作和使用机制,它独立于硬件设备、接口、操作系统和编程语言,具有与硬件结构无关的特点。VISA的这一特性使之适用于各种仪器接口,无论仪器使用的串口还是其他任何一种总线,诸如GPIB、VXI、PXI和LXI等,都具有相同的操作函数,从而实现了控制操作上的统一。Visa基于自底向上的结构模型,创造了一个统一形式的I/O控制函数集。一方面,对初学者或是简单任务的设计者来说,Visa提供了简单易用的控制函数集,在应用形式上相当简单;另一方面,对复杂系统的组建者来说,Visa提供了非常强大的仪器控制功能与资源管理。

2 PyVISA库 / PyVISA Library

PyVISA是Python的一个包,使Python能够独立于接口(如GPIB,RS232,USB,Ethernet)控制各种测量设备。PyVISA极大的简化了对仪器的控制方式,仅仅需要几行代码即可以实现对仪器的操作。

2 PyVISA环境搭建

需要利用Python实现对仪器的控制,首先需要对环境进行相应的配置,基本在于以下几点,

       1. 确保系统中有visa32.dll文件的存在;

       2. 安装相应的仪器驱动软件;

       3. 安装pyvisa包。

其中,visa32.dll文件一般存放的位置在c:/windows/system32/visa32.dll,而仪器的驱动可到相应官网进行下载,而pyvisa包则可以使用pip进行安装,

  pip install pyvisa

3 PyVISA基本用法

下面介绍一下pyvisa用于仪器控制的基本使用方式,十分简单,详细的使用方式可以参考官方说明

  1. 首先导入visa模块,定义一些基本信息,包括visadll的位置,以及仪器控制方式及其信息,如TCP/IP的IP地址,GPIB的端口号信息等,按照指定格式进行填充。
  2. 接着利用visadll文件创建一个visa的实例对象,有了这个实例对象就可以连接仪器。利用创建的visa对象生成连接实例,此处以GPIB和TCPIP为例。
  3. 利用连接实例就可以开始与仪器进行通信了,通信的接口函数主要有以下3种,write() /read() /query(),具体使用哪一种需要根据每种仪器的通信协议命令集查询命令的属性,为写/读/读写等。有了接口函数就可以利用对应的命令集进行命令的发送。而具体的命令则可以通过相应的仪器手册进行查询。
import visa
visa_dll = 'c:/windows/system32/visa32.dll'
tcp_addr = 'TCPIP::192.168.1.1::inst0::INSTR'
gpib_addr = 'GPIB0::12::INSTR'
# Create an object of visa_dll
rm = visa.ResourceManager(visa_dll)
# Create an instance of certain interface(GPIB and TCPIP)
tcp_inst = rm.open_resource(tcp_addr)
gpib_inst = rm.open_resource(gpib_addr)
# Command '*IDN?' can fetch instrument info
# Using write()/read()/query() function to make communication with device
# according to the command type
print(tcp_inst.query('*IDN?'))
print(gpib_inst.query('*IDN?'))

4 PyVISA应用实例

下面是一些在使用pyvisa进行仪器控制时编写的基本使用实例,可作为使用参考。

Note: 实例中仅仅是实现了一部分需求,若需要完成更多的命令需求,可以参考手册文件。

4.1 安捷伦E5071C (TCP/IP)

import visa
class E50X():
    def __init__(self, ip, visaDLL=None, *args):
        self.ip = ip
        self.visaDLL = 'c:/windows/system32/visa32.dll' if visaDLL is None else visaDLL
        self.address = 'TCPIP::%s::inst0::INSTR' % self.ip

        self.resourceManager = visa.ResourceManager(self.visaDLL)


    def open(self):
        self.instance = self.resourceManager.open_resource(self.address)

        self.instance.write('MMEM:STOR:SNP:FORM DB')


    def close(self):
        if self.instance is not None:
            self.instance.close()

            self.instance = None


    def create_dir(self, path):
        print('MMEM:MDIR "%s"' % path)

        self.instance.write('MMEM:MDIR "%s"' % path)


    def recall_sta(self, filename):
        print('MMEM:LOAD "%s"' % filename)
        self.instance.write('MMEM:LOAD "%s"' % filename)
        # Time sleep in case of sta load uncompleted

        time.sleep(0.5)


    def wind_act(self, wind):

        self.instance.write('DISP:WIND%d:ACT' % wind)


    def wind_max(self, wind):

        self.instance.write('DISP:MAX %s' % wind)


    def trigger(self, status):
        cmdList = {'hold': 'OFF', 'continuous': 'ON'}

        self.instance.write('INIT1:CONT %s' % cmdList[status])


    def save_snp(self, name, n=2):
        print('MMEM:STOR:SNP "%s.s%dp"' % (name, n))

        self.instance.write('MMEM:STOR:SNP "%s.s%dp"' % (name, n))


    def save_image(self, imagname, fmt):
        assert fmt in ['jpg', 'png'], 'Invalid postfix of image'
        print('MMEM:STOR:IMAG "%s.%s"' % (imagname, fmt))

        self.instance.write('MMEM:STOR:IMAG "%s.%s"' % (imagname, fmt))


    def reset(self):

        self.instance.write('*RST')


    def read_idn(self):
        idn = self.instance.query('*IDN?')
        print(idn)
        return idn
    
    def read_data(self, wind=1, trac=1, axis='x'):
        posi = {'x': 'XAX?', 'y': 'FDAT?'}
        data = self.instance.query('CALC%d:TRAC%d:DATA:%s' % (wind, trac, posi[axis]))
        return eval(data)
    
    def tran_file(self):
        re = self.instance.query(":MMEM:TRAN? 'D:\\22.S2P'")
        with open("x.S2P", 'w') as f:
            f.write(re)
        print(type(re))

if __name__ == '__main__':
    e50 = E50X('192.168.1.17')
    e50.open()
    e50.read_idn()
    e50.tran_file()
    e50.close()

4.2 安捷伦C34970A (GPIB)

import visa
class A34X():
    def __init__(self):
        self.address = 'GPIB0::8::INSTR'
        self.visaDll = 'c:/windows/system32/visa32.dll'
        self.resourceManager = visa.resourceManager(self.visaDll)
    def open(self):
        self.instance = resourceManager.open_resource(self.address)
        self.idn = self.instance.query('*IDN?')
        print(self.idn)


    def reset(self):
        self.instance.write('*RST')


    def set_dc(self):
        self.instance.write('CONF:CURR:DC AUTO, (@119, 120)')

  • 4
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
failed UnsatisfiableError: The following specifications were found to be incompatible with the existing python installation in your environment: Specifications: - torchaudio -> python[version='2.7.*|3.5.*|3.6.*|>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|3.4.*'] Your python: python=3.8 If python is on the left-most side of the chain, that's the version you've asked for. When python appears to the right, that indicates that the thing on the left is somehow not available for the python version you are constrained to. Note that conda will not change your python version to a different minor version unless you explicitly specify that. The following specifications were found to be incompatible with each other: Output in format: Requested package -> Available versions Package pytorch-cuda conflicts for: torchvision -> pytorch-cuda[version='11.6.*|11.7.*|11.8.*'] torchaudio -> pytorch==2.0.1 -> pytorch-cuda[version='>=11.6,<11.7|>=11.7,<11.8|>=11.8,<11.9'] torchaudio -> pytorch-cuda[version='11.6.*|11.7.*|11.8.*'] torchvision -> pytorch==2.0.1 -> pytorch-cuda[version='>=11.6,<11.7|>=11.7,<11.8|>=11.8,<11.9'] Package requests conflicts for: python=3.8 -> pip -> requests torchvision -> requests Package setuptools conflicts for: torchvision -> setuptools pytorch -> jinja2 -> setuptools python=3.8 -> pip -> setuptools Package msvc_runtime conflicts for: pytorch -> python[version='>=3.5,<3.6.0a0'] -> msvc_runtime torchvision -> python[version='>=3.5,<3.6.0a0'] -> msvc_runtime Package pytorch conflicts for: torchaudio -> pytorch[version='1.10.0|1.10.1|1.10.2|1.11.0|1.12.0|1.12.1|1.13.0|1.13.1|2.0.0|2.0.1|1.9.1|1.9.0|1.8.1|1.8.0|1.7.1|1.7.0|1.6.0'] torchvision -> pytorch[version='1.10.0|1.10.1|1.10.2|1.11.0|1.12.0|1.12.1|1.13.0|1.13.1|2.0.0|2.0.1|1.9.1|1.9.0|1.8.1|1.8.0|1.7.1|1.7.0|1.6.0|1.5.1']
06-03

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值