Mercurial > ~darius > hgwebdir.cgi > pyinst
annotate usb488.py @ 65:29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
author | Daniel O'Connor <doconnor@gsoft.com.au> |
---|---|
date | Tue, 19 Jan 2021 16:06:29 +1030 |
parents | 4558e5ccd775 |
children | bf411c7f5e78 |
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 | 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 | |
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 |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
38 import time |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 import usb |
56 | 40 from functools import reduce |
0
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 # |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
43 # 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
|
44 # |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
45 # Write: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
46 # Offset Field Size Value Description |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 # 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
|
48 # 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
|
49 # 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
|
50 # 3 Reserved 1 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
51 # 4 TransferSize 4 0x06 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
52 # 5 .. 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
53 # 6 .. 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
54 # 7 .. 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
55 # 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
|
56 # 9 Reserved 1 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
57 # 10 Reserved 1 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
58 # 11 Reserved 1 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 # 12 Msg itself 1 0x2a '*' |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
60 # 13 1 0x49 'I' |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 # 14 1 0x44 'D' |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
62 # 15 1 0x4e 'N' |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
63 # 16 1 0x3f '?' |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
64 # 17 1 0x0a '\n' |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
65 # 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
|
66 # |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
67 # |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
68 # Send a read request: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
69 # Offset Field Size Value Description |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
70 # 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
|
71 # 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
|
72 # 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
|
73 # 3 Reserved 1 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
74 # 4 TransferSize 4 0x64 |
9 | 75 # 5 .. 0x00 |
76 # 6 .. 0x00 | |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
77 # 7 .. 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
78 # 8 bmTransferAttr 1 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
79 # 9 Term char 1 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
80 # 10 Reserved 1 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
81 # 11 Reserved 1 0x00 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
82 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
83 # No libusb versions of these available |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
84 USB_CLASS_APP_SPECIFIC = 254 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
85 USB_SUBCLASS_TMC = 3 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
86 USB_PROTOCOL_488 = 1 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
87 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
88 # USB488 message IDs |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
89 DEV_DEP_MSG_OUT = 1 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
90 REQUEST_DEV_DEP_MSG_IN = 2 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
91 DEV_DEP_MSG_IN = 2 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
92 |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
93 # USB TMC control requests |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
94 INITIATE_ABORT_BULK_OUT = 1 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
95 CHECK_ABORT_BULK_OUT_STATUS = 2 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
96 INITIATE_ABORT_BULK_IN = 3 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
97 CHECK_ABORT_BULK_IN_STATUS = 4 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
98 INITIATE_CLEAR = 5 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
99 CHECK_CLEAR_STATUS = 6 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
100 GET_CAPABILITIES = 7 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
101 INDICATOR_PULSE = 64 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
102 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
103 # Interface capability bits |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
104 IF_CAP_LISTEN_ONLY = 0x01 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
105 IF_CAP_TALK_ONLY = 0x02 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
106 IF_CAP_HAS_INDICATOR = 0x04 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
107 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
108 # Device capability bits |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
109 DEV_CAP_TERM_CHAR = 0x01 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
110 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
111 # USBTMC status definitions |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
112 STATUS_SUCCESS = 0x01 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
113 STATUS_PENDING = 0x02 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
114 STATUS_FAILED = 0x80 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
115 STATUS_TRANSFER_NOT_IN_PROGRESS = 0x81 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
116 STATUS_SPLIT_NOT_IN_PROGRESS = 0x82 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
117 STATUS_SPLIT_IN_PROGRESS = 0x83 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
118 |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
119 class USB488Device(object): |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
120 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
|
121 """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
|
122 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
|
123 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
124 busses = usb.busses() |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
125 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
126 # |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
127 # Search for the device we want |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
128 # |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
129 found = False |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
130 for bus in busses: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
131 for dev in bus.devices: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
132 # Skip ones that don't match |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
133 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
|
134 continue |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
135 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
|
136 continue |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
137 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
|
138 continue |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
139 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
|
140 continue |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
141 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
142 # 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
|
143 # 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
|
144 # 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
|
145 # unless you want to hard code everything. |
56 | 146 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
|
147 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
|
148 for altif in iface: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
149 # 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
|
150 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
|
151 altif.interfaceSubClass == USB_SUBCLASS_TMC and \ |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
152 altif.interfaceProtocol == USB_PROTOCOL_488: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
153 found = True |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
154 break |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
155 if found: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
156 break |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
157 if found: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
158 break |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
159 if found: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
160 break |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
161 if found: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
162 break |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
163 if not found: |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
164 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
|
165 |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
166 # 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 self.handle = handle |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
173 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
174 # 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
|
175 try: |
19045ad9f5f5
Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
176 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
|
177 except ValueError: |
19045ad9f5f5
Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
178 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
|
179 try: |
19045ad9f5f5
Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
180 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
|
181 except ValueError: |
19045ad9f5f5
Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
182 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
|
183 try: |
19045ad9f5f5
Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
184 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
|
185 except ValueError: |
19045ad9f5f5
Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
56
diff
changeset
|
186 self.serial = None |
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 # 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
|
189 self.intrep = self.bulkinep = self.bulkoutep = None |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
190 |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
191 for ep in altif.endpoints: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
192 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
|
193 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
|
194 self.intrep = ep.address |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
195 |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
196 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
|
197 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
|
198 self.bulkinep = ep.address |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
199 else: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
200 self.bulkoutep = ep.address |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
201 self.maxPacket = ep.maxPacketSize |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
202 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
203 # 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
|
204 if self.intrep == None: |
56 | 205 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
|
206 |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
207 # Data from the device (mandatory) |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
208 if self.bulkinep == None: |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
209 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
|
210 |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
211 # Data to the device (mandatory) |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
212 if self.bulkoutep == None: |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
213 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
|
214 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
215 self.tag = 1 |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
216 self.initiateClear() |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
217 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
218 def __str__(self): |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
219 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
|
220 if self.serial != "": |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
221 rtn += " S/N: " + self.serial |
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 return rtn |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
224 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
225 def incrtag(self): |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
226 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
|
227 if self.tag == 0: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
228 self.tag += 1 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
229 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
230 def write(self, data): |
12
5ff9130cc953
This code isn't scope specific.
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
231 """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
|
232 |
56 | 233 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
|
234 # 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
|
235 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
|
236 orddata += [ord('\n')] |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
237 datalen = len(orddata) |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
238 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
239 # Build the packet |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
240 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
|
241 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
|
242 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
|
243 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
244 # Add the data |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
245 pkt = pkt + orddata |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
246 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
247 # Align to 4 bytes |
58 | 248 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
|
249 pkt = pkt + [0] * (alignlen - len(pkt)) |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
250 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
251 # Bump the tag |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
252 self.incrtag() |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
253 |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
254 # 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
|
255 while len(pkt) > 0: |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
256 chunk = pkt[0:self.maxPacket] |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
257 pkt = pkt[self.maxPacket:] |
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
258 |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
259 #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
|
260 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
|
261 if wrote != len(chunk): |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
262 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
|
263 |
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
|
264 def read(self, timeout = None): |
5
51d1fc44a753
Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
265 """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
|
266 |
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
|
267 if timeout == None: |
59
dd27521002a5
Change default timeout to 500msec
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
58
diff
changeset
|
268 timeout = 0.5 |
dd27521002a5
Change default timeout to 500msec
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
58
diff
changeset
|
269 |
5
51d1fc44a753
Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
270 # Mangle into milliseconds |
51d1fc44a753
Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
271 _timeout = int(timeout * 1000.0) |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
272 |
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
|
273 # 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
|
274 # 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
|
275 # 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
|
276 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
|
277 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
|
278 |
e2089824735a
Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
279 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
|
280 # 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
|
281 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
|
282 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
|
283 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
|
284 |
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
|
285 # 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
|
286 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
|
287 |
e2089824735a
Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
288 # 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
|
289 self.incrtag() |
0
a43a47dfc902
First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
290 |
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
|
291 # Send it |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
292 #print("Sending " + str(pkt)) |
5
51d1fc44a753
Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
293 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
|
294 if wrote != len(pkt): |
56 | 295 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
|
296 |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
297 #print("Reading..") |
5
51d1fc44a753
Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
298 read = self.handle.bulkRead(self.bulkinep, datalen, _timeout) |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
299 #print("Read %s bytes: %s" % (len(read), str(read))) |
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
|
300 |
e2089824735a
Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
301 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
|
302 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
|
303 if read[1] != exptag: |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
304 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
|
305 if read[2] != ~exptag & 0xff: |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
306 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
|
307 |
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
|
308 actualdata = read[4] | read[5] << 8 | read[6] << 16 | read[7] << 24 |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
309 #print("Computed datalen is %d" % (actualdata)) |
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
|
310 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
|
311 if read[8] & 0x01: |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
312 #print("Breaking out due to EOM") |
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
|
313 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
|
314 |
e2089824735a
Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
315 # Stringify result for easier consumption |
56 | 316 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
|
317 # 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
|
318 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
|
319 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
|
320 |
e2089824735a
Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
321 return result |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
322 |
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
323 def ask(self, s, timeout = None): |
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
324 self.write(s) |
60
4558e5ccd775
Honour timeout passed into ask()
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
59
diff
changeset
|
325 return self.read(timeout = timeout) |
55
ad5942d22f78
Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
23
diff
changeset
|
326 |
11
f588f5bc834a
Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
327 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
|
328 """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
|
329 |
f588f5bc834a
Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
330 # 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
|
331 # 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
|
332 try: |
f588f5bc834a
Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
333 self.handle.getString(self.dev.iManufacturer, 100) |
56 | 334 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
|
335 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
|
336 |
f588f5bc834a
Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
337 return True |
65
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
338 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
339 def getCapabilities(self): |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
340 '''Returns interface and device capability bytes (see IF_CAP_* and DEV_CAP_*)''' |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
341 res = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, GET_CAPABILITIES, 0x18) |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
342 return res[4], res[5] |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
343 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
344 def indicatorPulse(self): |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
345 '''Send an indicator pulse request''' |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
346 res = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, INDICATOR_PULSE, 0x01) |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
347 |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
348 def initiateClear(self): |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
349 '''Send request to clear all transfers and wait until the device reports it is done and clear stalls''' |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
350 res = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, INITIATE_CLEAR, 0x01) |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
351 if res[0] == STATUS_SUCCESS: |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
352 while True: |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
353 res = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, CHECK_CLEAR_STATUS, 0x02) |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
354 if res[0] != STATUS_PENDING: |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
355 break |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
356 time.sleep(0.1) |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
357 self.handle.clearHalt(usb.ENDPOINT_IN | self.bulkinep) |
29bcef559283
Add initiateClear and a few others and use it to clear buffers on setup.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
60
diff
changeset
|
358 self.handle.clearHalt(usb.ENDPOINT_OUT | self.bulkoutep) |