Python script - hardcopies from a Tektronix TDS50xx Oscilloscope over GPIB - Page 1
Forum section: electronics / testgear

author: THDplusN_bad
author_rank: Regular Contributor
timestamp: November 27, 2021, 12:39:41 pm
content: 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_badCode:# 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 https://www.tek.com/support/faqs/how-save-and-transfer-screenshot-4-5-or-6-series-mso# 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 2021import pyvisa as visa # https://pyvisa.readthedocs.io/import osimport matplotlib.pyplot as pltimport matplotlib.image as mpimgimport timefrom datetime import datetime # std library# Replace the address in the following string with your instrument's VISA Resource AddressvisaResourceAddr = 'GPIB0::1::INSTR'# Setup VISA resource managerrm = visa.ResourceManager()scope = rm.open_resource(visaResourceAddr)# Generous timeout setting 20 sec.; meaningful if you have to read large data setsscope.timeout = 20000print ("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 stampdt = 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 errorsscope.write('*CLS')# Check if local directory on the TDS instrument exists, if not notify user and stopscope.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 userprint("PYTHON SCRIPT HAS FAILED.")# Close resourcesscope.close()rm.close()else:# Continue with hardcopy# Setup hardcopy parametersscope.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 Tektronixscope.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 = 40960hardcopydata = scope.read_raw(640*480)# Save hardcopy image data to the local disk in the "C:\My Documents" directoryowd = 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 panningimg = mpimg.imread(NewFilename)imgplot = plt.imshow(img)plt.show()# Change back to previous directory pathos.chdir(owd)# Delete local file on the TDS5054scope.write('FILESYSTEM:DELETE "C:\TekScope\images\TekH001.PNG"')# Close resourcesscope.close()rm.close()# Inform userprint("PYTHON SCRIPT COMPLETED SUCCESSFULLY!")

author: THDplusN_bad
author_rank: Regular Contributor
timestamp: November 30, 2021, 12:58:16 pm
content: A few users have asked for the prerequisites.My Python code requires you to install the following:Python,https://www.python.org/downloads/And the following so-called packages for Python. For a detailed tutorial on the installation of Python packages, please refer tohttps://packaging.python.org/tutorials/installing-packages/Matplotlib, seehttps://matplotlib.org/Pyvisa, seehttps://pyvisa.readthedocs.io/en/latest/Pyplot for viewing and examining the hardcopy PNG image file, seehttps://matplotlib.org/stable/tutorials/introductory/pyplot.htmlTimeOS Seehttps://www.pythonforbeginners.com/os/pythons-os-modulefor this one.Have fun,THDplusN_bad

author: THDplusN_bad
author_rank: Regular Contributor
timestamp: November 30, 2021, 12:59:19 pm
content: And with this, you can get a hardcopy from a crowded screen :-)

author: daveyk
author_rank: Frequent Contributor
timestamp: September 03, 2022, 09:04:23 pm
content: Question: According to the TDS5054 Programmer's manual, "When printing to a file, the file format is Windows Bitmap Image (BMP)".This is what I want. It is obviously story it as a PNG file. I really need it as a BMP. I have been able to transfer it via GPIB easy enough and quickly, but only as PNG, which my program can not use.If I can do BMP, It can Display it, resize it to 3.5" (for Word Docs) and transfer it to the clipboard.I have this utility working perfectly with my TDS3000 scopes and now am trying to get it working with my TDS5054B, and later my MSO4104. With the TDS3000, you can chose the format as PNG, BMPColor, BMP, TIFF, PCXColor, PCX, and RLE.Is there anyway to have the TDS5054 save it as BMP as in the programmer's manual?Dave

author: nctnico
author_rank: Super Contributor
timestamp: September 03, 2022, 10:23:38 pm
content: Quote from: daveyk on September 03, 2022, 09:04:23 pmIf you are using some kind of script, then you can add a steps that feeds the image through an image converter program. Python has modules for this, so if you use Python it shoudn't be super hard to do. I have done this before but I had to resort to using a temporary file to store the image before converting.

author: daveyk
author_rank: Frequent Contributor
timestamp: September 03, 2022, 11:04:44 pm
content: This is VB, but seems to work for me, getting the program to wait after giving the TDS5054 that hard:copy Start command. I don't know if it's right, but it works without sleeping.''perform hardcopyequip.WriteString "HARDCOPY START"equip.WriteString "*WAI"Do'equip.WriteString "*OPC?"Status = equip.ReadString'DoEvents'Loop While Status <> 1'If IsTDS5054 Then'equip.WriteString "FILESYSTEM:READFILE " & Chr(34) & "C:\TekScope\images\" & FileName & Ext & Chr$(34), True'End If'Also, I figure out how to control the output file format:equip.WriteString "EXPort:FORMat BMP"