comparison usb488.py @ 56:91b476ebc0f2

Run through 2to3
author Daniel O'Connor <doconnor@gsoft.com.au>
date Tue, 08 Dec 2020 14:00:45 +1030
parents ad5942d22f78
children 19045ad9f5f5
comparison
equal deleted inserted replaced
55:ad5942d22f78 56:91b476ebc0f2
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 # http://sdpha2.ucsd.edu/Lab_Equip_Manuals/usbtmc_usb488_subclass_1_00.pdf
36 # 36 #
37 37
38 import usb 38 import usb
39 from functools import reduce
39 40
40 # 41 #
41 # The usual SCPI commands are wrapped before being sent. 42 # The usual SCPI commands are wrapped before being sent.
42 # 43 #
43 # Write: 44 # Write:
113 114
114 # The libusb examples say you can check for device 115 # The libusb examples say you can check for device
115 # class and then open, however in that case you can't 116 # class and then open, however in that case you can't
116 # find the endpoint number which seems pretty useless 117 # find the endpoint number which seems pretty useless
117 # unless you want to hard code everything. 118 # unless you want to hard code everything.
118 for confidx in xrange(len(dev.configurations)): 119 for confidx in range(len(dev.configurations)):
119 for iface in dev.configurations[confidx].interfaces: 120 for iface in dev.configurations[confidx].interfaces:
120 for altif in iface: 121 for altif in iface:
121 # Check if this is a USB488 capable interface 122 # Check if this is a USB488 capable interface
122 if altif.interfaceClass == USB_CLASS_APP_SPECIFIC and \ 123 if altif.interfaceClass == USB_CLASS_APP_SPECIFIC and \
123 altif.interfaceSubClass == USB_SUBCLASS_TMC and \ 124 altif.interfaceSubClass == USB_SUBCLASS_TMC and \
163 self.bulkoutep = ep.address 164 self.bulkoutep = ep.address
164 self.maxPacket = ep.maxPacketSize 165 self.maxPacket = ep.maxPacketSize
165 166
166 # Required for 488.2 devices, optional otherwise 167 # Required for 488.2 devices, optional otherwise
167 if self.intrep == None: 168 if self.intrep == None:
168 print "Can't find interrupt endpoint" 169 print("Can't find interrupt endpoint")
169 170
170 # Data from the scope (mandatory) 171 # Data from the scope (mandatory)
171 if self.bulkinep == None: 172 if self.bulkinep == None:
172 raise BaseException("Can't find bulk-in endpoint") 173 raise BaseException("Can't find bulk-in endpoint")
173 174
190 self.tag += 1 191 self.tag += 1
191 192
192 def write(self, data): 193 def write(self, data):
193 """Send data (string) to the instrument""" 194 """Send data (string) to the instrument"""
194 195
195 orddata = map(ord, data) 196 orddata = list(map(ord, data))
196 # The device needs a \n at the end, enfore this 197 # The device needs a \n at the end, enfore this
197 if orddata[-1] != '\n': 198 if orddata[-1] != '\n':
198 orddata += [ord('\n')] 199 orddata += [ord('\n')]
199 datalen = len(orddata) 200 datalen = len(orddata)
200 201
252 253
253 # Send it 254 # Send it
254 #print "Sending " + str(pkt) 255 #print "Sending " + str(pkt)
255 wrote = self.handle.bulkWrite(self.bulkoutep, pkt, _timeout) 256 wrote = self.handle.bulkWrite(self.bulkoutep, pkt, _timeout)
256 if wrote != len(pkt): 257 if wrote != len(pkt):
257 print "Short write, got %d, expected %d" % (wrote, len(pkt)) 258 print("Short write, got %d, expected %d" % (wrote, len(pkt)))
258 259
259 #print "Reading.." 260 #print "Reading.."
260 read = self.handle.bulkRead(self.bulkinep, datalen, _timeout) 261 read = self.handle.bulkRead(self.bulkinep, datalen, _timeout)
261 #print "Read %s bytes: %s" % (len(read), str(read)) 262 #print "Read %s bytes: %s" % (len(read), str(read))
262 263
273 if read[8] & 0x01: 274 if read[8] & 0x01:
274 #print "Breaking out due to EOM" 275 #print "Breaking out due to EOM"
275 break 276 break
276 277
277 # Stringify result for easier consumption 278 # Stringify result for easier consumption
278 result = reduce(lambda x, y: x+y, map(chr, data)) 279 result = reduce(lambda x, y: x+y, list(map(chr, data)))
279 # Trim off \n if present 280 # Trim off \n if present
280 if result[-1] == '\n': 281 if result[-1] == '\n':
281 result = result[0:-1] 282 result = result[0:-1]
282 283
283 return result 284 return result
291 292
292 # libusb makes it very hard (at least on FreeBSD) to determine if we're still connected. 293 # libusb makes it very hard (at least on FreeBSD) to determine if we're still connected.
293 # This is a reasonable proxy.. 294 # This is a reasonable proxy..
294 try: 295 try:
295 self.handle.getString(self.dev.iManufacturer, 100) 296 self.handle.getString(self.dev.iManufacturer, 100)
296 except USBError, e: 297 except USBError as e:
297 return False 298 return False
298 299
299 return True 300 return True
300 301