view hardcopy @ 9:16a338171c99

remove random.h after programming
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Mon, 21 Apr 2014 11:14:00 +0100
parents 1a405bda2ffe
children
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 state=0
11 byte=0
13 def main():
14 p=argparse.ArgumentParser(description="Take hardcopy from Tek scope")
15 p.add_argument(dest="File", metavar="file", type=str, nargs="?", default="hardcopy.bmp",
16 help="output file to write")
17 p.add_argument("-d","--port", dest="Device", action="store", default=0,
18 help="serial port to use")
19 p.add_argument("-v","--verbose", dest="Verbose", action="store_true",
20 help="Be more verbose")
21 p.add_argument("--debug", dest="Debug", action="store_true",
22 help="Show commands and data")
23 args=p.parse_args()
25 if args.Device=="-":
26 class S:
27 def read(self,bytes=1<<14):return os.read(0,min(bytes,1<<14))
28 def write(self,data):return os.write(1,data)
29 usbgpib=S()
30 else:
31 usbgpib=serial.Serial(args.Device,timeout=5)
33 def w(data):
34 if args.Debug:
35 print "> %s"%repr(data)[1:-1]
36 usbgpib.write("%s\n"%data)
38 def r(binary=False):
39 global byte,state
40 data=""
41 while True:
42 d=usbgpib.read(1)
43 #print "state=%d d=%r"%(state,d)
44 if not binary:
45 if d==chr(13):
46 continue
47 if d==chr(10):
48 break
49 if d=="":
50 break
51 if state==0 and d=="\\":
52 state=1
53 byte=0
54 elif state==1:
55 if d>='0' and d<='3':
56 byte=64*int(d)
57 state=2
58 else:
59 data+=d
60 state=0
61 elif state==2:
62 byte+=8*int(d)
63 state=3
64 elif state==3:
65 byte+=int(d)
66 state=0
67 data+=chr(byte)
68 else:
69 data+=d
70 if binary and len(data)==256:
71 break
72 if args.Debug: print "< %s"%repr(data)[1:-1]
73 return data
75 for t in range(10):
76 data=r()
77 if "USB-GPIB" in data:
78 break
79 if not data:
80 w("++ver")
82 if "USB-GPIB" not in data:
83 print "USB-GPIB not found"
84 sys.exit(2)
86 w('rst')
87 r()
88 w('cac')
89 r()
91 w('write -"hardcopy:format bmp;port gpib;layout portrait"')
92 r()
93 w('write -"hardcopy start"')
94 time.sleep(0.2)
95 r()
96 w('read')
97 r()
98 data=""
99 while True:
100 d=r(True)
101 if d=="":
102 break
103 data+=d
104 if args.Verbose:
105 sys.stdout.write("%d\r"%len(data))
106 sys.stdout.flush()
107 if args.Verbose:
108 print
109 w("rst")
111 i=data.find("BM")
112 if i>=0 and i<200:
113 data=data[i:]
114 if data.endswith("\r\n++"):
115 data=data[:-4]
117 print "%d bytes"%len(data)
118 if args.File.endswith(".bmp"):
119 fd=file(args.File,"wb")
120 fd.write(data)
121 else:
122 fd=tempfile.NamedTemporaryFile("wb",suffix=".bmp")
123 fd.write(data)
124 fd.flush()
125 cmd="convert %s %s"%(fd.name,args.File)
126 if args.Verbose:print "$ %s"%cmd
127 os.system(cmd)
128 del fd
130 if __name__=="__main__":
131 main()