comparison scpi.py @ 71:00800345fbae

Python3ise RSIB code
author Daniel O'Connor <doconnor@gsoft.com.au>
date Thu, 24 Aug 2023 16:52:10 +0930
parents e2833d081413
children ca5a822c550a
comparison
equal deleted inserted replaced
70:6ffa6fcf278e 71:00800345fbae
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE. 25 # SUCH DAMAGE.
26 # 26 #
27 27
28 import exceptions
29 import numpy 28 import numpy
30 import re 29 import re
31 30
32 headerre = re.compile('^(:[A-Z:]+ ).*') 31 headerre = re.compile('^(:[A-Z:]+ ).*')
33 insturlre = re.compile('([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?') 32 insturlre = re.compile('([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?')
54 '''Decode binary data, returns numpy array''' 53 '''Decode binary data, returns numpy array'''
55 54
56 data = clean(data) 55 data = clean(data)
57 56
58 if data[0] != '#': 57 if data[0] != '#':
59 raise exceptions.ValueError('data length header incorrect') 58 raise ValueError('data length header incorrect')
60 dlenlen = int(data[1]) 59 dlenlen = int(data[1])
61 if dlenlen == 0: 60 if dlenlen == 0:
62 return numpy.array([]) 61 return numpy.array([])
63 62
64 dlen = int(data[2:dlenlen + 2]) 63 dlen = int(data[2:dlenlen + 2])
69 # Chop off the trailing \n if necessary 68 # Chop off the trailing \n if necessary
70 if len(data) == wanted + 1 and data[-1] == '\n': 69 if len(data) == wanted + 1 and data[-1] == '\n':
71 data = data[0:-1] 70 data = data[0:-1]
72 71
73 if len(data) != wanted: 72 if len(data) != wanted:
74 raise exceptions.ValueError('length mismatch, header says %d, actually got %d bytes' % (wanted, len(data))) 73 raise ValueError('length mismatch, header says %d, actually got %d bytes' % (wanted, len(data)))
75 return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype) 74 return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype)
76 75
77 def ascdecode(data, dtype = numpy.float32, sep = None): 76 def ascdecode(data, dtype = numpy.float32, sep = None):
78 '''Decode ASCII data, returns numpy array''' 77 '''Decode ASCII data, returns numpy array'''
79 78
94 93
95 94
96 def instURL(url): 95 def instURL(url):
97 m = insturlre.match(url) 96 m = insturlre.match(url)
98 if m == None: 97 if m == None:
99 raise exceptions.ValueError('Unable to parse URL') 98 raise ValueError('Unable to parse URL')
100 proto, xxx, user, xxx, pwd, address, xxx, port = m.groups() 99 proto, xxx, user, xxx, pwd, address, xxx, port = m.groups()
101 if proto == 'rsib': 100 if proto == 'rsib':
102 import rsib 101 import rsib
103 return rsib.RSIBDevice(address, port) 102 return rsib.RSIBDevice(address, port)
104 elif proto == 'usb': 103 elif proto == 'usb':
111 return vxi.VXIDevice(address, device = port) 110 return vxi.VXIDevice(address, device = port)
112 elif proto == 'scpi': 111 elif proto == 'scpi':
113 import scpisock 112 import scpisock
114 return scpisock.SCPISockDevice(address, port) 113 return scpisock.SCPISockDevice(address, port)
115 else: 114 else:
116 raise exceptions.NotImplementedError("unknown protocol " + proto) 115 raise NotImplementedError("unknown protocol " + proto)
117 116
118 117