Good Day,
the following simple Python script creates hardcopies from a Tektronix TDS5054 (non-B) DSO over GPIB. I have adopted this from another script released by Tektronix.
Please refrain from discussing my code style or quality - this is intended to provide some code to other TDS5000 owners that simply works. And it does on my system...
I am well aware that simple pause/sleep commands are not good for a number of reasons. However, I had spent some good amount of time with alternative methods, i.e. for synchronization incl. commands such as "*WAI", "BUSY?" pollings etc. All have failed. I tend to believe that the Windows 2000 OS running on the TDS5054 causes some serious latency.
Two example hardcopies are attached.
Enjoy! :)
Cheers,
THDplusN_bad
# A simple Python script that produces a screen hardcopy from a Tektronix type TDS5054 Oscilloscope and transfers this to the host PC in PNG format via GPIB interface
# Adopted from [url]https://www.tek.com/support/faqs/how-save-and-transfer-screenshot-4-5-or-6-series-mso[/url]
# See below for required packages such as pyvisa and matplotlib
# This was successfully tested with a TDS5054 non-B with Firmware release version 1.1.5, under MS Windows 10 64-bit Laptop PC and NI GPIB-HS USB adapter
# No warranty and no liability - use this at your own risk.
# THDplusN_bad, 27th Nov 2021
import pyvisa as visa # [url]https://pyvisa.readthedocs.io/[/url]
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import time
from datetime import datetime # std library
# Replace the address in the following string with your instrument's VISA Resource Address
visaResourceAddr = 'GPIB0::1::INSTR'
# Setup VISA resource manager
rm = visa.ResourceManager()
scope = rm.open_resource(visaResourceAddr)
# Generous timeout setting 20 sec.; meaningful if you have to read large data sets
scope.timeout = 20000
print ("Utility for the Tektronix TDS5054 oscilloscope")
print ("Produces a hardcopy of the screen display, saves it to local disk and transfers via GPIB.")
print ("Please connect your Tektronix TDS5054 oscilloscope via GPIB to your PC and set-up hardware. Note the default GPIB address is 1.")
print ("The plot will be saved in PNG format to your 'C:\My Documents' folder.")
Filename = input("Please enter a name for the hardcopy file. A timecode will be added to the filename: ")
PressEnter = input("Please press enter when ready: ")
Save_results_to = r"C:\My Documents"
# Create date & time stamp
dt = datetime.now()
NewFilename = dt.strftime(Filename+"_%Y%m%d_%H%M%S.png")
print("Identified Tektronix TDS oscilloscope:")
print(scope.query('*IDN?')) # Print instrument id to console window
# Clear any errors
scope.write('*CLS')
# Check if local directory on the TDS instrument exists, if not notify user and stop
scope.write('FILESYSTEM:CWD "C:\TekScope\"')
ListDir = scope.query('FILESYSTEM?')
DirExists=ListDir.count("images")
if DirExists == 0:
print("Please create a subfolder named 'C:\TekScope\images' on the TDS5054 oscilloscope and try again.")
# Inform user
print("PYTHON SCRIPT HAS FAILED.")
# Close resources
scope.close()
rm.close()
else:
# Continue with hardcopy
# Setup hardcopy parameters
scope.write('HARDCOPY:PORT FILE')
scope.write('HARDCOPY:FILENAME "C:\TekScope\images\TekH001.PNG"')
scope.write('HARDCOPY START')
# The following *OPC? query is used for synchronization, see Section "Synchronization Methods" on page 62 in the TDS5000 Programmer Guide from Tektronix
scope.query('*OPC?')
# The following delay is necessary for the script to complete every time and for provision of up-to-date hardcopy data. Further testing is required to understand and replace this in future.
# I have tried all kind of synchronization methods, but to no avail. I suppose the Windows OS' latency of the TDS5054 was causing these problems. You may need to change this for your system to work.
time.sleep(3)
scope.write('FILESYSTEM:READFILE "C:\TekScope\images\TekH001.PNG"')
scope.chunk_size = 40960
hardcopydata = scope.read_raw(640*480)
# Save hardcopy image data to the local disk in the "C:\My Documents" directory
owd = os.getcwd()
os.chdir(Save_results_to)
file = open(NewFilename, "wb")
file.write(hardcopydata)
file.close()
# Show hardcopy image with Pyplot as to allow zooming and panning
img = mpimg.imread(NewFilename)
imgplot = plt.imshow(img)
plt.show()
# Change back to previous directory path
os.chdir(owd)
# Delete local file on the TDS5054
scope.write('FILESYSTEM:DELETE "C:\TekScope\images\TekH001.PNG"')
# Close resources
scope.close()
rm.close()
# Inform user
print("PYTHON SCRIPT COMPLETED SUCCESSFULLY!")