annotate usb488.py @ 71:00800345fbae default tip

Python3ise RSIB code
author Daniel O'Connor <doconnor@gsoft.com.au>
date Thu, 24 Aug 2023 16:52:10 +0930
parents 7386f2888508
children
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
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
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
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
a6b746654ae2 Fix comment mangle.
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
75 # 5 .. 0x00
a6b746654ae2 Fix comment mangle.
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
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
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
102 # USB488
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
103 READ_STATUS_BYTE = 128
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
104 REN_CONTROL = 160
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
105 GO_TO_LOCAL = 161
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
106 LOCAL_LOCKOUT = 162
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
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 # 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
109 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
110 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
111 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
112
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 # 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
114 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
115
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
116 # USB488 interface capbility bits
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
117 USB488_IFCAP_TRIG = 0x01
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
118 USB488_IFCAP_GO_LOC = 0x02
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
119 USB488_IFCAP_488_2 = 0x04
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
120
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
121 # USB488 device capbility bits
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
122 USB488_DEVCAP_DT1 = 0x01
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
123 USB488_DEVCAP_RL1 = 0x02
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
124 USB488_DEVCAP_SR1 = 0x04
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
125 USB488_DEVCAP_SCPI = 0x08
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
126
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
127 # 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
128 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
129 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
130 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
131 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
132 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
133 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
134
66
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
135 # SCPI error/event queue status register bit
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
136 STATUS_EVE_QUEUE = 0x04
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
137
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 class USB488Device(object):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 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
140 """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
141 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
142
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 busses = usb.busses()
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 # Search for the device we want
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 found = False
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 for bus in busses:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 for dev in bus.devices:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 # Skip ones that don't match
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 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
153 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 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
155 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 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
157 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 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
159 continue
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 # 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
162 # 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
163 # 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
164 # unless you want to hard code everything.
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
165 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
166 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
167 for altif in iface:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 # 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
169 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
170 altif.interfaceSubClass == USB_SUBCLASS_TMC and \
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 altif.interfaceProtocol == USB_PROTOCOL_488:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 found = True
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 if not found:
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
183 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
184
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 # 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
186 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
187 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
188 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
189 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
190 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
191 self.handle = handle
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 # 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
194 try:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
195 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
196 except ValueError:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
197 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
198 try:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
199 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
200 except ValueError:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
201 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
202 try:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
203 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
204 except ValueError:
19045ad9f5f5 Make it more tolerant if we can't read strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 56
diff changeset
205 self.serial = None
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 # 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
208 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
209
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210 for ep in altif.endpoints:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 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
212 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
213 self.intrep = ep.address
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
214
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 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
216 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
217 self.bulkinep = ep.address
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 else:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 self.bulkoutep = ep.address
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 self.maxPacket = ep.maxPacketSize
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 # 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
223 if self.intrep == None:
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
224 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
225
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
226 # Data from the device (mandatory)
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 if self.bulkinep == None:
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
228 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
229
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
230 # Data to the device (mandatory)
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 if self.bulkoutep == None:
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
232 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
233
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 self.tag = 1
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
235 #self.init()
66
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
236
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
237 def init(self):
66
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
238 # Flush out any pending data
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
239 self.initiateClear()
66
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
240 # Perform dummy write/read otherwise the next read times out
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
241 try:
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
242 self.ask('*STB?', timeout = 0.001)
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
243 except usb.USBError:
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
244 pass
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
245 # Clear status register
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
246 for i in range(10):
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
247 self.write('*CLS')
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
248 if int(self.ask('*STB?')) & STATUS_EVE_QUEUE == 0:
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
249 break
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
250 else:
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
251 raise BaseException('Unable to clear status register')
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 def __str__(self):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254 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
255 if self.serial != "":
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 rtn += " S/N: " + self.serial
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 return rtn
66
bf411c7f5e78 Perform a dummy write/read after initiateClear and check the event/error queue bit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 65
diff changeset
259
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
260 def gettag(self):
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
261 tag = self.tag
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
262 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
263 if self.tag == 0:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264 self.tag += 1
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
265 return tag
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267 def write(self, data):
12
5ff9130cc953 This code isn't scope specific.
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
268 """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
269
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
270 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
271 # 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
272 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
273 orddata += [ord('\n')]
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 datalen = len(orddata)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276 # Build the packet
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
277 tag = self.gettag()
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
278 pkt = [ DEV_DEP_MSG_OUT, tag, ~tag & 0xff, 0x00,
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279 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
280 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
281
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
282 # Add the data
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
283 pkt = pkt + orddata
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
285 # Align to 4 bytes
58
0684a20cc5c3 Fix for Python 3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 57
diff changeset
286 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
287 pkt = pkt + [0] * (alignlen - len(pkt))
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
288
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289 # Split it up into maxPacket sized chunks and send..
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
290 # XXX; this is not correct, need a header for each one
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
291 while len(pkt) > 0:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
292 chunk = pkt[0:self.maxPacket]
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
293 pkt = pkt[self.maxPacket:]
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
294
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
295 #print("Sending %d 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
296 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
297 if wrote != len(chunk):
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
298 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
299
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
300 def read(self, timeout = None):
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
301 """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
302
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
303 if timeout == None:
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
304 timeout = 1
59
dd27521002a5 Change default timeout to 500msec
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 58
diff changeset
305
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
306 # Mangle into milliseconds
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
307 _timeout = int(timeout * 1000.0)
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
308
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
309 # 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
310 # 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
311 # 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
312 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
313 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
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 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
316 # Ask the device to send us something
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
317 tag = self.gettag()
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
318 pkt = [ REQUEST_DEV_DEP_MSG_IN, tag, ~tag & 0xff, 0x00,
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
319 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
320 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
321
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
322 # 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
323 #print("Sending " + str(pkt))
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
324 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
325 if wrote != len(pkt):
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
326 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
327
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
328 #print("Reading..")
5
51d1fc44a753 Add variable timeout for reads.
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
329 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
330 #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
331
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
332 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
333 raise BaseException("Unexpected Msg ID, got %s expected %d" % (read[0], DEV_DEP_MSG_IN))
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
334 if read[1] != tag:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
335 raise BaseException("Unexpected tag, got %d expected %d" % (read[1], tag))
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
336 if read[2] != ~tag & 0xff:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
337 raise BaseException("Unexpected tag inverse, got %d expected %d" % (read[1], ~tag & 0xff))
0
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
338
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
339 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
340 #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
341 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
342 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
343 #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
344 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
345
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
346 # Stringify result for easier consumption
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
347 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
348 # 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
349 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
350 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
351
e2089824735a Make the read support work properly & check what the device gives us.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
352 return result
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
353
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
354 def ask(self, s, timeout = None):
67
98b9258c75b6 Add chkcmd helper to execute command check STB to see if it failed.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 66
diff changeset
355 '''Wrapper to send a command and wait for a reply'''
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
356 self.write(s)
60
4558e5ccd775 Honour timeout passed into ask()
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 59
diff changeset
357 return self.read(timeout = timeout)
55
ad5942d22f78 Use BaseException rather than strings.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 23
diff changeset
358
67
98b9258c75b6 Add chkcmd helper to execute command check STB to see if it failed.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 66
diff changeset
359 def chkcmd(self, cmd):
98b9258c75b6 Add chkcmd helper to execute command check STB to see if it failed.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 66
diff changeset
360 '''Wrapper to send a command and check for an error'''
98b9258c75b6 Add chkcmd helper to execute command check STB to see if it failed.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 66
diff changeset
361 self.write(cmd)
98b9258c75b6 Add chkcmd helper to execute command check STB to see if it failed.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 66
diff changeset
362 if int(self.ask('*STB?')) & STATUS_EVE_QUEUE != 0:
98b9258c75b6 Add chkcmd helper to execute command check STB to see if it failed.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 66
diff changeset
363 self.write('*CLS')
98b9258c75b6 Add chkcmd helper to execute command check STB to see if it failed.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 66
diff changeset
364 raise BaseException('Command failed')
98b9258c75b6 Add chkcmd helper to execute command check STB to see if it failed.
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 66
diff changeset
365
11
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
366 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
367 """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
368
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
369 # 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
370 # 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
371 try:
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
372 self.handle.getString(self.dev.iManufacturer, 100)
56
91b476ebc0f2 Run through 2to3
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 55
diff changeset
373 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
374 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
375
f588f5bc834a Add a method to determine if the device is still connected.
Daniel O'Connor <darius@dons.net.au>
parents: 10
diff changeset
376 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
377
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
378 def getCapabilities(self):
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
379 '''Returns interface, device and USB488 capability bytes (see IF_CAP_*, DEV_CAP_*, USB488_IFCAP_* and USB488_DEVCAP_*)'''
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
380 res = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, GET_CAPABILITIES, 0x18)
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
381 return res[4], res[5], res[14], res[15]
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
382
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
383 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
384 '''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
385 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
386
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
387 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
388 '''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
389 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
390 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
391 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
392 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
393 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
394 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
395 time.sleep(0.1)
69
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
396 else:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
397 raise BaseException('INITIATE_CLEAR returned 0x%02x' % (res[0]))
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
398 self.handle.clearHalt(self.bulkinep)
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
399 self.handle.clearHalt(self.bulkoutep)
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
400
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
401 def renControl(self):
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
402 '''Send enable remote control message'''
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
403 res = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, REN_CONTROL, 1, 0xff)
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
404 return res[0]
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
405
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
406 def getStatus(self):
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
407 '''Returns IEEE 488 status byte'''
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
408 tag = self.gettag()
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
409 res = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_INTERFACE, READ_STATUS_BYTE, 3, tag)
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
410 if res[1] != tag:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
411 raise BaseException('Tag mismatch, got 0x%02x, expected 0x%02x' % (res[1], tag))
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
412 if res[0] != STATUS_SUCCESS:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
413 raise BaseException('Unit returned invalid USBTMC status: %d' % (res[0],))
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
414 return res[2]
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
415
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
416 def abortIO(self, tag, isout):
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
417 if isout:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
418 req = INITIATE_ABORT_BULK_OUT
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
419 chkreq = CHECK_ABORT_BULK_OUT_STATUS
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
420 ep = self.bulkoutep
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
421 name = 'out'
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
422 else:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
423 req = INITIATE_ABORT_BULK_IN
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
424 chkreq = CHECK_ABORT_BULK_IN_STATUS
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
425 ep = self.bulkinep
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
426 name = 'in'
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
427 res = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_ENDPOINT,
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
428 req, 2, value = tag, index = ep)
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
429 print('Initiate abort returned ' + str(res))
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
430 while True:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
431 res = self.handle.controlMsg(usb.ENDPOINT_IN | usb.TYPE_CLASS | usb.RECIP_ENDPOINT,
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
432 chkreq, 8, value = 0x00, index = ep)
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
433 print('Check abort returned ' + str(res))
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
434 if res[0] == STATUS_PENDING:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
435 print('Status pending for %s abort' % (name,))
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
436 time.sleep(1)
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
437 elif res[0] == STATUS_SUCCESS or res[0] == STATUS_TRANSFER_NOT_IN_PROGRESS:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
438 break
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
439 else:
7386f2888508 Make function more configurable
Daniel O'Connor <doconnor@gsoft.com.au>
parents: 67
diff changeset
440 raise BaseException('Invalid status reply to check abort %s 0x%02x' % (name, res[0]))