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