My first paddle into the sea of Python is a script that tests the time to execute and reliability of query commands.
It does this simply by executing the same query command (ID?) 10x in quick succession, and measuring the execution time to complete each query. The purpose of posting this is to make it available to others. It should probably run with any and all GPIB instruments.
With very little modification, this script could read a command line argument and execute that. One script to run many different query commands. I have chosen not to do that.
My plan is to copy/paste/modify this script for each query command, run the tests, and record a summary of the results in the script.
I have already found that the WaveTek 2520 is not a reliable responder. The pyVISA exception handling is not handling the occasional errors I see.
# Wavetek 2520 RF Signal Generator simple read test
# The purpose of this script is to gauge gpib command reliability, and to measure the
# execution time.
# This repeats the same gpib query command 10 times and measures the execution time
# for each query.
import pyvisa
import time
from timeit import default_timer as timer
# Define a function that measures the execution time of the argument.
# The argument is a gpib command.
# User Editable Variables
pause = 0.02 # Edit this. This is the pause from the end of one query to the next.
# It is the time taken for the instrument to be ready for the next query.
gpib_query = "ID? " # Edit this. This is the gpib query command under test.
# ID string for adapter 0: 'USB0::0x03EB::0x2065::GPIB_23_24238323232351C01171::INSTR'
def exec_query(cmd):
try:
start = timer()
ans = SigGen.query(cmd)
end = timer()
print("Query : ", cmd," Returned : ", str( ans ) )
ExTm = round((end - start) * 1000,2) # milliseconds
except pyvisa.VisaIOError:
print("########### WARNING : Time Out exception has occurred")
SigGen.clear()
return " Cmd Execution time: ", ExTm,"ms"
print("###### Simple GPIB test for WaveTek 2520 SigGen")
print("# Executes read ID 10x")
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
SigGen.clear() # Doesn't appear to have any effect on the Signal Generator.
###### Run this once to turn off auto ID. AutoID is not supported by the WaveTek 2520.
#SigGen.control_in(0xa1, 0x40, 0, 0)
#SigGen.write('!0001')
SigGen.write_termination = '\n'
SigGen.read_termination = '\n'
SigGen.timeout = 1000 #sets 1s timeout for the meter after the read
#print("Read the ID")
try:
for x in range (1,10):
print("# Loop Count :", x, exec_query(gpib_query)) # Edit the gpib
time.sleep(pause)
except KeyboardInterrupt:
SigGen.clear()
SigGen.close()
rm.close()
# RESULTS
# typical execution time of print(SigGen.query('ID? ')) is <12ms
# delay 20ms before next ID? read.
I usually get an output that looks like this:
GPIB_test>python WvTk2520_gpib_rd_ID.py\
###### Simple GPIB test for WaveTek 2520 SigGen
# Executes read ID 10x
# Show the list of Resources
('USB0::0x03EB::0x2065::GPIB_02_24238323232351C01171::INSTR', 'ASRL1::INSTR')
# Opening the SigGen
Query : ID? Returned : WVTK/2520A,V81.1,F5.1
# Loop Count : 1 (' Cmd Execution time: ', 11.91, 'ms')
Query : ID? Returned : WVTK/2520A,V81.1,F5.1
# Loop Count : 2 (' Cmd Execution time: ', 11.75, 'ms')
Query : ID? Returned : WVTK/2520A,V81.1,F5.1
# Loop Count : 3 (' Cmd Execution time: ', 11.74, 'ms')
Query : ID? Returned : WVTK/2520A,V81.1,F5.1
# Loop Count : 4 (' Cmd Execution time: ', 11.78, 'ms')
Query : ID? Returned : WVTK/2520A,V81.1,F5.1
# Loop Count : 5 (' Cmd Execution time: ', 11.89, 'ms')
Query : ID? Returned : WVTK/2520A,V81.1,F5.1
# Loop Count : 6 (' Cmd Execution time: ', 11.92, 'ms')
Query : ID? Returned : WVTK/2520A,V81.1,F5.1
# Loop Count : 7 (' Cmd Execution time: ', 11.73, 'ms')
Query : ID? Returned : WVTK/2520A,V81.1,F5.1
# Loop Count : 8 (' Cmd Execution time: ', 11.95, 'ms')
Query : ID? Returned : WVTK/2520A,V81.1,F5.1
# Loop Count : 9 (' Cmd Execution time: ', 11.74, 'ms')
when it doesn't work, I get an output that looks like this:
D:\darren\My Documents\Electronics\Wavetek SigGen\GPIB_test>python WvTk2520_gpib_rd_ID.py\
###### Simple GPIB test for WaveTek 2520 SigGen
# Executes read ID 10x
# Show the list of Resources
('USB0::0x03EB::0x2065::GPIB_02_24238323232351C01171::INSTR', 'ASRL1::INSTR')
# Opening the SigGen
Traceback (most recent call last):
File "D:\darren\My Documents\Electronics\Wavetek SigGen\GPIB_test\WvTk2520_gpib_rd_ID.py\", line 51, in <module>
SigGen.clear()
File "G:\Programs\Python310\Lib\site-packages\pyvisa\resources\resource.py", line 381, in clear
self.visalib.clear(self.session)
File "G:\Programs\Python310\Lib\site-packages\pyvisa\ctwrapper\functions.py", line 616, in clear
return library.viClear(session)
^^^^^^^^^^^^^^^^^^^^^^^^
File "G:\Programs\Python310\Lib\site-packages\pyvisa\ctwrapper\highlevel.py", line 226, in _return_handler
return self.handle_return_value(session, ret_value) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "G:\Programs\Python310\Lib\site-packages\pyvisa\highlevel.py", line 251, in handle_return_value
raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_SYSTEM_ERROR (-1073807360): Unknown system error (miscellaneous error).