Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Introduction

to Instrument Control Using Python

Table of Contents
Introduction 0
Stack Structure 1
General structure 1.1
Python stack structure 1.2
Installation and deployment 2
Linux 2.1
Windows 2.2
Mac 2.3
Instrument Control 3
Command-based 3.1
SCPI devices 3.1.1
non-SCPI devices 3.1.2
Object-based 3.2

2
Introduction to Instrument Control Using Python

Introduction to Instrument Control Using


Python
This book briefly describes the hardware protocol and software interfaces that a modern IVI-
specification-compatible instrument device should offer, and gives out guidelines about
interfacing these devices with python programming language using examples.

Stack Structure
Modern experimental instrument devices uses GPIB/USB/LAN as hardware interfaces to
talk with controllers (eg. Laboratory computers), utilizing the use of protocols such as
USBTMC on USB interface and VXI-11 on LAN interface, providing a command-respond-
event model of operating devices. Most of the devices define their operating commands with
compliance to the Standard Commands for Programmable Instrumentation (SCPI)
specifications.

Instrument vendors, software providers and open source community have built a large
number of software stacks to help interfacing with instruments, most of these stacks are
compliant to the Virtual Instrument Software Architecture (VISA) standards.
Implementing the Interchangeable Virtual Instruments (IVI) standards, some of these
stacks can even provide high abstraction at the level of object-oriented programming.

All these protocols, standards and specifications are maintained by the IVI foundation. This
part will describe the logical structure of these hardware and software stacks, especially on
the python stack which we'll be using.

Installation and deployment


The python stack for interfacing instruments including packages such as PyVISA, python-
ivi, python-vxi, python-usbtmc, python-gpib. Some of these packages require external
and native components which are platform-specific.

This part would provide guidelines for install and deploy these software stacks to production
environments on various platforms.

Instrument control

Introduction 3
Introduction to Instrument Control Using Python

Using the PyVISA and related python packages, direct command control to the instrument
devices could be implemented, which is an approach that would work flawlessly with all
compatible instruments.

Utilizing the python-ivi library and a proper ivi driver, object-oriented programming level
interfacing could be achieved.

This part would give concrete examples on how to interface instruments using the two
different approaches mentioned above.

Python Version
Principally, both python 2 and python 3 could be used in the stack, as all the packages
involved in this book is 2 and 3 compatible.

Due to most binary python distributions on Windows platform are python 2 distributions, this
book would cover its code in python 2 syntaxes.

Introduction 4
Introduction to Instrument Control Using Python

Stack structure
An instrument interfacing hardware and software stack involves a number of standards to
work. Tools and libraries act as different implementations of these standards.

Hardware standards
General Purpose Interface Bus (GPIB)
Universal Serial Bus (USB)
USB Test and Measurement Class (USBTMC)
LAN/Ethernet
LAN eXtensions for Instrumentation
VXI-11 Protocol
Software standards
Virtual Instrument Software Architecture (VISA)
Common implementations:
NI-VISA by National Instruments
pyVISA as part of the python stack we use
Interchangeable Virtual Instruments (IVI)
Common implementations:
NI-IDL IVI Driver Library by National Instruments
python-ivi as part of the python stack we use

Most of the common-used instrument standards are maintained by the IVI foundation and
their specification collection could be accessed here.

Stack Structure 5
Introduction to Instrument Control Using Python

Installation and deployment


Depending on the underlying back-end that the actual python package would be using, the
installation of the python stack for interfacing instruments could require the installation of
native libraries and corresponding process may vary from platform to platform.

This part would provide guidelines for installation process on Linux, Windows and Mac
platforms using different back-ends.

Installation and deployment 6


Introduction to Instrument Control Using Python

Linux Installation

Python
Most Linux distributions may come along with python and pip already installed, if not,
install them first:

Arch Linux:

# pacman -S python2-pip

Debian/Ubuntu:

# apt-get install python-pip

CentOS/Fedora:

# yum install python-pip

PyVISA
If you wish to use PyVISA to perform command-based instrument programming, get
PyVISA installed first:

# pip install pyvisa

If you don't wish to install the stack system-wide, but only to install it in the local user scope,
--user flag could be appended to install subcommand and root permission could be

omitted.

$ pip install --user pyvisa

All python package installation follow the same guideline.

Now PyVISA needs a back-end to work, which you could have an option.

NI-VISA

Linux 7
Introduction to Instrument Control Using Python

You could choose to install VISA library provided by National Instruments as the back-end of
PyVISA. NI-VISA library for Linux is mainly packaged for RPM-based Linux distributions and
installation on these distributions such as CentOS, scientific Linux or Fedora would be easy.

For installation guidelines of NI-VISA, please refer to the National Instruments website.

pyvisa-py
PyVISA provides a pure python back-end which is free and open source. One who is not

able or convenient enough to install NI-VISA should turn to this choice.

# pip install pyvisa-py

pyvisa-py relies on a number of python packages for interface communication.

pyusb
For connecting to devices through USB, pyusb must be installed.

Install libusb first for pyusb to work

Arch linux:

# pacman -S libusb

Debian/Ubuntu:

# apt-get install libusb

CentOS/Fedora:

# yum install libusb

And then install pyusb

# pip install pyusb

python-gpib
For connecting to devices through GPIB, python-gpib must be installed.

Linux 8
Introduction to Instrument Control Using Python

This module is part of the linux-gpib driver, it also requires the kernel driver to function,
which could be installed through source code. Detailed installation guide could be found at
project site.

For Arch Linux users, this could be installed through AUR:

$ yaourt -S python2-gpib

pyserial
For connecting to devices through serial ports, pyserial must be installed.

# pip install pyserial

python-ivi
If you wish to use python-ivi to achieve object-oriented programming level instrument
control, install python-ivi package first.

# pip install python-ivi

python-ivi relies on multiple communication library to talk to different instrument interfaces.

python-vxi11
For communication using VXI-11 protocol over Ethernet/LAN interface, python-ivi package
requires python-vxi11 to run.

# pip install python-vxi11

python-usbtmc
For communication using USBTMC protocol over USB interface, python-usbtmc must be
installed.

# pip install python-usbtmc

python-gpib

Linux 9
Introduction to Instrument Control Using Python

For communication over GPIB interface, python-gpib and corresponding Linux kernel
space driver linux-gpib must be installed.

Please follow the previous metioned guidelines to install linux-gpib and its python
bindings.

Linux 10
Introduction to Instrument Control Using Python

Windows installation
Windows users are recommended to install a binary distribution of python stack before
install any packages that relates to instrument control.

A binary distribution that has the scipy stack pre-installed is strongly recommended.
Various instrument control packages relies on scipy stack to perform math operations.
Without a binary distribution, users may need to build scipy stack themselves.

The following links are a recommended list of scipy binary distributions.

Anaconda
Enthought Canopy
Python(x, y)
WinPython
Pyzo

This list is originally from scipy 's official website, and may be updated afterwards, please
refer to the site for most updated versions.

Please choose from the above list according to your preference, and install the distribution
following the official guidelines it provides.

Installation of packages
After installing your chosen distribution, following the official guidelines, you should gain your
access to your pip command within your installed distribution.

Just follow all the installation guidelines in the Linux installation section using pip to
installed your preferred packages of instrument control.

NOTE: linux-gpib and python-gpib are only for linux platforms, for those who wish to
use GPIB interface on Windows platform, your only option is to use PyVISA with NI-
VISA , together with another proper GPIB drvier for your GPIB controller device.

Windows 11
Introduction to Instrument Control Using Python

Instrument control
This part will use examples to describe how to achieve instrument control in practice.

Example sections will be divided to two parts, with first part showing the overall code listings,
and the second part describing each code lines step by step, to help understand the
example codes.

Communication protocol
As described in the structure chapter, different devices take different specifications of
communication protocols to talk to controllers. Due to the design of the communication
interfaces, devices all take a command-respond model to work. Most devices and vendors
design their device commands with compliance to the SCPI standard. But there are indeed a
few exceptions. For example, Keithley's System SourceMeter 2600 Series SMU devices
take a script syntax called Test Script Processor (TSP).

For convenience and to help understanding of the framework, the command-based


instrument control section will contain 2 examples, one featuring SCPI-based commands
and another featuring non-SCPI commands, using TSP to control Keithley's SMU.

Instrument Control 12
Introduction to Instrument Control Using Python

Command-based Instrument Control


This part would provide 2 examples using PyVISA package to implement command-based
instrument control.

One of the example would be featuring non-SCPI based commands, taking TSP commands
for an instance, while another would be featuring SCPI-based commands.

Command-based 13
Introduction to Instrument Control Using Python

SCPI Devices
This part will contain the example to control an SCPI deivce.

The example script would concentrate on operating a Keithley's System SourceMeter


2400 Series SMU device to measure the I-V characteristic diagram of a diode.

Example code
import visa
import numpy as np
from matplotlib import pyplot as plt
from time import sleep

addr = 'TCPIP0::192.168.1.2::INSTR'
start = -5.0
end = 5.0
num = 100
limiti = 0.1
delay = 0.01

rm = visa.ResourceManager("@py")
ins = rm.get_instrument(addr)
V = np.linspace(start, end, num)

ins.write('*RST')
ins.write(':SOUR:FUNC VOLT')
ins.write(':SENS:CURR:PROT {}'.format(limiti))
ins.write(':SENS:FUNC CURR')
ins.write(':SENS:CURR:RANG {}'.format(limiti))

ins.write(':OUTP ON')

I = []
for i in V:
ins.write(':SOUR:VOLT:LEV {}'.format(i))
sleep(delay)
cur = float(ins.ask(':READ?'))
I.append(cur)

ins.write(':OUTP OFF')

plt.plot(V, I)
plt.show()

SCPI devices 14
Introduction to Instrument Control Using Python

Detailed comments

SCPI devices 15
Introduction to Instrument Control Using Python

Non-SCPI Devices
This part will contain the example to control an non-SCPI deivce. We would take Keithley's
TSP commands as example non-SCPI command system.

The example script would concentrate on operating a Keithley's System SourceMeter


2600 Series SMU device to measure the I-V characteristic diagram of a diode.

Example code
import visa
import numpy as np
from matplotlib import pyplot as plt
from time import sleep

addr = 'TCPIP0::192.168.1.2::INSTR'
start = -5.0
end = 5.0
num = 100
limiti = 0.1
delay = 0.01

rm = visa.ResourceManager("@py")
ins = rm.get_instrument(addr)
V = np.linspace(start, end, num)

ins.write('reset()')
ins.write('smua.source.func = smua.OUTPUT_DCVOLTS')
ins.write('smua.source.limiti = {}'.format(limiti))
ins.write('smua.measure.rangei = {}'.format(limiti))

ins.write('smua.source.output = smua.OUTPUT_ON')

I = []
for i in V:
ins.write('smua.source.levelv = {}'.format(i))
sleep(delay)
cur = float(ins.ask('print(smua.measure.i())'))
I.append(cur)

ins.write('smua.source.output = smua.OUTPUT_OFF')

plt.plot(V, I)
plt.show()

non-SCPI devices 16
Introduction to Instrument Control Using Python

Detailed comments

non-SCPI devices 17

You might also like