# HG changeset patch # User Daniel O'Connor # Date 1727263646 -34200 # Node ID ca5a822c550ad63546690b89db2066060687eaf8 # Parent da3558dec4e328c11f5d4d98e0d448ad4584b5e3 Fix for Python 3 Add getbin function to read large non-arrat data blocks (eg screen capture) diff -r da3558dec4e3 -r ca5a822c550a scpi.py --- a/scpi.py Wed Sep 25 20:56:39 2024 +0930 +++ b/scpi.py Wed Sep 25 20:57:26 2024 +0930 @@ -28,8 +28,8 @@ import numpy import re -headerre = re.compile('^(:[A-Z:]+ ).*') -insturlre = re.compile('([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?') +headerre = re.compile(b'^(:[A-Z:]+ ).*') +insturlre = re.compile(b'([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?') def query(d, q, timeout = None): d.write(q) @@ -49,29 +49,34 @@ data = clean(data) return dtype(data) -def bindecode(data, dtype = numpy.float32): - '''Decode binary data, returns numpy array''' - +def getbin(data): + '''Fetch binary blob, strips ASCII length header and checks''' data = clean(data) - if data[0] != '#': + if data[0] != ord('#'): raise ValueError('data length header incorrect') - dlenlen = int(data[1]) + dlenlen = int(chr(data[1])) if dlenlen == 0: return numpy.array([]) - + dlen = int(data[2:dlenlen + 2]) if dlen == 0: - return numpy.array([]) + return b'' wanted = dlen + 2 + dlenlen # Chop off the trailing \n if necessary if len(data) == wanted + 1 and data[-1] == '\n': data = data[0:-1] - + if len(data) != wanted: raise ValueError('length mismatch, header says %d, actually got %d bytes' % (wanted, len(data))) - return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype) + + return data[2 + dlenlen:] + +def bindecode(data, dtype = numpy.float32): + '''Decode binary data, returns numpy array''' + + return numpy.frombuffer(getbin(data), dtype = dtype) def ascdecode(data, dtype = numpy.float32, sep = None): '''Decode ASCII data, returns numpy array'''