annotate tek2024b_example.py @ 33:e80c2ff5fa87

Add program to plot data files from sitesurvey.py
author Daniel O'Connor <darius@dons.net.au>
date Wed, 21 Sep 2011 15:34:49 +0930
parents 9bb8a9f3df6b
children 91b476ebc0f2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
7
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
3 # Copyright (c) 2009
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
5 #
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
6 # Redistribution and use in source and binary forms, with or without
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
7 # modification, are permitted provided that the following conditions
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
8 # are met:
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
9 # 1. Redistributions of source code must retain the above copyright
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
10 # notice, this list of conditions and the following disclaimer.
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
11 # 2. Redistributions in binary form must reproduce the above copyright
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
12 # notice, this list of conditions and the following disclaimer in the
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
13 # documentation and/or other materials provided with the distribution.
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
14 #
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
25 # SUCH DAMAGE.
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
26 #
813e183cfd49 Add 2 clause BSDL.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
27
3
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 import matplotlib.pylab as pylab
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 import numpy
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
30 import scpi
3
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 import time
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
32 import usb488
3
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
34 def test(u):
3
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 u.write("*IDN?")
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
36 print "IDN reports " + u.read()
3
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
38 # Set data format to 16 bit big endian signed
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
39 u.write("DATA:ENC RIB")
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
40 u.write("DATA:WIDTH 2")
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
41
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
42 # Turn channel 1 on
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
43 u.write("SELECT:CH1 ON")
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
44
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
45 # Set the curve source to channel 1
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
46 u.write("DATA:SOURCE CH1")
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
47
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
48 # Fetch channel scale
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
49 u.write("CH1:SCALE?")
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
50 vscale = float(u.read(1).split()[1])
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
51 print "Channel 1 scale is %.2f volts/div" % (vscale)
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
52
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
53 # Fetch horizontal scale
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
54 u.write("HOR:MAIN:SCALE?")
18
9bb8a9f3df6b Update examples to match new code.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
55 hscale = scpi.getdata(u.read())
9bb8a9f3df6b Update examples to match new code.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
56 print "Horizontal scale is %f nsec/div" % (hscale * 1e9)
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
57
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
58 # Make sure the previous commands have been executed
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
59 u.write("*WAI")
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
60
18
9bb8a9f3df6b Update examples to match new code.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
61 # Fetch curve data (wait up to 1 second) since the read takes a little while
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
62 u.write("CURVE?")
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
63 then = time.time()
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
64 result = u.read(1.0)
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
65 now = time.time()
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
66 print "CURVE read took %f milliseconds" % ((now - then) * 1000.0)
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
67
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
68 # Parse data
18
9bb8a9f3df6b Update examples to match new code.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
69 ary = scpi.bindecode(result, dtype = numpy.dtype('>h'))
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
70
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
71 # Convert to volts
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
72 ary = ary / 32768.0 * vscale
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
73
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
74 # Plot data
3
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 pylab.plot(ary)
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 pylab.show()
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 if __name__ == "__main__":
16
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
79 u = usb488.USB488Device()
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
80 print "Found device"
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
81
c2c13d804fce Rename examples, rework them to be clearer and use helper functions.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
82 test(u)