annotate usb488.py @ 0:a43a47dfc902

First stab at code that actually works! Example: import usb488 u = usb488.USB488Device() u.write("*IDN?\n") Sending 20 bytes of data: [1, 1, 254, 0, 6, 0, 0, 0, 1, 0, 0, 0, 42, 73, 68, 78, 63, 10, 0, 0] a = u.read() Read 60 bytes: (2, 2, 253, 0, 48, 0, 0, 0, 1, 0, 0, 0, 84, 69, 75, 84, 82, 79, 78, 73, 88, 44, 84, 68, 83, 32 , 50, 48, 50, 52, 66, 44, 67, 48, 52, 55, 50, 54, 52, 44, 67, 70, 58, 57, 49, 46, 49, 67, 84, 32, 70, 86, 58, 118, 50, 50, 46, 48, 49, 10) s = reduce(lambda x, y: x+y, map(chr, a)) print s xxxxTEKTRONIX,TDS 2024B,C047264,CF:91.1CT FV:v22.01
author Daniel O'Connor <darius@dons.net.au>
date Wed, 13 May 2009 14:44:40 +0930
parents
children e2089824735a
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
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 # Spec/info..
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 # 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
7 # 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
8 # 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
9 # linux-2.6.29.3/drivers/usb/class/usbtmc.c
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 import usb
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 # 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
16 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 # Write:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 # Offset Field Size Value Description
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 # 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
20 # 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
21 # 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
22 # 3 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 # 4 TransferSize 4 0x06
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 # 5 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 # 6 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 # 7 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 # 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
28 # 9 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 # 10 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 # 11 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 # 12 Msg itself 1 0x2a '*'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 # 13 1 0x49 'I'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 # 14 1 0x44 'D'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 # 15 1 0x4e 'N'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 # 16 1 0x3f '?'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 # 17 1 0x0a '\n'
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 # 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
38 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 # Send a read request:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 # Offset Field Size Value Description
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 # 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
43 # 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
44 # 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
45 # 3 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 # 4 TransferSize 4 0x64
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 # 5 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 # 6 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 # 7 .. 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 # 8 bmTransferAttr 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 # 9 Term char 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 # 10 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 # 11 Reserved 1 0x00
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 # Tektronix TDS2024B
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 USB_VEND_TEKTRONIX = 1689
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 USB_PROD_TEKTORNIX = 874
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 # No libusb versions of these available
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 USB_CLASS_APP_SPECIFIC = 254
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 USB_SUBCLASS_TMC = 3
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 USB_PROTOCOL_488 = 1
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 # USB488 message IDs
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 DEV_DEP_MSG_OUT = 1
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 REQUEST_DEV_DEP_MSG_IN = 2
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 DEV_DEP_MSG_IN = 2
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 class USB488Device(object):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 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
71 """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
72 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
73
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 busses = usb.busses()
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 # Search for the device we want
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 #
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 found = False
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 for bus in busses:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 for dev in bus.devices:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 # Skip ones that don't match
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 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
84 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 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
86 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 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
88 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 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
90 continue
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 # 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
93 # 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
94 # 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
95 # unless you want to hard code everything.
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 for confidx in xrange(len(dev.configurations)):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 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
98 for altif in iface:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 # 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
100 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
101 altif.interfaceSubClass == USB_SUBCLASS_TMC and \
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 altif.interfaceProtocol == USB_PROTOCOL_488:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 found = True
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 break
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 if not found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 raise "Could not find a suitable USB device"
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 # Open the device and claim the USB interface that supports the spec
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 self.handle = dev.open()
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 self.handle.setConfiguration(dev.configurations[confidx].value)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 self.handle.claimInterface(altif.interfaceNumber)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 self.handle.setAltInterface(altif.alternateSetting)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 # Get some info for humans
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 self.vendname = self.handle.getString(dev.iManufacturer, 1024)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 self.prodname = self.handle.getString(dev.iProduct, 1024)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 self.serial = self.handle.getString(dev.iSerialNumber, 1024)
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 # 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
128 self.intrep = self.bulkinep = self.bulkoutep = None
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 for ep in altif.endpoints:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 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
132 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
133 self.intrep = ep.address
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 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
136 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
137 self.bulkinep = ep.address
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 else:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 self.bulkoutep = ep.address
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 self.maxPacket = ep.maxPacketSize
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 # 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
143 if self.intrep == None:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 print "Can't find interrupt endpoint"
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 # Data from the scope (mandatory)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 if self.bulkinep == None:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 raise "Can't find bulk-in endpoint"
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 # Data to the scope (mandatory)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 if self.bulkoutep == None:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 raise "Can't find bulk-out endpoint"
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 self.tag = 1
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 def __str__(self):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 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
158 if self.serial != "":
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 rtn += " S/N: " + self.serial
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 return rtn
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 def incrtag(self):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 self.tag += 1
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 if self.tag == 0:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 self.tag += 1
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 def write(self, data):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 """Send data (string) to the scope"""
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 orddata = map(ord, data)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 datalen = len(orddata)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173 # Build the packet
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 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
175 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
176 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
177
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 # Add the data
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 pkt = pkt + orddata
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 # Align to 4 bytes
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 alignlen = ((len(pkt) / 4) + 1) * 4
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 pkt = pkt + [0] * (alignlen - len(pkt))
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 # Bump the tag
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 self.incrtag()
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 # 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
189 while len(pkt) > 0:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 chunk = pkt[0:self.maxPacket]
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 pkt = pkt[self.maxPacket:]
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 print "Sending %s bytes of data: %s" % (len(chunk), chunk)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 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
195 if wrote != len(chunk):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196 raise "Short write, got %d, expected %d" % (wrote, len(chunk))
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 def read(self):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 """Read data from the device"""
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 datalen = 1024
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 # Ask the device to send us something
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 pkt = [ REQUEST_DEV_DEP_MSG_IN, self.tag, ~self.tag & 0xff, 0x00,
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 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
206 datalen >> 24 & 0xff, 0, 0, 0, 0]
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208 # Bump tag
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209 self.incrtag()
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 # Send it
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 wrote = self.handle.bulkWrite(self.bulkoutep, pkt)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 if wrote != len(pkt):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 print "Short write, got %d, expected %d" % (wrote, len(pkt))
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 read = self.handle.bulkRead(self.bulkinep, datalen)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 print "Read %s bytes: %s" % (len(read), str(read))
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 return read
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 def find488():
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 """Search for a USB488 device, returns a handle, iface, dev tuple for it"""
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 busses = usb.busses()
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 found = False
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 for bus in busses:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 for dev in bus.devices:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 for confidx in xrange(len(dev.configurations)):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 # XXX: what do multi-interface devices look like?
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230 iface = dev.configurations[confidx].interfaces[0][0]
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 # 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
232 if iface.interfaceClass == USB_CLASS_APP_SPECIFIC and \
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 iface.interfaceSubClass == USB_SUBCLASS_TMC and \
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 iface.interfaceProtocol == USB_PROTOCOL_488:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 handle = dev.open()
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 handle.setConfiguration(1)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 handle.claimInterface(0)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 handle.setAltInterface(0)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239 #handle.setConfiguration(confidx)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 #handle.claimInterface(0)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 found = True
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 break
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 if found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245 break
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 if not found:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 raise "Could not find scope, check perms"
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250 return (handle, iface, dev)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 def geteps(iface):
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 """Returns a tuple of intr,input,output addresses of endpoints for the interface"""
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254 intrep = bulkinep = bulkoutep = None
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
255
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 for ep in iface.endpoints:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257 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
258 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
259 intrep = ep.address
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
261 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
262 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
263 bulkinep = ep.address
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264 else:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265 bulkoutep = ep.address
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 # 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
268 if intrep == None:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269 print "Can't find interrup endpoint"
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 # Data from the scope
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272 if bulkinep == None:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273 raise "Can't find bulk-in endpoint"
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275 # Data to the scope
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276 if bulkoutep == None:
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 raise "Can't find bulk-out endpoint"
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279 return intrep, bulkinep, bulkoutep
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
281 def main():
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
282 handle, iface, dev = find488()
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
283 print "Found device"
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 intrep, bulkinep, bulkoutep = geteps(iface)
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
286 print "Found endpoints"
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
288 if __name__ == "__main__":
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289 main()
a43a47dfc902 First stab at code that actually works!
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
290