I recently learned how to control measuring instruments using python. Compared to famous measuring instrument control software (LabV * EW, etc.), I am impressed by its high versatility and degree of freedom. I will summarize specific sample programs at a later date, but here I will list the commands of pyvisa that are frequently used. It's a memorandum for myself, so I don't know.
Reference page: PyVISA https://pyvisa.readthedocs.io/en/latest/
python3.7.7 Windows10
Here we assume that Python is already installed.
What you need to communicate. Generally, it is provided free of charge by National Instruments. Install the one that matches the OS of your PC. In addition, PyVISA works with VISA provided by other companies. (Keysight VISA, R & S VISA, tekVISA, etc.)
Enter from pip (pip3).
$ pip install pyvisa
This completes the environment construction. Very easy.
import pyvisa
rm = pyvisa.ResourceManager()
visa_list = rm.list_resources()
usb_1 = visa_list[0]
inst_1 = rm.openresources(usb1)
inst_1.write('*IDN?')
out = inst_1.read()
#Of course you can use query
# out = inst_1.query('*IDN?')
print(out)
# (Measuring instrument information)
This code will be explained below.
Import and create an instance on python.
import pyvisa
#Instance generation, something like magic
rm = pyvisa.ResourceManager()
#Obtaining the VISA resource name of the measuring instrument connected to the PC
visa_list = rm.list_resources()
Here, `rm``` is optional. Also,
rm.list_resources ()
`` is a list and the VISA resource name is returned. Here we assume that one device is connected to a USB port. Get the VISA resource name.
#VISA resource name
usb_1 = visa_list[0]
Specify the instrument using the VISA resource name.
#Designation of measuring instrument
inst_1 = rm.open_resource(usb1)
inst_1.write('*IDN?')
'*idn?'
Is a command to listen to the model information of the measuring instrument. It is used in many devices.
inst_1.read()
You can read the response to the query you sent earlier. Since we are sending'IDN?'This time, the model information will be returned.
inst_1.query('IDN?')
Suguremono that sends a query and goes all at once until it receives a return value. The top two can be written in one line, which can be simplified. Note that if you send a command without a query, an error will occur.
inst_1.write('*rst, *IDN?')
You can also send multiple commands at once. Writing many lines, such as device setting commands, complicates the program. It's a little refreshing to use. However, if you send too many commands at once, communication will not be able to keep up, so you need to wait and see.
A control program is constructed by combining the VISA communication commands introduced so far with the basic programming framework such as loops and calculations. For commands for SCPI communication sent to measuring instruments via VISA, refer to the instruction manuals of various measuring instruments.
Here we have explained how to control the instrument using Python. I think it is better to use a USB cable or NI's USB-GPIB cable to connect to the measuring instrument. The GPIB cable is a little expensive, and recent measuring instruments basically have a USB terminal, so we recommend connecting with a USB cable.
In the future, we plan to easily summarize the programs that actually control and the code used at that time.
Recommended Posts