Skip to content

Instantly share code, notes, and snippets.

Last active October 19, 2023 16:18
Show Gist options
  • Save idt12312/a9d9a600a100b7d0ad121114d1d4cf5d to your computer and use it in GitHub Desktop.
Save idt12312/a9d9a600a100b7d0ad121114d1d4cf5d to your computer and use it in GitHub Desktop.
Load waveform and save it as csv file for RIGOL MSO5000 series oscilloscope
import visa
import argparse
import csv
def load_waveform(chidx, points_request):
inst.write(f':WAV:SOUR CHAN{chidx}')
inst.write(':WAV:MODE RAW')
inst.write(f':WAV:POIN {points_request}')
inst.write(':WAV:FORM BYTE')
preample = inst.query(':WAV:PRE?').split(',')
points = int(preample[2])
xinc = float(preample[4])
xorg = float(preample[5])
xref = float(preample[6])
yinc = float(preample[7])
yorg = float(preample[8])
yref = float(preample[9])
print(f'loading CH{chidx} {points}pts')
data_bin = inst.query_binary_values('WAV:DATA?', datatype='B', container=bytes)
t = [(float(i) - xref)*xinc + xorg for i in range(points)]
v = [(float(byte_data) - yref)*yinc + yorg for byte_data in data_bin]
return t,v
argparser = argparse.ArgumentParser()
argparser.add_argument('-a', '--address', required=True, help='VISA address like "TCPIP::{ipaddress}::INSTR"')
argparser.add_argument('-o', '--output', help='Output file name (default: "waveform.csv")', default='waveform.csv')
argparser.add_argument('-p', '--points', type=int, help='Points of the data to be load', default=1000)
argparser.add_argument('-c', '--chlist', help='Specify channels which waveforms will be load from like "-c 1,2,3,4"', default='1')
args = argparser.parse_args()
chlist = [int(x) for x in args.chlist.split(',')]
inst = visa.ResourceManager().open_resource(args.address)
load_data = {}
for chidx in chlist:
t, v = load_waveform(chidx, args.points)
if 'time' not in load_data.keys():
load_data['time'] = t
load_data[f'CH{chidx}'] = v
with open(args.output, 'w', newline='') as f:
writer = csv.writer(f, delimiter=',')
Copy link

Super helpful, thanks for putting this up! Found a minor bug, in load_waveform line 22 and 23, xorg and yorg should be multiplied by x and yinc.

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