python技巧:使用pyvisa控制仪器;安装NI-VISA等visa库;导入pyvisa并创建资源管理器;打开和使用仪器
PyVISA: Control your instruments with Python
PyVISA: Control your instruments with Python — PyVISA 1.15.1.dev14+gae06ee8 documentationhttps://pyvisa.readthedocs.io/en/latest/
VISA的起源
the Virtual Instrument Software Architecture (VISA) specification was defined in the middle of the 90ies. VISA is a standard for configuring, programming, and troubleshooting instrumentation systems comprising GPIB, VXI, PXI, Serial, Ethernet, and/or USB interfaces.
VISA library
Python can be used to call functions from a VISA shared library (.dll, .so, .dylib) allowing to directly leverage the standard implementations. In addition, Python can be used to directly access most bus systems used by instruments which is why one can envision to implement the VISA standard directly in Python (see the PyVISA-Py project for more details). PyVISA is both a Python wrapper for VISA shared libraries but can also serve as a front-end for other VISA implementation such as PyVISA-Py.
Currently there are two backends available: The one included in pyvisa, which uses the IVI library (include NI-VISA, Keysight VISA, R&S VISA, tekVISA etc.), and the backend provided by pyvisa-py, which is a pure python implementation of the VISA library. If no backend is specified, pyvisa uses the IVI backend if any IVI library has been installed (see next section for details). Failing that, it uses the pyvisa-py backend.
具体步骤
一,安装和配置VISA驱动
1、安装NI-VISA
要使用pyvisa与仪器进行通信,你需要安装一个VISA库,如NI-VISA。
2、配置VISA库
安装NI-VISA后,你可以通过NI Measurement & Automation Explorer (NI MAX) 来配置和管理你的仪器。打开NI MAX后,你可以看到所有连接到你计算机的仪器,并可以配置它们的通信参数。
3、测试连接
配置完成后,你可以通过NI MAX来测试与仪器的连接,确保所有配置都正确无误。
二,导入pyvisa并创建资源管理器
1、导入pyvisa库
安装和配置完成后,你可以在你的Python代码中导入pyvisa库:
pip install pyvisa
import pyvisa
2、创建资源管理器
导入pyvisa库后,你需要创建一个资源管理器(Resource Manager)来管理和访问所有连接到你计算机的仪器。可以通过以下代码来创建资源管理器:
rm = pyvisa.ResourceManager()
资源管理器创建完成后,你可以通过它来获取所有连接到你计算机的仪器列表:
instruments = rm.list_resources()
print(instruments)
三,打开和使用仪器
1、打开仪器
获取到仪器列表后,你可以通过资源管理器来打开特定的仪器。假设你有一个仪器的地址是'GPIB0::14::INSTR',你可以通过以下代码来打开这个仪器:
instrument = rm.open_resource('GPIB0::14::INSTR')
2、发送和接收命令
打开仪器后,你可以通过send和receive命令与仪器进行通信。例如,发送一个命令来获取仪器的标识信息:
idn = instrument.query('*IDN?')
print(idn)
3、关闭仪器
使用完仪器后,你需要关闭它以释放资源:
instrument.close()
注意:
使用其他VISA库
除了NI-VISA,你还可以使用其他VISA库,如Keysight VISA或Tektronix VISA。使用方法与NI-VISA类似,只需安装相应的驱动程序即可。
2、pyvisa-py
如果你希望使用纯Python实现的VISA库,可以使用pyvisa-py。pyvisa-py是pyvisa的一个后端,可以替代NI-VISA等驱动程序。安装pyvisa-py的方法如下:
pip install pyvisa-py
安装完成后,你可以在创建资源管理器时指定使用pyvisa-py后端:
rm = pyvisa.ResourceManager('@py')