comparison usb488.py @ 55:ad5942d22f78

Use BaseException rather than strings. Add ask method. Wrap tag to a byte.
author Daniel O'Connor <doconnor@gsoft.com.au>
date Tue, 08 Dec 2020 13:59:05 +1030
parents 876d951bbcc0
children 91b476ebc0f2
comparison
equal deleted inserted replaced
54:42621291eb9b 55:ad5942d22f78
30 # 30 #
31 # http://www.usb.org/developers/devclass_docs/USBTMC_1_006a.zip 31 # http://www.usb.org/developers/devclass_docs/USBTMC_1_006a.zip
32 # http://svn.openmoko.org/developers/werner/ahrt/host/tmc/README 32 # http://svn.openmoko.org/developers/werner/ahrt/host/tmc/README
33 # http://www.home.agilent.com/agilent/redirector.jspx?action=ref&cname=AGILENT_EDITORIAL&ckey=1189335&lc=eng&cc=US&nfr=-35560.0.00 33 # http://www.home.agilent.com/agilent/redirector.jspx?action=ref&cname=AGILENT_EDITORIAL&ckey=1189335&lc=eng&cc=US&nfr=-35560.0.00
34 # linux-2.6.29.3/drivers/usb/class/usbtmc.c 34 # linux-2.6.29.3/drivers/usb/class/usbtmc.c
35 # http://sdpha2.ucsd.edu/Lab_Equip_Manuals/usbtmc_usb488_subclass_1_00.pdf
35 # 36 #
36 37
37 import usb 38 import usb
38 39
39 # 40 #
130 if found: 131 if found:
131 break 132 break
132 if found: 133 if found:
133 break 134 break
134 if not found: 135 if not found:
135 raise "Could not find a suitable USB device" 136 raise BaseException("Could not find a suitable USB device")
136 137
137 # Open the device and claim the USB interface that supports the spec 138 # Open the device and claim the USB interface that supports the spec
138 handle = dev.open() 139 handle = dev.open()
139 handle.setConfiguration(dev.configurations[confidx].value) 140 handle.setConfiguration(dev.configurations[confidx].value)
140 handle.claimInterface(altif.interfaceNumber) 141 handle.claimInterface(altif.interfaceNumber)
152 153
153 for ep in altif.endpoints: 154 for ep in altif.endpoints:
154 if ep.type == usb.ENDPOINT_TYPE_INTERRUPT and \ 155 if ep.type == usb.ENDPOINT_TYPE_INTERRUPT and \
155 ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN: 156 ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN:
156 self.intrep = ep.address 157 self.intrep = ep.address
157 158
158 if ep.type == usb.ENDPOINT_TYPE_BULK: 159 if ep.type == usb.ENDPOINT_TYPE_BULK:
159 if ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN: 160 if ep.address & usb.ENDPOINT_IN == usb.ENDPOINT_IN:
160 self.bulkinep = ep.address 161 self.bulkinep = ep.address
161 else: 162 else:
162 self.bulkoutep = ep.address 163 self.bulkoutep = ep.address
166 if self.intrep == None: 167 if self.intrep == None:
167 print "Can't find interrupt endpoint" 168 print "Can't find interrupt endpoint"
168 169
169 # Data from the scope (mandatory) 170 # Data from the scope (mandatory)
170 if self.bulkinep == None: 171 if self.bulkinep == None:
171 raise "Can't find bulk-in endpoint" 172 raise BaseException("Can't find bulk-in endpoint")
172 173
173 # Data to the scope (mandatory) 174 # Data to the scope (mandatory)
174 if self.bulkoutep == None: 175 if self.bulkoutep == None:
175 raise "Can't find bulk-out endpoint" 176 raise BaseException("Can't find bulk-out endpoint")
176 177
177 self.tag = 1 178 self.tag = 1
178 179
179 def __str__(self): 180 def __str__(self):
180 rtn = "Mfg: %s Prod: %s" % (self.vendname, self.prodname) 181 rtn = "Mfg: %s Prod: %s" % (self.vendname, self.prodname)
182 rtn += " S/N: " + self.serial 183 rtn += " S/N: " + self.serial
183 184
184 return rtn 185 return rtn
185 186
186 def incrtag(self): 187 def incrtag(self):
187 self.tag += 1 188 self.tag = (self.tag + 1) % 255
188 if self.tag == 0: 189 if self.tag == 0:
189 self.tag += 1 190 self.tag += 1
190 191
191 def write(self, data): 192 def write(self, data):
192 """Send data (string) to the instrument""" 193 """Send data (string) to the instrument"""
218 pkt = pkt[self.maxPacket:] 219 pkt = pkt[self.maxPacket:]
219 220
220 #print "Sending %s bytes of data: %s" % (len(chunk), chunk) 221 #print "Sending %s bytes of data: %s" % (len(chunk), chunk)
221 wrote = self.handle.bulkWrite(self.bulkoutep, chunk) 222 wrote = self.handle.bulkWrite(self.bulkoutep, chunk)
222 if wrote != len(chunk): 223 if wrote != len(chunk):
223 raise "Short write, got %d, expected %d" % (wrote, len(chunk)) 224 raise BaseException("Short write, got %d, expected %d" % (wrote, len(chunk)))
224 225
225 def read(self, timeout = None): 226 def read(self, timeout = None):
226 """Read data from the device, waits for up to timeout seconds for each USB transaction""" 227 """Read data from the device, waits for up to timeout seconds for each USB transaction"""
227 228
228 if timeout == None: 229 if timeout == None:
258 #print "Reading.." 259 #print "Reading.."
259 read = self.handle.bulkRead(self.bulkinep, datalen, _timeout) 260 read = self.handle.bulkRead(self.bulkinep, datalen, _timeout)
260 #print "Read %s bytes: %s" % (len(read), str(read)) 261 #print "Read %s bytes: %s" % (len(read), str(read))
261 262
262 if read[0] != DEV_DEP_MSG_IN: 263 if read[0] != DEV_DEP_MSG_IN:
263 raise "Unexpected Msg ID, got %s expected %d" % (read[0], DEV_DEP_MSG_IN) 264 raise BaseException("Unexpected Msg ID, got %s expected %d" % (read[0], DEV_DEP_MSG_IN))
264 if read[1] != exptag: 265 if read[1] != exptag:
265 raise "Unexpected tag, got %d expected %d" % (read[1], exptag) 266 raise BaseException("Unexpected tag, got %d expected %d" % (read[1], exptag))
266 if read[2] != ~exptag & 0xff: 267 if read[2] != ~exptag & 0xff:
267 raise "Unexpected tag inverse, got %d expected %d" % (read[1], ~exptag & 0xff) 268 raise BaseException("Unexpected tag inverse, got %d expected %d" % (read[1], ~exptag & 0xff))
268 269
269 actualdata = read[4] | read[5] << 8 | read[6] << 16 | read[7] << 24 270 actualdata = read[4] | read[5] << 8 | read[6] << 16 | read[7] << 24
270 #print "Computed datalen is %d" % (actualdata) 271 #print "Computed datalen is %d" % (actualdata)
271 data += read[12:12 + actualdata] 272 data += read[12:12 + actualdata]
272 if read[8] & 0x01: 273 if read[8] & 0x01:
278 # Trim off \n if present 279 # Trim off \n if present
279 if result[-1] == '\n': 280 if result[-1] == '\n':
280 result = result[0:-1] 281 result = result[0:-1]
281 282
282 return result 283 return result
283 284
285 def ask(self, s, timeout = None):
286 self.write(s)
287 return self.read(timeout = None)
288
284 def isConnected(self): 289 def isConnected(self):
285 """Check if the device is present""" 290 """Check if the device is present"""
286 291
287 # libusb makes it very hard (at least on FreeBSD) to determine if we're still connected. 292 # libusb makes it very hard (at least on FreeBSD) to determine if we're still connected.
288 # This is a reasonable proxy.. 293 # This is a reasonable proxy..