Hi
I have been looking for / testing methods to get a reliable response from gpib queries sent to my WaveTek2520 RF signal generator.
The script I have come up with does this by ignoring any initial null responses, then it keeps reading until at least two adjacent nulls are returned.
I have no idea if this is best practice, or any practice, but it appears to work.
The instruction set and other key parameters are easily adjusted, so this script could be readily adapted to other instruments with simple query commands. That is why I have posted the script here:
# Wavetek 2520 RF Signal Generator simple read test
# The purpose of this script is to test a method to obtain reliable
# responses from the instrument. This script is designed to reliably
# read the responses to query type commands only.
#
# Reads fail because:
# the script runs too fast (pauses need to be inserted)
# the first read returns a null response and,
# responses are initially from a FIFO buffer.
#
# This script:
# discards an initial single null response.
# keeps reading until the responses are at least two nulls.
# This script will read multi-line responses.
# This script executes all query type gpib commands in a loop.
# The commands are stored in a 2xn matrix.
# The columns are a:
# query command
# description of the command
#
# 0: cmd SET?
# 1: description List all internal settings
# The instructions are in a user defined loop count.
# The commands are in an editable array to allow this script to
# be used with any simple query command, including multi-line responses.
# Edit "pause" to adjust the inserted delay periods.
import pyvisa
import time
from timeit import default_timer as timer
import sys
import array as arr
import numpy as np
# CMD commands to run
# D:
# D:\My Documents\Electronics\Wavetek SigGen\GPIB_test\
# Define a function that measures the execution time of the argument.
# The argument is a gpib command.
# User Editable Variables
read_cnt = 5 # The number of times to loop through a set of queries
pause = .05 # Edit this. This is the pause from the end of one query to the next.
# It is the added delay to allow the instrument to be ready for the next query.
############ Query Commands Arrays
# Edit this array to define all of the query commands.
# Array: "command", No. of response lines to read, description
arrayCmds = np.array([
["ID?" ,"ID"],
["FRQ?","Frequency"],
["FST?","Frequency Step"],
["LVL?","Amplitude"],
["LST?","Amp Step"],
["RF? ","RF Output on/off"],
["IAM?","Internal AM "],
["IFM?","Internal FM "],
["XAM?","External AM "],
["XFM?","External FM "],
["RTE?","Internal modulation tone 400/1000Hz"],
["MOD?","Modulation Mode"],
["ERR?","Error message"],
["OPT?","Installed Options"],
["SET?","List all settings:"]])
# The function to execute a GPIB query command. The command is executed and the results
# are printed.
def exec_query():
try:
for row in arrayCmds:
print("# Query : ", row[0]," = ",row[1])
start = timer()
end = start
# Write, Read same as query
SigGen.write(row[0]) # send command to instrument
RdNull = False # Count the number of nulls received
while True:
time.sleep(pause)
ans = SigGen.read()
end = timer()
duration = round((end - start) * 1000,2)
if ans == "" : # if read = null
#print("Null 1", RdNull)
if RdNull == True:
#print("Null 2", RdNull)
break
RdNull = True
else:
RdNull = False
print("# Returned : ", str( ans )," Duration : ", duration )
except pyvisa.VisaIOError:
print("########### WARNING : Time Out exception has occurred")
return "# Cmd Execution time: ", duration,"ms"
# ID string for adapter 0: 'USB0::0x03EB::0x2065::GPIB_23_24238323232351C01171::INSTR'
print("###### Simple GPIB test for WaveTek 2520 SigGen")
print("# Executes a list of query commands")
print()
print("# Show the list of Resources")
rm = pyvisa.ResourceManager()
print(rm.list_resources())
print("# Opening the SigGen")
# Resource = GPIB-usb adapter 0. Different for each adapter.
SigGen = rm.open_resource('USB0::0x03EB::0x2065::GPIB_02_24238323232351C01171::INSTR')
print()
# Reset the instrument, wait for reset to complete
print("# Clear the instrument")
SigGen.clear()
time.sleep(0.1)
###### Run this once to turn off auto ID. AutoID is not supported by WvTk2520.
#SigGen.control_in(0xa1, 0x40, 0, 0)
#SigGen.write('!0001')
SigGen.write_termination = '\n'
SigGen.read_termination = '\n'
SigGen.timeout = 500 #sets timeout (ms) for the instrument to respond.
print("# Run all query commands ...")
try:
for x in range (0,read_cnt):
print("# Loop Count :", x, exec_query()) # The gpib command under test
time.sleep(pause)
except KeyboardInterrupt:
print("################ Interrupted by keyboard !")
SigGen.clear()
SigGen.close()
rm.close()
# RESULTS
# Reading the responses (multiple reads) until nulls are sensed is a reliable method of
# reading data from a WaveTek 2520 RF signal generator. This may work with other
# instruments by modifying the command array.
A sample output is shown below:
# Loop Count : 3 ('# Cmd Execution time: ', 884.63, 'ms')
# Query : ID? = ID
# Returned : WVTK/2520A,V81.1,F5.1 Duration : 59.91
# Query : FRQ? = Frequency
# Returned : FREQUENCY 222.222 MHz Duration : 60.2
# Query : FST? = Frequency Step
# Returned : FREQ STEP 50.0 kHz Duration : 62.51
# Query : LVL? = Amplitude
# Returned : AMPLITUDE -10.0 dBm Duration : 61.86
# Query : LST? = Amp Step
# Returned : AMP STEP 0.10 dB Duration : 113.0
# Query : RF? = RF Output on/off
# Returned : RF OUTPUT OFF Duration : 60.41
# Query : IAM? = Internal AM
# Returned : INTERNAL AM 100.0 % Duration : 61.69
# Query : IFM? = Internal FM
# Returned : INTERNAL FM 80.0 kHz Duration : 62.06
# Query : XAM? = External AM
# Returned : EXTERNAL AM 60.0 % Duration : 62.13
# Query : XFM? = External FM
# Returned : EXTERNAL FM 235.0 kHz Duration : 111.9
# Query : RTE? = Internal modulation tone 400/1000Hz
# Returned : 499-SYSTEM OPERATIONAL Duration : 60.57
# Query : MOD? = Modulation Mode
# Returned : MODULATION EXT PM Duration : 60.55
# Query : ERR? = Error message
# Returned : 499-SYSTEM OPERATIONAL Duration : 60.66
# Query : OPT? = Installed Options
# Returned : OPT 75:Level Step Duration : 60.07
# Returned : OPT 7A:External Phase Mod Duration : 121.1
# Query : SET? = List all settings:
# Returned : FREQUENCY 222.222 MHz Duration : 112.46
# Returned : FREQ STEP 50.0 kHz Duration : 172.73
# Returned : AMPLITUDE -10.0 dBm Duration : 233.66
# Returned : INTERNAL AM 100.0 % Duration : 293.71
# Returned : INTERNAL FM 80.0 kHz Duration : 353.74
# Returned : EXTERNAL AM 60.0 % Duration : 414.08
# Returned : EXTERNAL FM 235.0 kHz Duration : 475.76
# Returned : RF OUTPUT OFF Duration : 536.76
# Returned : 499-SYSTEM OPERATIONAL Duration : 596.77
# Returned : MODULATION EXT PM Duration : 657.02
# Returned : AMP STEP 0.10 dB Duration : 717.81
# Returned : EXT PHASE 0.30 rads Duration : 778.42
# Loop Count : 4 ('# Cmd Execution time: ', 882.73, 'ms')