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