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 3: Save Waveform Datas to PC, PyVisa - 3/4 Series MDO

Programming examples for remote communication with instruments
C/C++, C#, Python, Matlab, LabVIEW
Post Reply
zhuyu4839
Posts: 1
Joined: March 13th, 2020, 5:42 pm
Country: China

Python 3: Save Waveform Datas to PC, PyVisa - 3/4 Series MDO

Post by zhuyu4839 » March 13th, 2020, 6:12 pm

Code: Select all



#-------------------------------------------------------------------------------
# Name:  Save Waveform Datas to PC, PyVisa - 3/4 Series MDO Scopes
# Purpose:  This example demonstrates how to save a waveform data from
#   the scope to the PC.
#
# Created:  2020-03-14
#
# Development Environment: Python 3.7, PyVisa 1.10, NI-VISA 2019, Windows 7
#
# Compatible Instruments: MDO 3000 & 4000 Series Oscilliscopes
#
# Compatible Interfaces:  USB, Ethernet, GPIB
#
# Tektronix provides the following example "AS IS" with no support or warranty.
#
#-------------------------------------------------------------------------------

import visa # https://pyvisa.readthedocs.org/en/stable/

# Modify the following lines to configure this script for your instrument
#==============================================
visaResourceAddr = 'visa resource'

#==============================================
rm = visa.ResourceManager()
scope = rm.open_resource(visaResourceAddr)

print(scope.query('*IDN?'))


scope.write('HEADer OFF')
rec_len = int(scope.query('HORizontal:RECOrdlength?'))  #record length

scope.write('DATa:SOUrce CH1')
scope.write('DATa:START {}'.format(1))
scope.write('DATa:STOP {}'.format(rec_len))

#data_point = scope.query('WFMOutpre:NR_pt?')
x_unit = scope.query('WFMOutpre:XUNit?')
x_zero = float(scope.query('WFMOutpre:XZEro?'))
x_incr = float(scope.query('WFMOutpre:XINcr?'))

y_unit = scope.query('WFMOutpre:YUNit?')
y_zero = float(scope.query('WFMOutpre:YZEro?'))
y_offset = float(scope.query('WFMOutpre:YOFF?'))
y_mult = float(scope.query('WFMOutpre:YMUlt?'))
b_num = scope.query('WFMOutpre:BYT_nr?')

scope.write('CURVE?')
data = scope.read_raw()    # get point data

# the data start '#' because of scope.write('HEADer OFF')
data_start = data[0]
assert 0x23 == data_start, 'error start of data'

# the digits of point length, hex to number
len_digits = data[1] - 0x30
print('number of data length is %d', len_digits)

points_num = 0
for i in range(len_digits):
    #### calculator the actual length of data
    points_num += (10**(len_digits - i - 1)) * (data[i+2] - 48)
assert int(points_num) == rec_len, 'error data length, not equal "points(%d)"'%rec_len

data_len = len(data)

## the data end of 0x0a
data_end = data[data_len - 1]
assert 0x0a == data_end, 'error end of data'

## the x-axis data, start with unit
data_x = [x_unit]
## the y-axis data, start with unit
data_y = [y_unit]

## Complement code tuple
_COMPLEMENT = [i if i < 0x80 else -(0xff-i+1) for i in range(0xff+1)]

###Exclude '#', digits and last 0x0a
x = len_digits + 2 + 1
for i in range(data_len - x):
    data_i = data[i + x]
    data_x.append(x_zero + x_incr * i) 
    data_y.append(y_zero + y_mult * _COMPLEMENT[data_i])

print(data_x)
print(data_y)

scope.close()
rm.close()

Carl M
Tektronix Applications
Tektronix Applications
Posts: 800
Joined: July 30th, 2008, 4:45 pm
Country: United States

Re: Python 3: Save Waveform Datas to PC, PyVisa - 3/4 Series MDO

Post by Carl M » June 9th, 2020, 5:26 am

i don't understand the context for this code. Why did you write it? What engineering challenge are you trying to solve?
  • Why not use Numpy for efficient and easy to use vector objects?
  • you omit setting timeouts, endlines, and character encoding. your code cannot be robust without these properly configured.
  • you never check or initialize the event status register. I could not be successful with remote instrumentation without these tools; *esr?, *cls
  • pyvisa has method for reading binary block data; https://pyvisa.readthedocs.io/en/1.8/rv ... ary-values
  • you typecast data via conditional statements and arithmetic. there are more straightforward and efficient means to do this but would be unnecessary if you use Numpy and pyvisa method query_binary_values()
  • scaling operations are done in a loop. matrix math via Numpy will scale better.
My MDO Simple Plot example, viewtopic.php?t=138684, works very well with 3 and 4 series instruments and addresses all the above. The Default Setup, Autoset, Single, and plotting parts of the example can be omitted if all you requires is scaled vector data.

Best of luck with your testing.
Tektronix Application Engineer

Post Reply

Return to “Remote Instrument Communication (Programming) Examples”

Who is online

Users browsing this forum: No registered users and 2 guests