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.

28 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?)

  5. CH says:

    I used the code from the git repo.
    When I ran it, it seems that ‘data’ is 10 items larger than ‘time’,
    It seems the first 10 entires in the data list might not be proper data,
    since they7 don’t seem to change; Could these possibly be something
    else? ranges perhaps?

    • mahto says:

      Oh, that is totally possible. I got frustrated because the whole thing was pretty fragile (sending an improper command could crash the scope, etc), and haven’t really played with it much since- I’d be interested to know if you make any progress though, since I might need to make some automated test rigs that use them in the near future.

    • Mike says:

      I just had the same issue with the 10 extra data points and I have started to work on fixing it. I added an offset to the numpy.frombuffer() command. Check out my fork at github. I just started on this today and I’m not quite sure where it will take me.

      @mahto: This is a great start, thanks for your work setting this up.

      • mahto says:

        @Mike Thanks! Good luck getting it set up- I just thought of a new project to use the code for (a live scope trace with a little character that runs and jumps across the screen as new data comes in), so I might be motivated to get back into this as well :-)

  6. Hamid Ohadi says:

    Thank you for your script. It got me started. I have a question though. Have you got this scope to capture in RAW mode? I can’t get more than 600 datapoints out of it and I’m using the latest 2.04 firmware. In the manual it says you could potentially capture 5k points. I tried:

    :WAV:POIN:MODE RAW
    :ACQ:MEMD LONG

  7. NathanW says:

    Hey this was great. Thanks for publishing your code. I used your first class to work with a Tektronix 2024C scope with no problems. It only took me a few hours searching to find you :-)

    Great work!

  8. Cullen Newsom says:

    line 41 of realtimechart.py

    data = (data – 130.0 – voltoffset/voltscale*25) / 25 * voltscale + voltoffset

    I also had to
    plot.plot(t1[0:600], d1[0:600])
    plot.plot(t2[0:600], d2[0:600])

    Which gives me a slightly bogus plot.

    Many thanks for posting this.

  9. Pingback: Spiking Circuits » Automatic analog measurements using a Rigol oscilloscope » Spiking Circuits

  10. Frnak McKenney says:

    My thanks toeveryone for the all information.

    I’m working with a DS1102E(fw v00.02.06), and am still trying to figure out how determine how much data will be coming back from a :WAV command.

    Also, coding in C, it appears that a read() requesting more than 2148 bytes (0×0864) , at least in NORM mode, will cause the Rigolto lock up completely, leaving oddly repeating bit patterns on the screen. Cycling power clears the problem ( but a RESET button would be really nice. )

    Given this restriction, can anyone suggest how to retreive the 1M data points supposedly available in RAW mode?

    ( Can anyone suggest why 2148 is a “magic” number in this context? Or am I the only one who is seeing this limitation? )

  11. Skye Sweeney says:

    One issue I am having talking with my Rigol is one of pacing. You need to wait for a command to finish before you send the next. With other equipment there is often a command you can send that will only return when the scope is ready to accept a ‘real’ command. Such a command is not documented in the programmer’s guide for my D1102E. Has anyone found a workaround?

  12. Matthias says:

    Hi,

    for all those who are having problems with the scope hanging while reading more data: There is a series of patches waiting in linux-next (that is Kernel 3.11) which will address the issues:

    https://git.kernel.org/cgit/linux/kernel/git/next/linux-next.git/log/drivers/usb/class/usbtmc.c

    I have not yet tested them myself but my hopes are high ;-D

  13. Mike Alport says:

    I am trying to interface to the Rigol DS 1102E Oscilloscope under OSX 10.9 using these routines but I get the error:
    OSError: [Errno 2] No such file or directory: ‘/dev/usbtmc0′

    As I understand it correctly, instrument.py should function as the USB driver but if I do a:
    ls /dev/tty.*
    the new USB port does not appear.

    Any suggestions would be appreciated.

    Thanks,

    Mike Alport

  14. Gordy says:

    I am running Linux Mint 16 (64bit) and I would like to know how to run this Python program that has ben written. I do know that Python has been installed in Mint. I am no programmer and need some help using this program.

    Thank you for any reply
    Gordy

  15. Pingback: Rigol DS1052E (50MHz Digitales Speicher-Oszilloskop, 2 Kanäle, USB Storage Access) | Linux Hardware Guide

  16. Pingback: My Raspberry Pi talks to my Oscilloscope | We Saw a Chicken …

  17. Pingback: Rigol DS1052E (50MHz Digital Oscilloscope, 2 Channels, USB Storage Access) | Linux Hardware Guide.com

  18. Dan Stahlke says:

    I managed to get deep memory (a million samples) to work. It seems to be necessary to use the “single” trigger sweep mode. Code is at https://github.com/dstahlke/rigol_long_mem

  19. Alejandro Santos says:

    You can listen to AM radio with your scope, by writing a GNURadio plugin.

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>