annotate example.py @ 78:47da95b31dcb

Move sifmt function to where it can be reused.
author Daniel O'Connor <doconnor@gsoft.com.au>
date Fri, 27 Sep 2024 16:50:42 +0930
parents 4ca9fdf0795a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
64
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
1 #!/usr/bin/env python
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
2
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
3 # Copyright (c) 2009
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
5 #
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
6 # Redistribution and use in source and binary forms, with or without
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
7 # modification, are permitted provided that the following conditions
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
8 # are met:
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
9 # 1. Redistributions of source code must retain the above copyright
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
10 # notice, this list of conditions and the following disclaimer.
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
11 # 2. Redistributions in binary form must reproduce the above copyright
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
12 # notice, this list of conditions and the following disclaimer in the
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
13 # documentation and/or other materials provided with the distribution.
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
14 #
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
25 # SUCH DAMAGE.
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
26 #
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
27
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
28 import usb488
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
29 import matplotlib.pylab as pylab
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
30 import numpy
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
31 import time
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
32
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
33 def main():
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
34 u = usb488.USB488Device()
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
35 print("Found device")
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
36
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
37 u.write("*IDN?")
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
38 print("IDN reports " + u.read())
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
39
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
40 u.write("DATA:ENC RIB") # Big endian signed
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
41 u.write("DATA:WIDTH 2") # 2 bytes wide
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
42 u.write("SELECT:CH1 ON") # Turn channel 1 on
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
43 u.write("DATA:SOURCE CH1") # Set the curve source to channel 1
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
44
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
45 u.write("CH1:SCALE?")
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
46 vscale = float(u.read(1).split()[1])
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
47 print("Channel 1 scale is %.2f volts/div" % (vscale))
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
48 u.write("HOR:MAIN:SCALE?")
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
49 hscale = float(u.read(1).split()[1])
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
50 print("Horizontal scale is %f sec/div" % (hscale))
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
51
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
52 u.write("*WAI") # Make sure the previous commands have been
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
53 # executed
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
54 u.write("CURVE?") # Ask for the curve data
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
55 then = time.time()
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
56 result = u.read(1.0) # Takes the CRO a while for this
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
57 now = time.time()
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
58 print("CURVE read took %f milliseconds" % ((now - then) * 1000.0))
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
59 data = result[13:] # Chop off the header (should verify this really..)
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
60 dattype = numpy.dtype('>h') # Big endian 16 bit quantity
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
61 ary = numpy.fromstring(data, dtype = dattype)
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
62 ary = ary / 32768.0 * vscale
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
63 pylab.plot(ary)
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
64 pylab.show()
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
65
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
66 if __name__ == "__main__":
4ca9fdf0795a Add simple scope example
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
diff changeset
67 main()