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 Example: AFG3000 create and send waveform

Questions in this forum area are community supported. Tektronix does not regularly monitor or guarantee responses in this area.
Post Reply
Chris B
Tektronix Applications
Tektronix Applications
Posts: 657
Joined: December 17th, 2010, 3:17 pm

Python Example: AFG3000 create and send waveform

Post by Chris B » January 24th, 2012, 3:06 pm

The following example creates a waveform complex enough that using the ArbExpress equation editor is a challenge. After the waveform is created, it is scaled, formatted and sent to an AFG3000, along with configuring the output to run the waveform in burst mode.
(similar to MATLAB example: viewtopic.php?t=133567)

Code: Select all

Create waveform and send to AFG3000

This script crates a 5mS waveform that starts with a 1uS low value, sends
a 1.2mS high pulse, then for the remained of the 5ms will send a squarewave
with a set frequency and duty cycle.  The waveform is then sent to and AFG 3000,
and set to play in burst mode, with and internal timmer triggering one cycle
of the waveform every 200mS

python 2.7 (http://www.python.org/)
pyvisa 1.3 (http://pypi.python.org/pypi/PyVISA/1.3)
#Enter your values here
frequency = 10e3
DutyCycle = 40                      # in percent

#Get the this from The OpenChoice Instrument Manager
instrumentdescriptor = 'USB0::0x0699::0x0345::CU000007::INSTR'

numpoint = int(125e3)               #Change this if needed, but should be OK
samplerate = 25e6
timeacq = numpoint / samplerate     # Set this so you get 5mS

#import needed librarys
import visa
from struct import pack
print 'Creating Waveform'
#Create the waveform
waveform = []

for j in range(numpoint):
    index = j * 1/float(samplerate)
    if -1 < index <= 1e-6:  #sets the initial dellay at 0
    elif 1e-6 < index <= 1.201e-3: # set the first 1.2mS pulse
        break #stop here once the 1.2mS time has been created

# Build one segment of the square wave, space first, on second
samplesperwave = samplerate / frequency
on_samples = DutyCycle / 100.0 * samplesperwave
off_samples = samplesperwave - on_samples
onewave = []
for j in range(int(off_samples)):
for j in range(int(on_samples)):

#Cycle the one wave until total time on is at 5mS
while len(waveform) * 1/float(samplerate) < 5e-3:
    for j in onewave:
        if len(waveform) * 1/float(samplerate) < 5e-3:
            #makes sure that we stop at 5mS, even if not even dividied

#Write the waveform to a text file 'testwave.txt'
fid = open('testwave.txt', 'w')
for j in waveform:
    fid.write(str(j) + '\n')

#Scale waveform magnitutde to raw value max for AFG
for j in range(len(waveform)):
    waveform[j] = waveform[j] * 16382

#create the binary block header.  Values will be converted to uint
header = '#' + str(len(str(numpoint * 2))) + str(numpoint * 2)

#create a binary string from the waveform values to transfer
binwavefrom = pack('>'+'h'*len(waveform), *waveform)

print 'Compelted Createing Waveform'
print 'Sending waveform to AFG'

#Next sending to the AFG
AFG = visa.instrument(instrumentdescriptor)
print AFG.ask('*IDN?')
#reset and clear the AFG status

#setup the waveform, lenght = number of points for waveform defined above
AFG.write('trace:define ememory, ' + str(numpoint))
#transfer waveform to afg
AFG.write('trace ememory, '+ header + binwavefrom)

#configure the channel to play arbitrary waveform, burst mode, .2ms timer
#on trigger, 0 - 5V output, 1 cycle of waveform per occurance.
AFG.write('source1:function ememory')
AFG.write('source1:frequency 200')
AFG.write('source1:burst:mode trig')
AFG.write('source1:burst:ncycles 1')
AFG.write('source1:burst:state on')
AFG.write('trigger:sequence:timer 0.2')
AFG.write('source1:voltage:high 5')
AFG.write('source1:voltage:low 0')

#finnaly, turn the output on
AFG.write('output1 on')

#freeze the output window until key is pressed
raw_input('Press any key to Exit')
Tested using AFG3252

Chris B
Tektronix Applications
Tektronix Applications
Posts: 657
Joined: December 17th, 2010, 3:17 pm

Re: Python Example: AFG3000 create and send waveform

Post by Chris B » September 5th, 2013, 10:40 am

Updated one line:
binwavefrom = pack('h'*len(waveform), *waveform)


binwavefrom = pack('>'+'h'*len(waveform), *waveform)

to ensure that the byte order is big-endian

Posts: 1
Joined: July 25th, 2016, 12:42 pm
Country: United States

Re: Python Example: AFG3000 create and send waveform

Post by ally.menon » July 25th, 2016, 12:44 pm

When I tested this code myself, I received the following error:
Creating Waveform
Completed Creating Waveform
Sending waveform to AFG
TEKTRONIX,AFG3252,C020669,SCPI:99.0 FV:2.1.0

Traceback (most recent call last):
File "<tmp 1>", line 88, in <module>
AFG.write('trace ememory, '+ header + binwavefrom)
File "c:\anaconda2\lib\site-packages\pyvisa\resources\messagebased.py", line 202, in write
if message.endswith(term):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 76: ordinal not in range(128)

Essentially the problem is with the write function which is causing some error with the code. Do you have any suggestions as to how I can fix this? I have no idea what's going on.

Post Reply

Return to “Older and Obsolete Signal Sources”

Who is online

Users browsing this forum: No registered users and 3 guests