comparison scpi.py @ 73:ca5a822c550a

Fix for Python 3 Add getbin function to read large non-arrat data blocks (eg screen capture)
author Daniel O'Connor <doconnor@gsoft.com.au>
date Wed, 25 Sep 2024 20:57:26 +0930
parents 00800345fbae
children
comparison
equal deleted inserted replaced
72:da3558dec4e3 73:ca5a822c550a
26 # 26 #
27 27
28 import numpy 28 import numpy
29 import re 29 import re
30 30
31 headerre = re.compile('^(:[A-Z:]+ ).*') 31 headerre = re.compile(b'^(:[A-Z:]+ ).*')
32 insturlre = re.compile('([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?') 32 insturlre = re.compile(b'([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?')
33 33
34 def query(d, q, timeout = None): 34 def query(d, q, timeout = None):
35 d.write(q) 35 d.write(q)
36 return d.read(timeout) 36 return d.read(timeout)
37 37
47 def getdata(data, dtype = float): 47 def getdata(data, dtype = float):
48 '''Get a single data item''' 48 '''Get a single data item'''
49 data = clean(data) 49 data = clean(data)
50 return dtype(data) 50 return dtype(data)
51 51
52 def bindecode(data, dtype = numpy.float32): 52 def getbin(data):
53 '''Decode binary data, returns numpy array''' 53 '''Fetch binary blob, strips ASCII length header and checks'''
54
55 data = clean(data) 54 data = clean(data)
56 55
57 if data[0] != '#': 56 if data[0] != ord('#'):
58 raise ValueError('data length header incorrect') 57 raise ValueError('data length header incorrect')
59 dlenlen = int(data[1]) 58 dlenlen = int(chr(data[1]))
60 if dlenlen == 0: 59 if dlenlen == 0:
61 return numpy.array([]) 60 return numpy.array([])
62 61
63 dlen = int(data[2:dlenlen + 2]) 62 dlen = int(data[2:dlenlen + 2])
64 if dlen == 0: 63 if dlen == 0:
65 return numpy.array([]) 64 return b''
66 65
67 wanted = dlen + 2 + dlenlen 66 wanted = dlen + 2 + dlenlen
68 # Chop off the trailing \n if necessary 67 # Chop off the trailing \n if necessary
69 if len(data) == wanted + 1 and data[-1] == '\n': 68 if len(data) == wanted + 1 and data[-1] == '\n':
70 data = data[0:-1] 69 data = data[0:-1]
71 70
72 if len(data) != wanted: 71 if len(data) != wanted:
73 raise ValueError('length mismatch, header says %d, actually got %d bytes' % (wanted, len(data))) 72 raise ValueError('length mismatch, header says %d, actually got %d bytes' % (wanted, len(data)))
74 return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype) 73
74 return data[2 + dlenlen:]
75
76 def bindecode(data, dtype = numpy.float32):
77 '''Decode binary data, returns numpy array'''
78
79 return numpy.frombuffer(getbin(data), dtype = dtype)
75 80
76 def ascdecode(data, dtype = numpy.float32, sep = None): 81 def ascdecode(data, dtype = numpy.float32, sep = None):
77 '''Decode ASCII data, returns numpy array''' 82 '''Decode ASCII data, returns numpy array'''
78 83
79 data = clean(data) 84 data = clean(data)