dump_trace.py

changeset 5
4d86b11abb82
parent 4
269aacbb2bf2
child 10
2999318b49a2
equal deleted inserted replaced
4:269aacbb2bf2 5:4d86b11abb82
1 import sys 1 import sys
2 import time 2 import time
3 import gpib 3 import gpib
4 4
5 MODE = {'binary': 'DDBN', 5 class GPIBtracedumper(gpib.GPIB):
6 'ascii': 'DDAS',
7 'ansi': 'DDAN',
8 }
9 6
10 def read_trace(cnx, mode="binary"): 7 MODE = {'binary': 'DDBN',
11 mode = mode.lower() 8 'ascii': 'DDAS',
12 assert mode in MODE 9 'ansi': 'DDAN',
13 mode = MODE[mode] 10 }
14 res = "" 11 def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
15 cnx.write('%s\r'%mode) 12 address=0):
16 i = 0 13 super(GPIBtracedumper, self).__init__(device, baudrate, timeout, address, mode=1)
17 while i<5: 14
18 l = cnx.readline() 15 def read_trace(self, mode="binary"):
19 if l.strip() == "": 16 mode = mode.lower()
20 i += 1 17 assert mode in MODE
21 time.sleep(0.1) 18 mode = MODE[mode]
22 continue 19 res = ""
23 #print "got a new line (%s chars) [i=%s]"%(len(l), i) 20 self._cnx.write('%s\r'%mode)
24 res += l
25 i = 0 21 i = 0
26 return res 22 while i<self._retries:
23 l = self._cnx.readline()
24 if l.strip() == "":
25 i += 1
26 time.sleep(self._timeout)
27 continue
28 res += l
29 i = 0
30 return res
27 31
28 32
29 if __name__=='__main__': 33 if __name__=='__main__':
30 import optparse 34 import optparse
31 opt = optparse.OptionParser("A simple tool for dumping the current trace") 35 opt = optparse.OptionParser("A simple tool for dumping the current trace")
44 dest='address', 48 dest='address',
45 help='GPIB address of the device', 49 help='GPIB address of the device',
46 ) 50 )
47 options, argv = opt.parse_args(sys.argv) 51 options, argv = opt.parse_args(sys.argv)
48 52
49 cnx = open_connection(device=options.device, 53 cnx = GPIBtracedumper(device=options.device,
50 address=int(options.address), mode=1) 54 address=int(options.address))
51 res = read_trace(cnx, mode=options.mode) 55 res = cnx.read_trace(mode=options.mode)
52 56
53 if options.filename: 57 if options.filename:
54 open(options.filename, 'w').write(res) 58 open(options.filename, 'w').write(res)
55 else: 59 else:
56 print res 60 print res

mercurial