Skip to content

Instantly share code, notes, and snippets.

@steveway
Created November 22, 2024 14:53
Show Gist options
  • Select an option

  • Save steveway/fbdd6be4c572919d45460cf3114abdf7 to your computer and use it in GitHub Desktop.

Select an option

Save steveway/fbdd6be4c572919d45460cf3114abdf7 to your computer and use it in GitHub Desktop.
This should get the Waveform Data from the new Rigol DHO devices and formats it to be identical to saved .csv files from the device itself.
#! /usr/bin/env python3
import pyvisa as visa
from serial.serialutil import SerialException
from decimal import Decimal, getcontext
def main():
# Get list of connected devices
rm = visa.ResourceManager()
device_list = {}
for device_name in rm.list_resources():
print(device_name)
device_id = ""
try:
device_id = rm.open_resource(device_name).query("*IDN?")
except (visa.VisaIOError, FileNotFoundError, SerialException):
pass
print(f"device_id: {device_id}")
if device_id.lower().startswith("rigol"): # Add only devices which names start with Rigol
if device_name not in device_list:
device_list[device_name] = rm.open_resource(device_name)
print(f"device_list: {device_list}")
for name in device_list.keys():
getcontext().prec = 36
device = device_list[name]
device.write(":WAVEform:SOURCE CHANnel1")
device.write(":WAVEform:MODE NORMAL")
time_inc = Decimal(device.query(":WAVEform:XINCrement?"))
y_inc = float(device.query(":WAVEform:YINCrement?"))
y_origin = float(device.query(":WAVEform:YORigin?"))
y_ref = float(device.query(":WAVEform:YREFerence?"))
print(f"y_inc: {y_inc}")
print(f"y_origin: {y_origin}")
print(f"y_ref: {y_ref}")
print(f"time_inc: {time_inc}")
# device.timeout = 30000
used_format = "ASCii"
device.write(f":WAVEform:FORMat {used_format}")
if used_format == "WORD" or used_format == "BYTE":
buf = device.query_binary_values(':WAVEform:DATA?', datatype='H')
elif used_format == "ASCii":
buf = device.query_ascii_values(":WAVEform:DATA?")
filename = "Rigol_Trace_{}.csv".format(name.split("::")[3])
print(f"filename: {filename}")
position = Decimal(device.query(":WAVEform:XORigin?").strip())
with open(filename, 'w') as f:
f.write("Time(s),CH1V\n")
for point in buf:
if point:
print(f"point: {point}")
if used_format == "WORD" or used_format == "BYTE":
voltage = (point - y_origin - y_ref) * y_inc
voltage_string = format(voltage, ".6e")
elif used_format == "ASCii":
voltage_string = format(point, ".6e")
position_string = format(position, ".6e").replace("e-", "e-0").replace("e+", "e+0")
f.write(f"{position_string},{voltage_string}\n")
position = position + time_inc
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment