Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _ctrl_out(self, req, value, index, data=''):
"""Send a vendor message to the device"""
try:
return self.usb_dev.ctrl_transfer(Prolific.CTRL_OUT,
req, value, index, data, self.usb_write_timeout)
except usb.core.USBError, e:
raise ProlificError('UsbError: %s' % str(e))
def write_data(self, data):
"""Write data in chunks to the chip"""
offset = 0
size = len(data)
try:
while offset < size:
write_size = self.writebuffer_chunksize
if offset + write_size > size:
write_size = size - offset
length = self.usb_dev.write(self.in_ep,
data[offset:offset+write_size],
self.interface,
self.usb_write_timeout)
if length <= 0:
raise ProlificError("Usb bulk write error")
offset += length
return offset
except usb.core.USBError, e:
raise ProlificError('UsbError: %s' % str(e))
# did we read exactly the right amount of bytes?
if len(data) == size:
return data
else:
# partial copy, not enough bytes in the local cache to
# fulfill the request
part_size = min(size-len(data),
len(self.readbuffer)-self.readoffset)
if part_size < 0:
raise AssertionError("Internal Error")
data += self.readbuffer[self.readoffset:\
self.readoffset+part_size]
self.readoffset += part_size
return data
except usb.core.USBError, e:
raise ProlificError('UsbError: %s' % str(e))
# never reached
raise ProlificError("Internal error")
def _vendor_out(self, value, index, data=''):
"""Send a vendor message to the device"""
try:
return self.usb_dev.ctrl_transfer(Prolific.VENDOR_OUT,
Prolific.VENDOR_WRITE_REQUEST, value, index, data,
self.usb_write_timeout)
except usb.core.USBError, e:
raise ProlificError('UsbError: %s' % str(e))
def read_data_bytes(self, size, attempt=1):
"""Read data in chunks from the chip."""
# Attempt count is useless with PL2303
# Packet size sanity check
if not self.max_packet_size:
raise ProlificError("max_packet_size is bogus")
packet_size = self.max_packet_size
length = 1 # initial condition to enter the usb_read loop
data = Array('B')
# everything we want is still in the cache?
if size <= len(self.readbuffer)-self.readoffset:
data = self.readbuffer[self.readoffset:self.readoffset+size]
self.readoffset += size
return data
# something still in the cache, but not enough to satisfy 'size'?
if len(self.readbuffer)-self.readoffset != 0:
data = self.readbuffer[self.readoffset:]
# end of readbuffer reached
self.readoffset = len(self.readbuffer)
# read from USB, filling in the local cache as it is empty
try:
while (len(data) < size) and (length > 0):
return data
else:
# partial copy, not enough bytes in the local cache to
# fulfill the request
part_size = min(size-len(data),
len(self.readbuffer)-self.readoffset)
if part_size < 0:
raise AssertionError("Internal Error")
data += self.readbuffer[self.readoffset:\
self.readoffset+part_size]
self.readoffset += part_size
return data
except usb.core.USBError, e:
raise ProlificError('UsbError: %s' % str(e))
# never reached
raise ProlificError("Internal error")
def _set_control_lines(self, value):
"""Control IO lines"""
try:
return self._ctrl_out(Prolific.SET_CONTROL_REQUEST, value, 0)
except usb.core.USBError, e:
raise ProlificError('UsbError: %s' % str(e))
def _ctrl_in(self, req, value, index, length):
"""Request for a vendor message from the device"""
try:
return self.usb_dev.ctrl_transfer(Prolific.CTRL_IN,
req, value, index, length, self.usb_read_timeout)
except usb.core.USBError, e:
raise ProlificError('UsbError: %s' % str(e))