dump_as_ascii.py

changeset 0
9af1509775b6
equal deleted inserted replaced
-1:000000000000 0:9af1509775b6
1 import sys
2 import time
3 import serial
4
5 MODE = {'binary': 'DDBN',
6 'ascii': 'DDAS',
7 'ansi': 'DDAN',
8 }
9 class ConnectionError(Exception):
10 pass
11
12 def read_trace(cnx, mode="binary"):
13 mode = mode.lower()
14 assert mode in MODE
15 mode = MODE[mode]
16 res = ""
17 cnx.write('%s\r'%mode)
18 i = 0
19 while i<5:
20 l = cnx.readline()
21 if l.strip() == "":
22 i += 1
23 time.sleep(0.1)
24 continue
25 #print "got a new line (%s chars) [i=%s]"%(len(l), i)
26 res += l
27 i = 0
28 return res
29
30 def open_connection(device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
31 address=0, mode=1):
32 p = serial.Serial(port=device, baudrate=baudrate, timeout=timeout)
33
34 p.write('++addr %d\r'%address) # set address to 0
35 p.write('++mode %d\r'%mode) # read listen only mode
36 p.write('++mode\r')
37 i = 0
38 for i in range(10):
39 rmode = p.readline().strip()
40 if rmode != "":
41 break
42 time.sleep(timeout)
43
44 if rmode == '' or int(rmode) != mode:
45 raise ConnectionError("Can't set GPIB mode to %s"%mode)
46 return p
47
48
49 import optparse
50 opt = optparse.OptionParser()
51 opt.add_option('-f', '--filename', default=None,
52 dest='filename',
53 help='Output filename. If not set, write to stdout')
54 opt.add_option('-d', '--device', default='/dev/ttyUSB0',
55 dest='device',
56 help='Device of the RS232 connection (default: /dev/ttyUSB0)',
57 )
58 opt.add_option('-m', '--mode', default='binary',
59 dest='mode',
60 help='Dumping mode (may be "binary" [default], "ascii" or "ansi")',
61 )
62 opt.add_option('-a', '--address', default=0,
63 dest='address',
64 help='GPIB address of the device',
65 )
66 options, argv = opt.parse_args(sys.argv)
67
68 cnx = open_connection(device=options.device,
69 address=int(options.address), mode=1)
70 res = read_trace(cnx, mode=options.mode)
71
72 if options.filename:
73 open(options.filename, 'w').write(res)
74 else:
75 print res
76

mercurial