annotate scpi.py @ 16:c2c13d804fce

Rename examples, rework them to be clearer and use helper functions.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 10 Aug 2011 12:51:07 +0930
parents 37d6ceb4850f
children 20df02be818a
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
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 def bindecode(data, header = '', dtype = numpy.float32):
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 '''Decode binary data, returns numpy array'''
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 if data[0:len(header)] != header:
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 raise exceptions.ValueError('data header incorrect')
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 ofs = len(header)
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 if data[ofs] != '#':
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 raise exceptions.ValueError('data length header incorrect')
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 dlenlen = int(data[ofs + 1])
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 dlen = int(data[ofs + 2:ofs + dlenlen + 2])
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 if len(data) != len(header) + dlen + 2 + dlenlen:
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 raise exceptions.ValueError('length mismatch, header says %d, actually got %d bytes' % (len(header) + dlen + 2 + dlenlen, len(data)))
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 return numpy.frombuffer(data[len(header) + 2 + dlenlen:], dtype = dtype)
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 def ascdecode(data, dtype = numpy.float32):
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 '''Decode ASCII data, returns numpy array'''
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 return numpy.fromstring(data, dtype = dtype, sep = ',')
37d6ceb4850f Add helper functions for SCPI.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47