Skip to content

Instantly share code, notes, and snippets.

@samclane
Last active March 8, 2022 14:45
Show Gist options
  • Save samclane/c0fb90219214361cb963674b3bc82848 to your computer and use it in GitHub Desktop.
Save samclane/c0fb90219214361cb963674b3bc82848 to your computer and use it in GitHub Desktop.
Gets the current wave from the oscilloscope and turns it into a .wav file
#!/usr/bin/python
"""
Download data from a Rigol DS1052E oscilloscope channel 1 and
dump to a .wav file.
By Ken Shirriff, http://righto.com/rigol
Modified by Sawyer McLane 2/28/2019
"""
import sys
import visa
import wave
import numpy
import time
rm = visa.ResourceManager()
# Get the USB device, e.g. 'USB0::0x1AB1::0x0588::DS1ED141904883'
instruments = rm.list_resources()
usb = [x for x in instruments if "USB" in x]
if len(usb) != 1:
print('Bad instrument list', instruments)
sys.exit(-1)
scope = rm.open_resource(usb[0])
scope.values_format.use_ascii('x', '$', numpy.array)
scope.timeout = 25e3
# Grab the raw data from channel 1
# scope.write(":STOP")
scope.write(":WAV:POIN:MODE RAW")
scope.write(":WAV:DATA? CHAN1")
rawdata = scope.read_raw()[:int(1e7)]
data_size = len(rawdata)
sample_rate = float(''.join(scope.ask_for_values(':ACQ:SAMP?')[0]))
print('Data size:', data_size, "Sample rate:", sample_rate)
scope.write(":KEY:FORCE")
scope.close()
# Dump data to the wav file
wav_file = wave.open("./audio/{}.wav".format(str(int(time.time()))), "w")
nchannels = 1
sampwidth = 1
comptype = "NONE"
compname = "not compressed"
wav_file.setparams((nchannels, sampwidth, sample_rate, data_size,
comptype, compname))
# Data will be written inverted
wav_file.writeframes(rawdata)
wav_file.close()
@josiahsinclair
Copy link

Has pyvisa deprecated values_format attribute? When I run your script I get the following error: "AttributeError: 'USBInstrument' object has no attribute 'values_format'"

@samclane
Copy link
Author

samclane commented Aug 9, 2021

Has pyvisa deprecated values_format attribute? When I run your script I get the following error: "AttributeError: 'USBInstrument' object has no attribute 'values_format'"

Yes, I think you're correct.

I haven't run this code in a while, but looking at the PyVISA docs, I can only find reference to values_format when looking at deprecated versions.

@Chitres
Copy link

Chitres commented Mar 2, 2022

Hello samclane

I had two questions:

  1. What's the alternative to values_format?
  2. Will this work for other RIGOL scopes like DS6064?

@samclane
Copy link
Author

samclane commented Mar 5, 2022

Hello samclane

I had two questions:

  1. What's the alternative to values_format?
  2. Will this work for other RIGOL scopes like DS6064?
  1. I haven't used pyvisa in quite a while, so I'm not sure right now. This script is deprecated as of now, but it's probably worth me taking a look at since this script is of use to a few other people

  2. Again, I'm not sure. I think it should, as VISA sits at the driver layer and should be hardware agnostic.

@josiahsinclair
Copy link

Hi Samclane and Chitres,

I am not sure what the new way to set the values format is on Rigol Oscillopes, but I have found that without it, you just get data in binary format, which you can convert to voltage by querying the scope for the channel y-scale and y-offset. I've posted a jupyter-notebook which works for me:

https://github.com/josiahsinclair/RigolScopeWaveformCapture.git

Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment