Controlling a Rigol oscilloscope using Linux and Python

Rigol DS1052E Oscilloscope

After many frustrated nights trying to debug electronics projects blindly (the analog scope is wayyyy too much work to pull off the shelf and use), I decided it was time to spring for a digital storage oscilloscope. Since I had read many good things about them, I chose the Rigol DS1052E, which is a two channel 50 MHz scope that can be easily modded to work at 100 MHz. The scope is way smaller and lighter than I expected, and has a nice set of buttons that give it the feel of a quality instrument. It works perfectly well as a standalone device, however since it has a USB slave port that implements the usbtmc interface, it turned out to be pretty easy to control using Linux. After a night of coding (most of which involved re-learning Python), I was able to grab data from the device and plot it:

Controlling the scope from Linux

This is all well and good, and should be particularly useful for my engineering pursuits, however it has me thinking about the artistic possibilities of a high-speed data acquisition device. I’m not sure exactly what I will do with it yet, but I’m thinking scope+processing or scope+pd could lead to some interesting possibilities. Any suggestions?

Source code for the current, boring use after the break.

First, a rudimentary library for accessing the usbtmc driver. After I become more familiar with what I want from the device, I may turn this into an actual interface- for now, it just exposes the devices programming interface. Name it instrument.py:

import os
 
class usbtmc:
    """Simple implementation of a USBTMC device driver, in the style of visa.h"""
 
    def __init__(self, device):
        self.device = device
        self.FILE = os.open(device, os.O_RDWR)
 
        # TODO: Test that the file opened
 
    def write(self, command):
        os.write(self.FILE, command);
 
    def read(self, length = 4000):
        return os.read(self.FILE, length)
 
    def getName(self):
        self.write("*IDN?")
        return self.read(300)
 
    def sendReset(self):
        self.write("*RST")
 
 
class RigolScope:
    """Class to control a Rigol DS1000 series oscilloscope"""
    def __init__(self, device):
        self.meas = usbtmc(device)
 
        self.name = self.meas.getName()
 
        print self.name
 
    def write(self, command):
        """Send an arbitrary command directly to the scope"""
        self.meas.write(command)
 
    def read(self, command):
        """Read an arbitrary amount of data directly from the scope"""
        return self.meas.read(command)
 
    def reset(self):
        """Reset the instrument"""
        self.meas.sendReset()

And here is an example program that uses the library to grab the waveform from Channel 1 and graph it:

#!/usr/bin/python
import numpy
import matplotlib.pyplot as plot
 
import instrument
 
""" Example program to plot the Y-T data from Channel 1"""
 
# Initialize our scope
test = instrument.RigolScope("/dev/usbtmc0")
 
# Stop data acquisition
test.write(":STOP")
 
# Grab the data from channel 1
test.write(":WAV:POIN:MODE NOR")
 
test.write(":WAV:DATA? CHAN1")
rawdata = test.read(9000)
data = numpy.frombuffer(rawdata, 'B')
 
# Get the voltage scale
test.write(":CHAN1:SCAL?")
voltscale = float(test.read(20))
 
# And the voltage offset
test.write(":CHAN1:OFFS?")
voltoffset = float(test.read(20))
 
# Walk through the data, and map it to actual voltages
# First invert the data (ya rly)
data = data * -1 + 255
 
# Now, we know from experimentation that the scope display range is actually
# 30-229.  So shift by 130 - the voltage offset in counts, then scale to
# get the actual voltage.
data = (data - 130.0 - voltoffset/voltscale*25) / 25 * voltscale
 
# Get the timescale
test.write(":TIM:SCAL?")
timescale = float(test.read(20))
 
# Get the timescale offset
test.write(":TIM:OFFS?")
timeoffset = float(test.read(20))
 
# Now, generate a time axis.  The scope display range is 0-600, with 300 being
# time zero.
time = numpy.arange(-300.0/50*timescale, 300.0/50*timescale, timescale/50.0)
 
# If we generated too many points due to overflow, crop the length of time.
if (time.size > data.size):
    time = time[0:600:1]
 
# See if we should use a different time axis
if (time[599] < 1e-3):
    time = time * 1e6
    tUnit = "uS"
elif (time[599] < 1):
    time = time * 1e3
    tUnit = "mS"
else:
    tUnit = "S"
 
# Start data acquisition again, and put the scope back in local mode
test.write(":RUN")
test.write(":KEY:FORC")
 
# Plot the data
plot.plot(time, data)
plot.title("Oscilloscope Channel 1")
plot.ylabel("Voltage (V)")
plot.xlabel("Time (" + tUnit + ")")
plot.xlim(time[0], time[599])
plot.show()
This entry was posted in tech and tagged , , . Bookmark the permalink.

5 Responses to Controlling a Rigol oscilloscope using Linux and Python

  1. Pingback: Hotsolder » Blog Archive » Rigol DS1052E or DS1102E Linux Software (link)

  2. Al W says:

    Nice work. I linked to this post on my site which has a lot of stuff about these scopes. You might mention that you need matplotlib for this to work (I’m a C guy, not a Python guy ;-) ). Easy enough to install with the package manager under Kubuntu, though, so no sweat.

    Oh. And in my case, I had to set my permissions on /dev/usbtmc0, but that’s easy. Probably ought to go fix up udev to set the 666 permissions by default, but for the moment I’m too lazy ;-)

    Now what would be interesting is to marry this to gnuplot. Have a look at: http://www.hotsolder.com/2010/04/analog-chart-recorder-in-shell-script.html

    Maybe that’s my weekend project if you don’t beat me to it!

    Thanks for the post.

    • mahto says:

      Thanks! Good point about matplotlib- I’m a casual Python user, and can’t keep track of which libraries I’ve pull edfrom a package manager. I’m actually still considering switching the whole thing over to C, so that I have a better chance of using it with other environments such as the aforementioned Processing.

      For udev, I added a new rules file (called 40-usbtmc-permissions.rules) to /etc/udev/rules.d, containing this line:

      ATTRS{idVendor}==”1ab1″, ATTRS{idProduct}==”0588″, MODE=”0660″, GROUP=”usbtmc”

      Of course, I also had to create the group usbtmc, and add myself to it. Perhaps I’ll write better instructions for that :-) .

      Go for the gnuplot integration- I actually wrote the example using gnuplot first, but then switched it to matplotlib since the interface was nicer from Python. Feel free to write a commandline aware client for the driver :-D .

      If you want to work on the project, you can grab the latest version here: http://github.com/cibomahto/pyusbtmc

  3. elliot says:

    This causes my DS1052E to lock up, am I doing something wrong?

  4. mahto says:

    @elliot Probably not, the scopes seem pretty unstable- what version of the firmware do you have running? It works fine for me using 00.02.02 SP2, but it wouldn’t work for me with the newest firmware (2.04?)

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">