|
#!/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) |
Works pretty well. Thanks!