comparison scpi.py @ 27:e2833d081413

- Handle zero length data from the instrument. - Handle trailing newlines. - Add helper function to allow URL like instrument connections.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 21 Sep 2011 14:56:21 +0930
parents a124aa7067e7
children 00800345fbae
comparison
equal deleted inserted replaced
26:226ea06bc050 27:e2833d081413
28 import exceptions 28 import exceptions
29 import numpy 29 import numpy
30 import re 30 import re
31 31
32 headerre = re.compile('^(:[A-Z:]+ ).*') 32 headerre = re.compile('^(:[A-Z:]+ ).*')
33 insturlre = re.compile('([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?')
34
35 def query(d, q, timeout = None):
36 d.write(q)
37 return d.read(timeout)
33 38
34 def clean(data): 39 def clean(data):
35 # The instrument might return some header before the data (TEK 2024B does this) 40 # The instrument might return some header before the data (TEK 2024B does this)
36 m = headerre.match(data) 41 m = headerre.match(data)
37 if m == None: 42 if m == None:
51 data = clean(data) 56 data = clean(data)
52 57
53 if data[0] != '#': 58 if data[0] != '#':
54 raise exceptions.ValueError('data length header incorrect') 59 raise exceptions.ValueError('data length header incorrect')
55 dlenlen = int(data[1]) 60 dlenlen = int(data[1])
61 if dlenlen == 0:
62 return numpy.array([])
63
56 dlen = int(data[2:dlenlen + 2]) 64 dlen = int(data[2:dlenlen + 2])
57 if len(data) != dlen + 2 + dlenlen: 65 if dlen == 0:
58 raise exceptions.ValueError('length mismatch, header says %d, actually got %d bytes' % (dlen + 2 + dlenlen, len(data))) 66 return numpy.array([])
67
68 wanted = dlen + 2 + dlenlen
69 # Chop off the trailing \n if necessary
70 if len(data) == wanted + 1 and data[-1] == '\n':
71 data = data[0:-1]
72
73 if len(data) != wanted:
74 raise exceptions.ValueError('length mismatch, header says %d, actually got %d bytes' % (wanted, len(data)))
59 return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype) 75 return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype)
60 76
61 def ascdecode(data, dtype = numpy.float32, sep = None): 77 def ascdecode(data, dtype = numpy.float32, sep = None):
62 '''Decode ASCII data, returns numpy array''' 78 '''Decode ASCII data, returns numpy array'''
63 79
74 sep = ',' 90 sep = ','
75 else: 91 else:
76 sep = ' ' 92 sep = ' '
77 return numpy.fromstring(data, dtype = dtype, sep = sep) 93 return numpy.fromstring(data, dtype = dtype, sep = sep)
78 94
95
96 def instURL(url):
97 m = insturlre.match(url)
98 if m == None:
99 raise exceptions.ValueError('Unable to parse URL')
100 proto, xxx, user, xxx, pwd, address, xxx, port = m.groups()
101 if proto == 'rsib':
102 import rsib
103 return rsib.RSIBDevice(address, port)
104 elif proto == 'usb':
105 import usb488
106 return usb488.USB488Device()
107 elif proto == 'vxi':
108 import vxi
109 if port == None:
110 port = 'inst0'
111 return vxi.VXIDevice(address, device = port)
112 elif proto == 'scpi':
113 import scpisock
114 return scpisock.SCPISockDevice(address, port)
115 else:
116 raise exceptions.NotImplementedError("unknown protocol " + proto)
117
118