How to save Data from Oscilloscope using Python in Linux

Oscilloscope data taking using PythonThis is a general article for connecting any data taking instrument to the computer using python. There are two frameworks for connection namely NI-visa developed by National Instrumentation and PyVISA-py developed by Python itself. In this article, we will describe how to connect Tektronix Oscilloscope to PC via Ethernet or USB using PyVISA-py in Linux.  After connection, we have explained seamless data logging  from oscilloscope.

Library Prerequisites in Linux

Here we have used fedora 28 for this tutorial, however we have also tested this method in Ubuntu 16.04 and 18.04. We will be needing python libraries which can be installed using yum, apt-get or pip depending on the Linux distribution.

Python module installation in fedora 28

sudo yum install python-pip
sudo pip install pyusb
sudo pip install pyvisa-py
sudo yum install python-matplotlib
sudo yum install python-scipy
sudo pip install numpy

Python module installation in Ubuntu

sudo apt-get install python-pip
sudo pip install pyvisa-py
sudo pip install pyusb
sudo pip install matplotlib
sudo pip install scipy
sudo pip install numpy

In order to get data from oscilloscope using python program, we need to establish connection to our device either using USB or Ethernet LAN. The method to connect Tektronix Oscilloscope to PC is discussed in the following section. The IP address or the USB port address of our device will be needed in the Python program in order to establish connection.

Connection of Oscilloscope to Computer

How to connect Tektronix Oscilloscope to PC via Ethernet

A step by step tutorial to connect Tektronix Oscilloscope to PC via Ethernet has been explained in previous post. In our case, we have set the IP address of oscilloscope  to which will be used in python program for establishing connection.

Note: the IP address of the PC should be similar for example but not same as IP address of oscilloscope and the device IP address should be called in python program.

How to connect Tektronix Oscilloscope to PC via USB

The Tektronix Oscilloscope can be connected to PC via USB type B which is shown is following figure.

USB Type A to Type B connector
USB Type A to Type B connector
Connecting USB Type B to Oscilloscope
Connecting USB Type B to Oscilloscope

After connecting type A of the USB to the PC and type B to the Oscilloscope, we need to obtain the USB port address of the Oscilloscope which is fixed and can’t be changed like LAN IP address.

How to obtain USB port address of Oscilloscope
  1. Press Utility button and you will find utility menu at the bottom of screen.Oscilloscope Utility ButtonUtility
  2. Press button corresponding to Utility Page and you will find a menu bar on the left side of the screen.Utility Page
  3. Using “Multipurpose a” knob, select I/O.Input Output
  4. Press button corresponding to USB which will open a USB Device Port menu on the right of the screen.Oscilloscope USB Device Port
  5. Press button corresponding to USBTMC Configuration which will open a window showing you the port address of USB. This IDs and serial number should be noted because it will be used in the python program in establishing connection to Oscilloscope. Oscilloscope USB Port address

Testing Connection of Oscilloscope

After connecting your device using USB or Ethernet, you can check the connection in python as following.

Opening python in terminal with superuser permission.

sudo python

After this python environment will open showing python version and now you can enter python commands.

Now we will import PyVisa-Py module.

>>> import visa

Let’s call Resource Manager using PyVisa-Py library.

>>> rm = visa.ResourceManager('@py')

One can use visa.ResourceManager(‘@ni’)  for calling NI-Visa. If it’s called without argument, it will use NI-Visa by default.

In case your Resource manager is unable to locate visa library, you can specify the path as follows

rm = ResourceManager('Path to library') # Example: /usr/lib/

Calling Oscilloscope’s IP address ( in case of LAN connection.

>>> scope = rm.open_resource('TCPIP::')

Calling Oscilloscope using USB port address. Use USB ID’s and Serial Number obtained in the previous section.

scope = rm.open_resource('USB0::{Vendor ID}::{Model ID}::{Serial Number}::INSTR')
>>> scope = rm.open_resource('USB0::0x0699::0x0409::C010730::INSTR')

Checking the connection by reading and writing.

>>> scope.query("*IDN?")

Which is same as:

>>> scope.write("*IDN?")
>>> print(

This will print instrument model number and other information.

TEKTRONIX,MSO4104B,C010730,CF:91.1CT FV:v1.24

If you have reached this far and have obtained your instrument information, Congratulations! The connection has been established and now, we can move for instrument controlling and data acquisition.

We have explained only basic commands of PyVisa-Py  library according to our use, however you can read the PyVisa-Py  manual for detailed information.

Data Acquisition Python Programs

The data from oscilloscope can be read in two ways: ASCII and Binary. ASCII is slow, but human readable where as Binary is faster. Its better to read data in Binary, which can be converted into analyzable form later. For this method, one must know the structure of the binary data.

Program for Single Channel of Oscilloscope

The following program take data from channel 1 of the oscilloscope and plots it. You can save this program as and run it using python by typing command sudo python in terminal.

import visa
import numpy as np
from struct import unpack
import pylab

# Establish Connection
rm = visa.ResourceManager('@py') # Calling PyVisaPy library
#scope = rm.open_resource('USB0::0x0699::0x0409::C010730::INSTR') # Connecting via USB
scope = rm.open_resource('TCPIP::') # Connecting via LAN 
# Setting source as Channel 1
scope.write('DATA:SOU CH1') 
scope.write('DATA:WIDTH 1') 
scope.write('DATA:ENC RPB')

# Getting axis info
ymult = float(scope.query('WFMPRE:YMULT?')) # y-axis least count
yzero = float(scope.query('WFMPRE:YZERO?')) # y-axis zero error
yoff = float(scope.query('WFMPRE:YOFF?')) # y-axis offset
xincr = float(scope.query('WFMPRE:XINCR?')) # x-axis least count

# Reading Binary Data from instrument
data = scope.read_raw() # Reading binary data
headerlen = 2 + int(data[1]) # Finding header length
header = data[:headerlen] # Separating header 
ADC_wave = data[headerlen:-1] # Separating data

# Converting to Binary to ASCII
ADC_wave = np.array(unpack('%sB' % len(ADC_wave),ADC_wave))

Volts = (ADC_wave - yoff) * ymult + yzero
Time = np.arange(0, xincr * len(Volts), xincr)

# Plotting Volt Vs. Time
pylab.plot(Time, Volts)

After running this program, you will get the plot of current data from oscilloscope. Here, we have shown plot of pulse from plastic scintillator.

Plot Oscilloscope Waveform Python
Plot of Oscilloscope Waveform using Python

If you like this tutorial, please share with someone who is interested. You can follow us on facebook and twitter. You can subscribe us for Email Notification also to get an email whenever we publish a new post.

Leave a Reply

Your email address will not be published. Required fields are marked *