| Products > Test Equipment |
| Python script - hardcopies from a Tektronix TDS50xx Oscilloscope over GPIB |
| (1/2) > >> |
| THDplusN_bad:
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 --- Code: ---# 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!") --- End code --- |
| THDplusN_bad:
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 to https://packaging.python.org/tutorials/installing-packages/ Matplotlib, see https://matplotlib.org/ Pyvisa, see https://pyvisa.readthedocs.io/en/latest/ Pyplot for viewing and examining the hardcopy PNG image file, see https://matplotlib.org/stable/tutorials/introductory/pyplot.html Time OS See https://www.pythonforbeginners.com/os/pythons-os-module for this one. Have fun, THDplusN_bad |
| THDplusN_bad:
And with this, you can get a hardcopy from a crowded screen :-) |
| daveyk:
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 |
| nctnico:
--- Quote from: daveyk on September 03, 2022, 09:04:23 pm ---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. --- End quote --- If 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. |
| Navigation |
| Message Index |
| Next page |