Tektronix Technical Forums are maintained by community involvement. Feel free to post questions or respond to questions by other members. Should you require a time-sensitive answer, please contact your local Tektronix support center here.

Python MSO64 2-byte read binary waveform example

Programming examples for remote communication with instruments
C/C++, C#, Python, Matlab, LabVIEW
Post Reply
RonFred
Posts: 2
Joined: October 5th, 2020, 8:26 pm
Country: United States

Python MSO64 2-byte read binary waveform example

Post by RonFred » October 5th, 2020, 9:08 pm

I just started using an MSO64 scope and found that my 1-byte waveform read examples produced noticeable error results. I thought I would share my working example here for a 2-byte binary waveform read and plot. Readers can view the two discussion threads I used to merge my example together. Thank you Carl M and Syl.

Code: Select all

# -*- coding: utf-8 -*-
"""
Title: Python 2-byte read binary waveform plot example
Created on Mon Oct  5 18:56:27 2020

Tested on Tek MSO64 scope

Discussion threads used:
    
    Python-Tek DPO 400 series: read binary waveform with 2 bytes
    https://forum.tek.com/viewtopic.php?t=138488
    
    Python: MDO Simple Plot
    https://forum.tek.com/viewtopic.php?t=138684

@author: BiophysicsLab.com
"""
import visa
import numpy as np
# from struct import unpack
import pylab

visa_address = 'TCPIP0::192.168.1.32::inst0::INSTR'
rm = visa.ResourceManager()
scope = rm.open_resource(visa_address)

scope.timeout = 10000 # ms
scope.encoding = 'latin_1'
scope.read_termination = '\n'
scope.write_termination = None
scope.write('*cls') # clear ESR
scope.write('header OFF') # disable attribute echo in replies
print(scope.query('*idn?'), end='\n\n')

scope.write('DATA:SOU CH1')
scope.write('DATA:WIDTH 2')
scope.write('DATA:ENC SRI')

ymult = float(scope.query('WFMPRE:YMULT?'))
yzero = float(scope.query('WFMPRE:YZERO?'))
yoff = float(scope.query('WFMPRE:YOFF?'))
xincr = float(scope.query('WFMPRE:XINCR?'))

xzero = float(scope.query('WFMPRE:XZERO?'))
pre_trig_record = int(scope.query('WFMPRE:PT_OFF?')) 

scope.write('CURVE?')
data = scope.read_raw()

# error checking
r = int(scope.query('*esr?'))
print('event status register: 0b{:08b}'.format(r))
r = scope.query('allev?').strip()
print('all event messages: {}'.format(r))

# disconnect
scope.close()
rm.close()

headerlen = 2 + int(data[1])
header = data[:headerlen]
ADC_wave = data[headerlen:-1]
ADC_wave = np.frombuffer(ADC_wave, dtype=np.int16)

Volts = (ADC_wave - yoff) * ymult  + yzero

total_time = xincr * len(Volts)
t_start = (-pre_trig_record * xincr) + xzero
t_stop = t_start + total_time
Time = np.linspace(t_start, t_stop, num=len(Volts), endpoint=False)

pylab.plot(Time, Volts, linewidth=1)
pylab.title('channel 1') # plot label
pylab.xlabel('time (seconds)') # x label
pylab.ylabel('voltage (volts)') # y label
pylab.grid(b=True, which='major', color='gray', linestyle='--', alpha=0.7)
pylab.grid(b=True, which='minor', color='gray', linestyle=':', alpha=0.4)
pylab.minorticks_on()
pylab.show()
Here is a plot using the 2-byte code listed above from the output of a DHT22 temperature/humidity sensor
http://www.biophysicslab.com/wp-content ... window.jpg

Here is a similar plot using 1-byte read binary waveform code (see the two discussion threads in code listing above for examples)
http://www.biophysicslab.com/wp-content ... window.jpg

Post Reply

Return to “Remote Instrument Communication (Programming) Examples”

Who is online

Users browsing this forum: No registered users and 1 guest