HP3562A/__init__.py

changeset 14
07e2cbf140df
parent 13
78e3e839658b
child 15
b930440af354
equal deleted inserted replaced
13:78e3e839658b 14:07e2cbf140df
10 10
11 Constants 11 Constants
12 --------- 12 ---------
13 13
14 """ 14 """
15 import struct
16 import gpib
15 17
16 from gpib import gpib 18 ########################################
19 # HP3562A internal binary types decoders
20
21 def decode_float(s):
22 assert len(s) in [4,8]
23 # exponential term
24 e = ord(s[-1])
25 if e & 0x80:
26 e = e - 256
27
28 # mantissa
29 m = [ord(x) for x in s[:-1]]
30 M = 0.
31 for i in range(len(s)-1):
32 #M += m[i]<<(i*8)
33 M += float(m[i])/2**((i+1)*8)
34 # XXX how do we deal negative numbers?
35 #if m[0] & 0x80:
36 # M = M - 2^(len(s))
37 return M * 2**(e+1)
38
39 def decode_string(s):
40 nb = ord(s[0])
41 s = s[1:nb+2]
42 r = ""
43 # XXX why do we need to do this? It's not described in the manual...
44 for c in s:
45 r += chr(ord(c) & 0x7F)
46 return r
47
48
49 ###
50 # Some useful functions
51 def format_header(header, head_struct, columns=80):
52 """
53 Pretty print a data block (trace, state or coord)
54 """
55 todisp = []
56 for row in head_struct:
57 key = row[0]
58 val = header.get(key, "N/A")
59 if isinstance(val, basestring):
60 val = repr(val)
61 else:
62 val = str(val)
63 todisp.append((key+":", val))
64 maxk = max([len(k) for k, v in todisp])
65 maxv = max([len(v) for k, v in todisp])
66 fmt = "%%-%ds %%-%ds"%(maxk, maxv)
67 w = maxk+maxv+4
68 ncols = columns/w
69 nrows = len(todisp)/ncols
70 res = ""
71 for i in range(nrows):
72 res += "| ".join([fmt%todisp[j*nrows+i] for j in range(ncols)]) + "\n"
73 return res
74
75 def decode_header(data, header_struct):
76 d = data
77 typ = d[:2]
78 assert typ == "#A"
79
80 totlen = struct.unpack('>h', d[2:4])[0]
81 idx = 4
82 tt=0
83 header = {}
84 for i, (nam, dtype, fmt, nbytes) in enumerate(header_struct):
85 if dtype == str:
86 val = decode_string(d[idx:])
87 else:
88 if fmt:
89 v = struct.unpack('>'+fmt, d[idx: idx+nbytes])[0]
90 if isinstance(dtype, dict):
91 val = dtype.get(int(v), "N/A")
92 else:
93 val = dtype(v)
94 else:
95 val = dtype(d[idx: idx+nbytes])
96 header[nam] = val
97 idx += nbytes
98 return header, idx
17 99
18 ##################### 100 #####################
19 # HP3562A constants 101 # HP3562A constants
20 102
21 # GPIB buffer size is 3x80 characters lines 103 # GPIB buffer size is 3x80 characters lines
100 (0x10, "MSSM", "Missed sample"), 182 (0x10, "MSSM", "Missed sample"),
101 (0x20, "TMPR", "Timed preview"), 183 (0x20, "TMPR", "Timed preview"),
102 (0x40, "ACDA", "Accept date"), 184 (0x40, "ACDA", "Accept date"),
103 #... 185 #...
104 ] 186 ]
187
188

mercurial