def _open(self): try: dev = Serial(self.device, self.baud) except SerialException as e: raise DriverError(str(e)) if not dev.isOpen(): raise DriverError("Could not open device") self._serial = dev dev.timeout = 0.01
def _open(self): # Most of this is straight from the PyUSB example documentation dev = usb.core.find(idVendor=0x0fcf, idProduct=0x1008) if dev is None: raise DriverError('Could not open device (not found)') # dev.set_configuration() cfg = dev.get_active_configuration() interface_number = cfg[(0, 0)].bInterfaceNumber alternate_setting = usb.control.get_interface(dev, interface_number) intf = usb.util.find_descriptor(cfg, bInterfaceNumber=interface_number, bAlternateSetting=alternate_setting) # usb.util.claim_interface(dev, interface_number) ep_out = usb.util.find_descriptor( intf, custom_match = \ lambda e: \ usb.util.endpoint_direction(e.bEndpointAddress) == \ usb.util.ENDPOINT_OUT ) assert ep_out is not None ep_in = usb.util.find_descriptor( intf, custom_match = \ lambda e: \ usb.util.endpoint_direction(e.bEndpointAddress) == \ usb.util.ENDPOINT_IN ) assert ep_in is not None self._ep_out = ep_out self._ep_in = ep_in self._dev = dev self._int = interface_number
def read(self, count): if count <= 0: raise DriverError("Could not read from device (zero request).") if not self.opened: raise DriverError("Could not read from device (not open).") # TODO handle USBError exception here, probably rethrow as DriverError # timeouts might be handled as raising a DriverTimeoutError data = self._read(count) with self._lock: if self.log: self.log.logRead(data) if self.debug: self._dump(data, 'READ') return data
def _write(self, data): try: count = self._serial.write(data) self._serial.flush() except SerialTimeoutException as e: raise DriverError(str(e)) return count
def open(self): with self._lock: if self._opened: raise DriverError("Could not open device (already open).") self._open() if self.log: self.log.logOpen()
def close(self): with self._lock: if not self._opened: raise DriverError("Could not close device (not open).") self._close() if self.log: self.log.logClose()
def write(self, data): self._lock.acquire() try: if not self.is_open: raise DriverError("Could not write to device (not open).") if len(data) <= 0: raise DriverError("Could not write to device (no data).") if self.debug: self._dump(data, 'WRITE') ret = self._write(data) if self.log: self.log.logWrite(data[0:ret]) finally: self._lock.release() return ret
def read(self, count): self._lock.acquire() try: if not self.is_open: raise DriverError("Could not read from device (not open).") if count <= 0: raise DriverError("Could not read from device (zero request).") data = self._read(count) if self.log: self.log.logRead(data) if self.debug: self._dump(data, 'READ') finally: self._lock.release() return data
def open(self): self._lock.acquire() try: if self.is_open: raise DriverError("Could not open device (already open).") self._open() self.is_open = True if self.log: self.log.logOpen() finally: self._lock.release()
def write(self, msg): if not self.opened: raise DriverError("Could not write to device (not open).") data = msg.encode() ret = self._write(data) with self._lock: if self.debug: self._dump(data, 'WRITE') if self.log: self.log.logWrite(data[0:ret]) return ret
def close(self): self._lock.acquire() try: if not self.is_open: raise DriverError("Could not close device (not open).") self._close() self.is_open = False if self.log: self.log.logClose() finally: self._lock.release()
def write(self, data): self._lock.acquire() try: if not self.is_open: raise DriverError("Could not write to device (not open).") if len(data) <= 0: raise DriverError("Could not write to device (no data).") # Should attempt to write to USB first ret = self._write(data) data_written = data[0:ret] if self.log: self.log.logWrite(data_written) # Before logging, to show what we actually wrote if self.debug: self._dump(data_written, 'WROTE') finally: self._lock.release() return ret
class USB1Driver(Driver): def __init__(self, device, baud_rate=115200, log=None, debug=False): Driver.__init__(self, device, log, debug) self.baud = baud_rate def _open(self): try: dev = serial.Serial(self.device, self.baud) except serial.SerialException, e: raise DriverError(str(e)) if not dev.isOpen(): raise DriverError('Could not open device') self._serial = dev self._serial.timeout = 0.01
def _open(self): # Most of this is straight from the PyUSB example documentation dev = findDeviceUSB( idVendor=self.idVendor, idProduct=self.idProduct, custom_match=lambda d: (d.bus == self.bus or self.bus is None) and (d.address == self.address or self.address is None)) if dev is None: raise DriverError("Could not open device (not found)") # make sure the kernel driver is not active try: if dev.is_kernel_driver_active(0): try: dev.detach_kernel_driver(0) except USBError as e: exit("could not detach kernel driver: {}".format(e)) except NotImplementedError: pass # for non unix systems dev.set_configuration() cfg = dev.get_active_configuration() interfaceNumber = cfg[(0, 0)].bInterfaceNumber intf = find_descriptor(cfg, bInterfaceNumber=interfaceNumber, bAlternateSetting=get_interface( dev, interfaceNumber)) claim_interface(dev, interfaceNumber) endpoint_out_matcher = \ lambda e: endpoint_direction(e.bEndpointAddress) == ENDPOINT_OUT epOut = find_descriptor(intf, custom_match=endpoint_out_matcher) assert epOut is not None endpoint_in_matcher = \ lambda e: endpoint_direction(e.bEndpointAddress) == ENDPOINT_IN ep_in = find_descriptor(intf, custom_match=endpoint_in_matcher) assert ep_in is not None self._epOut = epOut self._epIn = ep_in self._dev = dev self._intNum = interfaceNumber
def _open(self): # Most of this is straight from the PyUSB example documentation dev = usb.core.find(idVendor=0x0fcf, idProduct=0x1008) if dev is None: raise DriverError('Could not open device (not found)') # make sure the kernel driver is not active if dev.is_kernel_driver_active(0): try: dev.detach_kernel_driver(0) except usb.core.USBError as e: sys.exit("could not detach kernel driver: {}".format(e)) dev.set_configuration() cfg = dev.get_active_configuration() interface_number = cfg[(0, 0)].bInterfaceNumber alternate_setting = usb.control.get_interface(dev, interface_number) intf = usb.util.find_descriptor(cfg, bInterfaceNumber=interface_number, bAlternateSetting=alternate_setting) usb.util.claim_interface(dev, interface_number) ep_out = usb.util.find_descriptor( intf, custom_match = \ lambda e: \ usb.util.endpoint_direction(e.bEndpointAddress) == \ usb.util.ENDPOINT_OUT ) assert ep_out is not None ep_in = usb.util.find_descriptor( intf, custom_match = \ lambda e: \ usb.util.endpoint_direction(e.bEndpointAddress) == \ usb.util.ENDPOINT_IN ) assert ep_in is not None self._ep_out = ep_out self._ep_in = ep_in self._dev = dev self._int = interface_number
def _open(self): raise DriverError("Not Implemented")
def _close(self): raise DriverError("Not Implemented")
def _read(self, count): raise DriverError("Not Implemented")
def _write(self, data): raise DriverError("Not Implemented")
def _open(self): try: dev = serial.Serial(self.device, self.baud) except serial.SerialException, e: raise DriverError(str(e))