annotate example.py @ 5:51d1fc44a753

Add variable timeout for reads. Modify the example to use it & print how long curve takes.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 13 May 2009 17:17:01 +0930
parents 62ffab79227e
children 813e183cfd49
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
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 import usb488
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 import matplotlib.pylab as pylab
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 import numpy
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import time
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 def main():
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 u = usb488.USB488Device()
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 print "Found device"
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 u.write("*IDN?")
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
13 print "IDN reports " + u.read()
3
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 u.write("DATA:ENC RIB") # Big endian signed
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 u.write("DATA:WIDTH 2") # 2 bytes wide
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 u.write("SELECT:CH1 ON") # Turn channel 1 on
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 u.write("DATA:SOURCE CH1") # Set the curve source to channel 1
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 u.write("CURVE?") # Ask for the curve data
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
20 then = time.time()
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
21 result = u.read(1.0) # Takes the CRO a while for this
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
22 now = time.time()
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
23 print "CURVE read took %f milliseconds" % ((now - then) * 1000.0)
3
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 data = result[13:] # Chop off the header
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 dattype = numpy.dtype('>h') # Big endian 16 bit quantity
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 ary = numpy.fromstring(data, dtype = dattype)
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 pylab.plot(ary)
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 pylab.show()
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 if __name__ == "__main__":
62ffab79227e Add example.py with pylab plotting example.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 main()