Instrument control using Python [pyvisa]

Introduction

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/

environment

python3.7.7 Windows10

Environment

Here we assume that Python is already installed.

NI-VISA installation

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.)

Install PyVISA

Enter from pip (pip3).

$ pip install pyvisa

This completes the environment construction. Very easy.

Sample code

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)

VISA writing

inst_1.write('*IDN?')

'*idn?'Is a command to listen to the model information of the measuring instrument. It is used in many devices.

Read VISA

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.

VISA query

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.

Send multiple commands once

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.

Finally

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

Instrument control using Python [pyvisa]
[Control engineering] Graphing transfer functions using Python
Install Python Control
Scraping using Python
Operate Redmine using Python Redmine
Fibonacci sequence using Python
Data analysis using Python 0
Data cleaning using Python
Using Python #external packages
WiringPi-SPI communication using Python
Python control syntax (memories)
Age calculation using python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
Try using Tweepy [Python2.7]
Get and automate ASP Datepicker control using Python and Selenium
Python notes using perl-ternary operator
Flatten using Python yield from
Scraping using Python 3.5 async / await
Save images using python3 requests
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
[Python tutorial] Control structure tool
Using Quaternion with Python ~ numpy-quaternion ~
Try using Kubernetes Client -Python-
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
Scraping using Python 3.5 Async syntax
Website change monitoring using python
Post to Twitter using Python
Start to Selenium using python
Search algorithm using word2vec [python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Create JIRA tickets using Python
Manipulate spreadsheets locally using Python
Python memo using perl --join
Web scraping using Selenium (Python)
Serial communication control with python and I2C communication (using USBGPIO8 device)
[Python] I tried using OpenPose
[ev3dev × Python] Single motor control
[Python] JSON validation using Voluptuous
Broadcast on LINE using python
Data analysis using python pandas
Translate using googletrans in Python
Using Python mode in Processing
Serial communication control with python and SPI communication (using USBGPIO8 device)
Using OpenCV with Python @Mac
[Python] Shooting game using pyxel
Send using Python with Gmail
Control the motor with a motor driver using python on Raspberry Pi 3!
Python control syntax, functions (Python learning memo ②)
How to install python using anaconda
[ev3dev × Python] Control of multiple motors
Harmonic mean with Python Harmonic mean (using SciPy)
Initializing global variables using Python decorators
[Python] Loading csv files using pandas
GUI programming in Python using Appjar
Retry post request using python requests