Mercurial > ~darius > hgwebdir.cgi > pyinst
annotate scpi.py @ 31:c6c86dcb54ba
Add code to automate a sitesurvey (to some degree).
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Wed, 21 Sep 2011 15:00:24 +0930 |
parents | e2833d081413 |
children | 00800345fbae |
rev | line source |
---|---|
15
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
2 |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
3 # Copyright (c) 2011 |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved. |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
5 # |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
6 # Redistribution and use in source and binary forms, with or without |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
7 # modification, are permitted provided that the following conditions |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
8 # are met: |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
9 # 1. Redistributions of source code must retain the above copyright |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
10 # notice, this list of conditions and the following disclaimer. |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 # 2. Redistributions in binary form must reproduce the above copyright |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 # notice, this list of conditions and the following disclaimer in the |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
13 # documentation and/or other materials provided with the distribution. |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
14 # |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
25 # SUCH DAMAGE. |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
26 # |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
27 |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 import exceptions |
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
29 import numpy |
17
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
30 import re |
15
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
31 |
17
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
32 headerre = re.compile('^(:[A-Z:]+ ).*') |
27
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
33 insturlre = re.compile('([a-zA-Z0-9]+)://(([^:]+)?(:([^/]+)@))?([^:]+)(:(.+))?') |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
34 |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
35 def query(d, q, timeout = None): |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
36 d.write(q) |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
37 return d.read(timeout) |
17
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
38 |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
39 def clean(data): |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
40 # The instrument might return some header before the data (TEK 2024B does this) |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
41 m = headerre.match(data) |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
42 if m == None: |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
43 return data |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
44 else: |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
45 hlen = len(m.groups()[0]) |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
46 return(data[hlen:]) |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
47 |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
48 def getdata(data, dtype = float): |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
49 '''Get a single data item''' |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
50 data = clean(data) |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
51 return dtype(data) |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
52 |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
53 def bindecode(data, dtype = numpy.float32): |
15
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
54 '''Decode binary data, returns numpy array''' |
17
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
55 |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
56 data = clean(data) |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
57 |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
58 if data[0] != '#': |
15
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 raise exceptions.ValueError('data length header incorrect') |
17
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
60 dlenlen = int(data[1]) |
27
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
61 if dlenlen == 0: |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
62 return numpy.array([]) |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
63 |
17
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
64 dlen = int(data[2:dlenlen + 2]) |
27
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
65 if dlen == 0: |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
66 return numpy.array([]) |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
67 |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
68 wanted = dlen + 2 + dlenlen |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
69 # Chop off the trailing \n if necessary |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
70 if len(data) == wanted + 1 and data[-1] == '\n': |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
71 data = data[0:-1] |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
72 |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
73 if len(data) != wanted: |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
74 raise exceptions.ValueError('length mismatch, header says %d, actually got %d bytes' % (wanted, len(data))) |
17
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
75 return numpy.frombuffer(data[2 + dlenlen:], dtype = dtype) |
15
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
76 |
20
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
77 def ascdecode(data, dtype = numpy.float32, sep = None): |
15
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
78 '''Decode ASCII data, returns numpy array''' |
17
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
79 |
20df02be818a
Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents:
15
diff
changeset
|
80 data = clean(data) |
20
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
81 |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
82 # Strip length off if present as we don't need it |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
83 if data[0] == '#': |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
84 l = int(data[1]) |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
85 data = data[l + 2:] |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
86 |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
87 # Take a guess at the separator |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
88 if sep == None: |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
89 if data.find(',', 0, 20) != -1: |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
90 sep = ',' |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
91 else: |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
92 sep = ' ' |
a124aa7067e7
Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
93 return numpy.fromstring(data, dtype = dtype, sep = sep) |
15
37d6ceb4850f
Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
94 |
27
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
95 |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
96 def instURL(url): |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
97 m = insturlre.match(url) |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
98 if m == None: |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
99 raise exceptions.ValueError('Unable to parse URL') |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
100 proto, xxx, user, xxx, pwd, address, xxx, port = m.groups() |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
101 if proto == 'rsib': |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
102 import rsib |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
103 return rsib.RSIBDevice(address, port) |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
104 elif proto == 'usb': |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
105 import usb488 |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
106 return usb488.USB488Device() |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
107 elif proto == 'vxi': |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
108 import vxi |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
109 if port == None: |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
110 port = 'inst0' |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
111 return vxi.VXIDevice(address, device = port) |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
112 elif proto == 'scpi': |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
113 import scpisock |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
114 return scpisock.SCPISockDevice(address, port) |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
115 else: |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
116 raise exceptions.NotImplementedError("unknown protocol " + proto) |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
117 |
e2833d081413
- Handle zero length data from the instrument.
Daniel O'Connor <darius@dons.net.au>
parents:
20
diff
changeset
|
118 |