Mon, 10 Dec 2007 21:22:33 +0100
small cleanup
0 | 1 | import sys |
2 | import time | |
4 | 3 | import gpib |
0 | 4 | |
5 | MODE = {'binary': 'DDBN', | |
6 | 'ascii': 'DDAS', | |
7 | 'ansi': 'DDAN', | |
8 | } | |
9 | ||
10 | def read_trace(cnx, mode="binary"): | |
11 | mode = mode.lower() | |
12 | assert mode in MODE | |
13 | mode = MODE[mode] | |
14 | res = "" | |
15 | cnx.write('%s\r'%mode) | |
16 | i = 0 | |
17 | while i<5: | |
18 | l = cnx.readline() | |
19 | if l.strip() == "": | |
20 | i += 1 | |
21 | time.sleep(0.1) | |
22 | continue | |
23 | #print "got a new line (%s chars) [i=%s]"%(len(l), i) | |
24 | res += l | |
25 | i = 0 | |
26 | return res | |
27 | ||
28 | ||
4 | 29 | if __name__=='__main__': |
30 | import optparse | |
31 | opt = optparse.OptionParser("A simple tool for dumping the current trace") | |
32 | opt.add_option('-f', '--filename', default=None, | |
33 | dest='filename', | |
34 | help='Output filename. If not set, write to stdout') | |
35 | opt.add_option('-d', '--device', default='/dev/ttyUSB0', | |
36 | dest='device', | |
37 | help='Device of the RS232 connection (default: /dev/ttyUSB0)', | |
38 | ) | |
39 | opt.add_option('-m', '--mode', default='binary', | |
40 | dest='mode', | |
41 | help='Dumping mode (may be "binary" [default], "ascii" or "ansi")', | |
42 | ) | |
43 | opt.add_option('-a', '--address', default=0, | |
44 | dest='address', | |
45 | help='GPIB address of the device', | |
46 | ) | |
47 | options, argv = opt.parse_args(sys.argv) | |
0 | 48 | |
4 | 49 | cnx = open_connection(device=options.device, |
50 | address=int(options.address), mode=1) | |
51 | res = read_trace(cnx, mode=options.mode) | |
0 | 52 | |
4 | 53 | if options.filename: |
54 | open(options.filename, 'w').write(res) | |
55 | else: | |
56 | print res | |
0 | 57 |