view hardcopy @ 0:6dfcd8e5b8df

first release
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Tue, 25 Mar 2014 20:31:00 +0000
parents
children 1a405bda2ffe
line wrap: on
line source
1 #!/usr/bin/env python
3 import argparse,os,sys,tempfile,time
4 try:
5 import serial
6 except ImportError:
7 print "Serial library not found - install python-serial"
8 sys.exit(99)
10 def main():
11 p=argparse.ArgumentParser(description="Take hardcopy from Tek scope")
12 p.add_argument(dest="File", metavar="file", type=str, nargs="?", default="hardcopy.bmp",
13 help="output file to write")
14 p.add_argument("-d","--port", dest="Device", action="store", default=0,
15 help="serial port to use")
16 args=p.parse_args()
18 if args.Device=="-":
19 class S:
20 def read(self,bytes=1<<14):return os.read(0,min(bytes,1<<14))
21 def write(self,data):return os.write(1,data)
22 usbgpib=S()
23 else:
24 usbgpib=serial.Serial(args.Device,timeout=5)
26 data=""
27 while True:
28 d=usbgpib.read(1)
29 if d=="" or d==chr(13) or d==chr(10):
30 break
31 data+=d
32 if "USB-GPIB" not in data:
33 print "USB-GPIB not found"
34 sys.exit(2)
36 usbgpib.write("rst\n")
37 time.sleep(0.2)
38 usbgpib.write("cac\n")
39 time.sleep(0.2)
40 usbgpib.write("cmd MTA UNL LAD\n")
41 usbgpib.write("wrt -\"hardcopy start\"\n")
42 usbgpib.write("cmd UNL MLA TAD\n")
43 usbgpib.write("read\n")
44 data=""
45 while True:
46 d=usbgpib.read()
47 if d=="":
48 break
49 data+=d
50 usbgpib.write("rst\n")
52 if data.startswith("\r"): data=data[1:]
53 if data.startswith("\n"): data=data[1:]
55 print "%d bytes"%len(data)
56 if args.File.endswith(".bmp"):
57 fd=file(args.File,"wb")
58 fd.write(data)
59 else:
60 fd=tempfile.NamedTemporaryFile("wb",suffix=".bmp")
61 fd.write(data)
62 fd.flush()
63 os.system("convert %s %s"%(fd.name,args.File))
64 del fd
66 if __name__=="__main__":
67 main()