Skip to content

Instantly share code, notes, and snippets.

@thegaragelab
Created May 2, 2014 08:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thegaragelab/981cf0eebc97fc5977ed to your computer and use it in GitHub Desktop.
Save thegaragelab/981cf0eebc97fc5977ed to your computer and use it in GitHub Desktop.
Image capture for Tektronix TDS 1012 Oscilloscope

This is a simple Python script to capture screen shots from a Tektronix scope, rotate them to landscape mode and convert them to PNG. The script simply sits in the background monitoring the serial port and creating the images in a specified directory.

The following settings are required on the TDS 1012:

  1. Printing in PCX format to the RS232 port.
  2. RS232 set to 19200N81.

A null modem cable is required to connect the scope to a PC, I used a USB to DB9 serial adaptor on the PC end.

#!/usr/bin/env python
#----------------------------------------------------------------------------
# 02-May-2014 ShaneG
#
# Capture data from a Tektronix TDS1012 scope and save as an image.
#----------------------------------------------------------------------------
from sys import argv, stdout
from os import makedirs, remove
from os.path import abspath, exists, isdir, join
from time import sleep, strftime
from PIL import Image
import serial
# What speed to use
BAUD_RATE = 19200
def grabImage(scope, path):
""" Grab a single image from the scope and save it to a file
"""
# Create the file
filename = join(path, strftime("scope_%y%m%d_%H%M%S"))
output = open(filename + ".pcx", "wb")
print "Receiving screen dump to '%s'" % filename
# Now read the data
bytes = 0
last = 0
while True:
data = scope.read(10240)
if len(data) == 0:
break
output.write(data)
bytes = bytes + len(data)
if int(bytes / 10240) > last:
stdout.write("%dK " % int(bytes / 1024))
stdout.flush()
last = int(bytes / 10240)
output.close()
print "Saved %d bytes." % bytes
# Now rotate and convert to a PNG
im = Image.open(filename + ".pcx")
im.load()
im = im.transpose(Image.ROTATE_270)
im.save(filename + ".png")
# And delete the PCX file
remove(filename + ".pcx")
#----------------------------------------------------------------------------
# Main program
#----------------------------------------------------------------------------
if __name__ == "__main__":
# Set default
port = "/dev/ttyUSB0"
path = None
# Get command line arguments
index = 1
while index < len(argv):
if argv[index] == "--port":
# Set serial port
index = index + 1
port = argv[index]
elif argv[index] == "--path":
# Set image directory
index = index + 1
path = argv[index]
else:
print "ERROR: Unrecognised argument."
exit(1)
# Next arg
index = index + 1
# Make sure the path exists and is a directory
if path is None:
path = "."
path = abspath(path)
if not exists(path):
makedirs(path)
if not isdir(path):
print "ERROR: Specified location is not a directory."
print " " + path
exit(1)
# Set up the serial port
scope = serial.Serial(port, BAUD_RATE, timeout = 2)
# And wait for some data
while True:
while scope.inWaiting() == 0:
sleep(0.25)
grabImage(scope, path)
@fhpriamo
Copy link

Works pretty well. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment