annotate scpi.py @ 21:6e7619f2dffd

Remove the test now it's in a separate file.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 11 Aug 2011 21:17:42 +0930
parents a124aa7067e7
children e2833d081413
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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:]+ ).*')
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
33
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
34 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
35 # 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
36 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
37 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
38 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
39 else:
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
40 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
41 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
42
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
43 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
44 '''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
45 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
46 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
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 bindecode(data, dtype = numpy.float32):
15
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 '''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
50
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
51 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
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 if data[0] != '#':
15
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 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
55 dlenlen = int(data[1])
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
56 dlen = int(data[2:dlenlen + 2])
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
57 if len(data) != dlen + 2 + dlenlen:
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
58 raise exceptions.ValueError('length mismatch, header says %d, actually got %d bytes' % (dlen + 2 + dlenlen, len(data)))
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
59 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
60
20
a124aa7067e7 Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
61 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
62 '''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
63
20df02be818a Work out the header using an RE and scrub it automatically.
Daniel O'Connor <darius@dons.net.au>
parents: 15
diff changeset
64 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
65
a124aa7067e7 Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
66 # 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
67 if data[0] == '#':
a124aa7067e7 Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
68 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
69 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
70
a124aa7067e7 Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
71 # 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
72 if sep == None:
a124aa7067e7 Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
73 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
74 sep = ','
a124aa7067e7 Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
75 else:
a124aa7067e7 Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
76 sep = ' '
a124aa7067e7 Make ascdecode smarter so it can handle Anritsu ASCII.
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
77 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
78