diff --git a/pyad2usb/devices.py b/pyad2usb/devices.py index a579182..2a657d5 100644 --- a/pyad2usb/devices.py +++ b/pyad2usb/devices.py @@ -266,7 +266,8 @@ class SerialDevice(Device): self._buffer = self._buffer[:-1] if timeout > 0 and time.time() - start_time > timeout: - break + raise util.TimeoutError('Timeout while waiting for line terminator.') + except serial.SerialException, err: raise util.CommError('Error reading from AD2SERIAL device: {0}'.format(str(err))) else: diff --git a/pyad2usb/util.py b/pyad2usb/util.py index cfde69d..658a00d 100644 --- a/pyad2usb/util.py +++ b/pyad2usb/util.py @@ -7,57 +7,69 @@ class NoDeviceError(Exception): class CommError(Exception): pass +class TimeoutError(Exception): + pass + class Firmware(object): - def __init__(self): - pass - - def __del__(self): - pass - - @staticmethod - def upload(dev, filename): - def do_upload(): - with open(filename) as f: - print 'firmwaring this mofo!' - for line in f: - line = line.rstrip() - if line[0] == ':': - print "> {0}".format(line) - dev.write(line + "\r") - crap = dev.read_line() - print "< {0}".format(crap) - - time.sleep(0.05) - - def read_until(data): - buf = '' - position = 0 - - while True: - try: - char = dev.read() - - if char is not None and char != '': - if char == data[position]: - position = position + 1 - if position == len(data): - return True - else: - position = 0 - except Exception, err: - pass - - if dev is None: - raise NoDeviceError('No device specified for firmware upload.') - - dev.close_reader() - - time.sleep(1) - - dev.write("=\r\n") - read_until('!boot') - - dev.write("=\r\n") - read_until('!load') - - do_upload() + def __init__(self): + pass + + def __del__(self): + pass + + @staticmethod + def upload(dev, filename): + def do_upload(): + with open(filename) as f: + print 'firmwaring this mofo!' + for line in f: + line = line.rstrip() + if line[0] == ':': + print "> {0}".format(line) + dev.write(line + "\r") + crap = dev.read_line() + print "< {0}".format(crap) + + time.sleep(0.05) + + def read_until(pattern, timeout=0.0): + start_time = time.time() + buf = '' + position = 0 + + while True: + try: + char = dev.read() + + if char is not None and char != '': + if char == pattern[position]: + position = position + 1 + if position == len(pattern): + break + else: + position = 0 + except Exception, err: + pass + + if timeout > 0 and time.time() - start_time > timeout: + raise TimeoutError('Timed out waiting for pattern: {0}'.format(pattern)) + + if dev is None: + raise NoDeviceError('No device specified for firmware upload.') + + dev.close_reader() + time.sleep(1) + + try: + dev.write("=\r\n") + read_until('!boot', timeout=10.0) + + dev.write("=\r\n") + read_until('!load', timeout=10.0) + + do_upload() + except TimeoutError, err: + print traceback.print_exc(err) + pass + + diff --git a/test.py b/test.py index fe9e5ba..c830467 100755 --- a/test.py +++ b/test.py @@ -70,13 +70,9 @@ try: while running: time.sleep(0.1) - print 'wat' - #a2u.close() dev.close() #overseer.close() except Exception, err: - #print 'Error: {0}'.format(str(err)) - print 'wtf' traceback.print_exc(err)