comparison scpi.py @ 20:a124aa7067e7

Make ascdecode smarter so it can handle Anritsu ASCII.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 11 Aug 2011 17:13:55 +0930
parents 20df02be818a
children e2833d081413
comparison
equal deleted inserted replaced
19:cba1c44060f5 20:a124aa7067e7
56 dlen = int(data[2:dlenlen + 2]) 56 dlen = int(data[2:dlenlen + 2])
57 if len(data) != dlen + 2 + dlenlen: 57 if len(data) != dlen + 2 + dlenlen:
58 raise exceptions.ValueError('length mismatch, header says %d, actually got %d bytes' % (dlen + 2 + dlenlen, len(data))) 58 raise exceptions.ValueError('length mismatch, header says %d, actually got %d bytes' % (dlen + 2 + dlenlen, len(data)))
59 return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype) 59 return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype)
60 60
61 def ascdecode(data, dtype = numpy.float32): 61 def ascdecode(data, dtype = numpy.float32, sep = None):
62 '''Decode ASCII data, returns numpy array''' 62 '''Decode ASCII data, returns numpy array'''
63 63
64 data = clean(data) 64 data = clean(data)
65 return numpy.fromstring(data, dtype = dtype, sep = ',') 65
66 # Strip length off if present as we don't need it
67 if data[0] == '#':
68 l = int(data[1])
69 data = data[l + 2:]
70
71 # Take a guess at the separator
72 if sep == None:
73 if data.find(',', 0, 20) != -1:
74 sep = ','
75 else:
76 sep = ' '
77 return numpy.fromstring(data, dtype = dtype, sep = sep)
66 78