annotate usb488.py @ 58:0684a20cc5c3

Fix for Python 3
author Daniel O'Connor <doconnor@gsoft.com.au>
date Fri, 08 Jan 2021 14:03:52 +1030
parents 19045ad9f5f5
children dd27521002a5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
a43a47dfc902 First stab at code that actually works!
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
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 # Spec/info..
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 # http://www.usb.org/developers/devclass_docs/USBTMC_1_006a.zip
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 # http://svn.openmoko.org/developers/werner/ahrt/host/tmc/README
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 # http://www.home.agilent.com/agilent/redirector.jspx?action=ref&cname=AGILENT_EDITORIAL&ckey=1189335&lc=eng&cc=US&nfr=-35560.0.00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 # linux-2.6.29.3/drivers/usb/class/usbtmc.c
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
35 # http://sdpha2.ucsd.edu/Lab_Equip_Manuals/usbtmc_usb488_subclass_1_00.pdf
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 import usb
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
39 from functools import reduce
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 # The usual SCPI commands are wrapped before being sent.
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 # Write:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 # Offset Field Size Value Description
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 # 0 MsgID 1 0x01 DEV_DEP_MSG_OUT
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 # 1 bTag 1 0x01 Varies with each transfer
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 # 2 bTagInverse 1 0xfe Inverse of previous field
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 # 3 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 # 4 TransferSize 4 0x06
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 # 5 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 # 6 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 # 7 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 # 8 bmTransferAttr 1 0x01 1 == end of msg
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 # 9 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 # 10 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 # 11 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 # 12 Msg itself 1 0x2a '*'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 # 13 1 0x49 'I'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 # 14 1 0x44 'D'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 # 15 1 0x4e 'N'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 # 16 1 0x3f '?'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 # 17 1 0x0a '\n'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 # 18-19 Alignment 2 0x0000 Bring into 4 byte alignment
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 # Send a read request:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 # Offset Field Size Value Description
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 # 0 MsgID 1 0x02 REQUEST_DEV_DEP_MSG_IN
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 # 1 bTag 1 0x02 Varies with each transfer
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 # 2 bTagInverse 1 0xfd Inverse of previous field
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 # 3 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 # 4 TransferSize 4 0x64
9
a6b746654ae2 Fix comment mangle.
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
74 # 5 .. 0x00
a6b746654ae2 Fix comment mangle.
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
75 # 6 .. 0x00
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 # 7 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 # 8 bmTransferAttr 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 # 9 Term char 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 # 10 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 # 11 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 # No libusb versions of these available
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 USB_CLASS_APP_SPECIFIC = 254
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 USB_SUBCLASS_TMC = 3
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 USB_PROTOCOL_488 = 1
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 # USB488 message IDs
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 DEV_DEP_MSG_OUT = 1
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 REQUEST_DEV_DEP_MSG_IN = 2
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 DEV_DEP_MSG_IN = 2
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 class USB488Device(object):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 def __init__(self, vendor = None, product = None, serial = None, path = None):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 """Search for a USB488 class device, if specified vendor,
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 product, serial and path will refine the search"""
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 busses = usb.busses()
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 # Search for the device we want
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 found = False
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 for bus in busses:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 for dev in bus.devices:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 # Skip ones that don't match
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 if vendor != None and dev.idVendor != vendor:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 if product != None and dev.idProduct != product:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 if serial != None and dev.idSerialNumber != serial:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 if path != None and dev.filename != path:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 # The libusb examples say you can check for device
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 # class and then open, however in that case you can't
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 # find the endpoint number which seems pretty useless
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 # unless you want to hard code everything.
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
119 for confidx in range(len(dev.configurations)):
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 for iface in dev.configurations[confidx].interfaces:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 for altif in iface:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 # Check if this is a USB488 capable interface
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 if altif.interfaceClass == USB_CLASS_APP_SPECIFIC and \
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 altif.interfaceSubClass == USB_SUBCLASS_TMC and \
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 altif.interfaceProtocol == USB_PROTOCOL_488:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 found = True
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 if not found:
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
137 raise BaseException("Could not find a suitable USB device")
57
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
138
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 # Open the device and claim the USB interface that supports the spec
8
381ed6f3d2d7 Save handle after using it, makes the code look nicer.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
140 handle = dev.open()
381ed6f3d2d7 Save handle after using it, makes the code look nicer.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
141 handle.setConfiguration(dev.configurations[confidx].value)
381ed6f3d2d7 Save handle after using it, makes the code look nicer.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
142 handle.claimInterface(altif.interfaceNumber)
381ed6f3d2d7 Save handle after using it, makes the code look nicer.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
143 handle.setAltInterface(altif.alternateSetting)
381ed6f3d2d7 Save handle after using it, makes the code look nicer.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
144 self.dev = dev
381ed6f3d2d7 Save handle after using it, makes the code look nicer.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
145 self.handle = handle
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 # Get some info for humans
57
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
148 try:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
149 self.vendname = handle.getString(dev.iManufacturer, 1024)
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
150 except ValueError:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
151 self.vendname = None
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
152 try:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
153 self.prodname = handle.getString(dev.iProduct, 1024)
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
154 except ValueError:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
155 self.prodname = None
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
156 try:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
157 self.serial = handle.getString(dev.iSerialNumber, 1024)
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
158 except ValueError:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
159 self.serial = None
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 # Determine the endpoints for each operation type
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 self.intrep = self.bulkinep = self.bulkoutep = None
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 for ep in altif.endpoints:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 if ep.type == usb.ENDPOINT_TYPE_INTERRUPT and \
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 self.intrep = ep.address
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
168
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 if ep.type == usb.ENDPOINT_TYPE_BULK:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 if ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 self.bulkinep = ep.address
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 else:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 self.bulkoutep = ep.address
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 self.maxPacket = ep.maxPacketSize
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 # Required for 488.2 devices, optional otherwise
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 if self.intrep == None:
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
178 print("Can't find interrupt endpoint")
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 # Data from the scope (mandatory)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 if self.bulkinep == None:
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
182 raise BaseException("Can't find bulk-in endpoint")
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 # Data to the scope (mandatory)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 if self.bulkoutep == None:
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
186 raise BaseException("Can't find bulk-out endpoint")
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 self.tag = 1
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 def __str__(self):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 rtn = "Mfg: %s Prod: %s" % (self.vendname, self.prodname)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 if self.serial != "":
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 rtn += " S/N: " + self.serial
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 return rtn
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 def incrtag(self):
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
198 self.tag = (self.tag + 1) % 255
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 if self.tag == 0:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 self.tag += 1
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 def write(self, data):
12
5ff9130cc953 This code isn't scope specific.
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
203 """Send data (string) to the instrument"""
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
204
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
205 orddata = list(map(ord, data))
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
206 # The device needs a \n at the end, enfore this
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
207 if orddata[-1] != '\n':
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
208 orddata += [ord('\n')]
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209 datalen = len(orddata)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 # Build the packet
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 pkt = [ DEV_DEP_MSG_OUT, self.tag, ~self.tag & 0xff, 0x00,
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 datalen & 0xff, datalen >> 8 & 0xff, datalen >> 16 & 0xff,
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 datalen >> 24 & 0xff, 1, 0, 0, 0 ]
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 # Add the data
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 pkt = pkt + orddata
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 # Align to 4 bytes
58
0684a20cc5c3 Fix for Python 3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 57
diff changeset
220 alignlen = ((len(pkt) // 4) + 1) * 4
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 pkt = pkt + [0] * (alignlen - len(pkt))
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 # Bump the tag
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 self.incrtag()
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 # Split it up into maxPacket sized chunks and send..
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 while len(pkt) > 0:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 chunk = pkt[0:self.maxPacket]
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 pkt = pkt[self.maxPacket:]
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
231 #print "Sending %s bytes of data: %s" % (len(chunk), chunk)
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232 wrote = self.handle.bulkWrite(self.bulkoutep, chunk)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 if wrote != len(chunk):
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
234 raise BaseException("Short write, got %d, expected %d" % (wrote, len(chunk)))
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235
23
876d951bbcc0 Redo timeout in a more sensible way (also change it to 100ms)
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
236 def read(self, timeout = None):
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
237 """Read data from the device, waits for up to timeout seconds for each USB transaction"""
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
238
23
876d951bbcc0 Redo timeout in a more sensible way (also change it to 100ms)
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
239 if timeout == None:
876d951bbcc0 Redo timeout in a more sensible way (also change it to 100ms)
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
240 timeout = 0.1
876d951bbcc0 Redo timeout in a more sensible way (also change it to 100ms)
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
241
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
242 # Mangle into milliseconds
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
243 _timeout = int(timeout * 1000.0)
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
245 # Maximum we accept at once
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
246 # Was 2^31 - 1 but that seems to make things take too long to
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
247 # read (perhaps libusb tries to malloc it..)
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
248 datalen = 10240
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
249 data = []
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
250
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
251 while True:
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
252 # Ask the device to send us something
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
253 pkt = [ REQUEST_DEV_DEP_MSG_IN, self.tag, ~self.tag & 0xff, 0x00,
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
254 datalen & 0xff, datalen >> 8 & 0xff, datalen >> 16 & 0xff,
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
255 datalen >> 24 & 0xff, 0, 0, 0, 0]
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
257 # Expected tag
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
258 exptag = self.tag
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
259
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
260 # Bump tag
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
261 self.incrtag()
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
263 # Send it
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
264 #print "Sending " + str(pkt)
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
265 wrote = self.handle.bulkWrite(self.bulkoutep, pkt, _timeout)
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
266 if wrote != len(pkt):
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
267 print("Short write, got %d, expected %d" % (wrote, len(pkt)))
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
269 #print "Reading.."
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
270 read = self.handle.bulkRead(self.bulkinep, datalen, _timeout)
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
271 #print "Read %s bytes: %s" % (len(read), str(read))
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
272
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
273 if read[0] != DEV_DEP_MSG_IN:
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
274 raise BaseException("Unexpected Msg ID, got %s expected %d" % (read[0], DEV_DEP_MSG_IN))
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
275 if read[1] != exptag:
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
276 raise BaseException("Unexpected tag, got %d expected %d" % (read[1], exptag))
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
277 if read[2] != ~exptag & 0xff:
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
278 raise BaseException("Unexpected tag inverse, got %d expected %d" % (read[1], ~exptag & 0xff))
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
280 actualdata = read[4] | read[5] << 8 | read[6] << 16 | read[7] << 24
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
281 #print "Computed datalen is %d" % (actualdata)
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
282 data += read[12:12 + actualdata]
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
283 if read[8] & 0x01:
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
284 #print "Breaking out due to EOM"
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
285 break
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
286
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
287 # Stringify result for easier consumption
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
288 result = reduce(lambda x, y: x+y, list(map(chr, data)))
1
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
289 # Trim off \n if present
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
290 if result[-1] == '\n':
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
291 result = result[0:-1]
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
292
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
293 return result
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
294
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
295 def ask(self, s, timeout = None):
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
296 self.write(s)
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
297 return self.read(timeout = None)
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
298
11
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
299 def isConnected(self):
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
300 """Check if the device is present"""
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
301
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
302 # libusb makes it very hard (at least on FreeBSD) to determine if we're still connected.
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
303 # This is a reasonable proxy..
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
304 try:
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
305 self.handle.getString(self.dev.iManufacturer, 100)
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
306 except USBError as e:
11
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
307 return False
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
308
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
309 return True
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
310