def _poll_read(self, expected, timeout_s=5.0): """Helper function to continuously poll reads on the FTDI device until an expected number of bytes are returned. Will throw a timeout error if no data is received within the specified number of timeout seconds. Returns the read data as a string if successful, otherwise raises an execption. """ start = time.time() # Start with an empty response buffer. response = bytearray(expected) index = 0 # Loop calling read until the response buffer is full or a timeout occurs. while time.time() - start <= timeout_s: ret, data = ftdi.read_data(self._ctx, expected - index) # Fail if there was an error reading data. if ret < 0: raise RuntimeError( 'ftdi_read_data failed with error code {0}.'.format(ret)) # Add returned data to the buffer. response[index:index + ret] = data[:ret] index += ret # Buffer is full, return the result data. if index >= expected: return str(response) time.sleep(0.01) raise RuntimeError( 'Timeout while polling ftdi_read_data for {0} bytes!'.format( expected))
def _transaction_start(self): """Start I2C transaction.""" # Clear command buffer and expected response bytes. ret, data = ftdi.read_data(self._ft232h._ctx, 100) #IMPORTANT!!!! PG HACK self._command = [] self._expected = 0
def read_continuously(ctx, verbose=True): try: while True: size, buf = ftdi.read_data(ctx, 256) if size == 0: continue hexed = binascii.hexlify(buf[:size]).decode('ascii') if verbose: print(size, hexed) else: print(hexed, end='') except KeyboardInterrupt: print()
def _read(self, length, timeout=5): start = time.time() response = bytearray(length) count = 0 while count < length: if (time.time() - start >= timeout): raise Exception("FTDI Read Timeout") read, data = ftdi.read_data(self._ctx, length - count) if read < 0: raise Exception("USB Error: {}".format(read)) response[count:read] = data[:read] count += read time.sleep(0.01) self._debug( 5, "MPSSE: Read: " + "".join("{:02x}".format(c) for c in response)) return response
def parse_continuously(ctx): buf = [] try: while True: size, buf_add = ftdi.read_data(ctx, 256) if size == 0: continue buf.extend(buf_add[:size]) while True: new_buf = parse_msg(buf) if new_buf != buf: buf = new_buf else: break except KeyboardInterrupt: pass
def read(self): rx = RxPkt() ret = "" remaining = 500 #todo: TIMEOUTS start = time.time() while remaining: readBytes, dat = ftdi1.read_data(self.ftdic, 1) if readBytes: remaining, ret = rx.checkByte(dat) if time.time() > start+3: raise TimeoutError("Timed Out!") break #if ret: # print "ret", ret return ret
def bruteforce_properties(ctx): for bits in [ftdi.BITS_8]: #[ftdi.BITS_7, ftdi.BITS_8]: for sbit in [ftdi.STOP_BIT_1, ftdi.STOP_BIT_15, ftdi.STOP_BIT_2]: for parity in [ftdi.NONE ]: #, ftdi.ODD, ftdi.EVEN, ftdi.MARK, ftdi.SPACE]: for break_ in [ftdi.BREAK_OFF, ftdi.BREAK_ON]: print(bits, sbit, parity, break_) ftdi.set_line_property2(ctx, bits, sbit, parity, break_) cnt = 0 while cnt < 4: size, buf = ftdi.read_data(ctx, 256) if size == 0: continue print(size, binascii.hexlify(buf[:size]).decode('ascii')) cnt += 1 print()
def _ftdi_read(self, num_bytes, max_iter=None): """Read data from the FTDI chip.""" bytes_left = num_bytes bytes_read = [] iter_left = max_iter while bytes_left: if iter_left == 0: break [n, buf] = ftdi.read_data(self.ftdic, bytes_left) if n < 0: raise IOError('USB read error (error code %i: %s)' % (n, USB_ERROR_CODE[n] if n in USB_ERROR_CODE else 'unknown')) bytes_read += map(ord, buf[:n]) bytes_left -= n if iter_left is not None: iter_left -= 1 if self._debug_ftdi and bytes_read: self._debug("[FTDI] read", "[%s]"%(" ".join("%02X" % b for b in bytes_read))) return bytes_read
def _poll_read(self, expected, timeout_s=5.0): """Helper function to continuously poll reads on the FTDI device until an expected number of bytes are returned. Will throw a timeout error if no data is received within the specified number of timeout seconds. Returns the read data as a string if successful, otherwise raises an execption. """ start = time.time() # Start with an empty response buffer. response = bytearray(expected) index = 0 # Loop calling read until the response buffer is full or a timeout occurs. while time.time() - start <= timeout_s: ret, data = ftdi.read_data(self._ctx, expected - index) # Fail if there was an error reading data. if ret < 0: raise RuntimeError('ftdi_read_data failed with error code {0}.'.format(ret)) # Add returned data to the buffer. response[index:index+ret] = data[:ret] index += ret # Buffer is full, return the result data. if index >= expected: return str(response) time.sleep(0.01) raise RuntimeError('Timeout while polling ftdi_read_data for {0} bytes!'.format(expected))
#!/usr/bin/env python # -*- coding: UTF-8 -*- ''' Tinkering that I did on Fedora 23 using this library: http://www.intra2net.com/en/developer/libftdi/download/libftdi1-1.2.tar.bz2 ''' import sys import time import ftdi1 as ftdi # My DS_UM232H: usb_vendor = 0x0403 usb_model = 0x6014 context = ftdi.new() dev = ftdi.usb_open(context, usb_vendor, usb_model) if dev < 0: print 'Failed to open. :-(' while True: aa, bb = ftdi.read_data(context, 1000) if aa != 0: # print bb[:aa], sys.stdout.write(bb[:aa]) else: time.sleep(0.01)
def ftread(): ret, data = ftdi.read_data(ctx, 200) if ret < 0: raise RuntimeError( 'ftdi_read_data failed with error code {0}.'.format(ret)) return data[0:ret]