Browse Source

Little better error handling for firmware upload. Tabs suck.

pyserial_fix
Scott Petersen 11 years ago
parent
commit
ca5831a336
3 changed files with 67 additions and 58 deletions
  1. +2
    -1
      pyad2usb/devices.py
  2. +65
    -53
      pyad2usb/util.py
  3. +0
    -4
      test.py

+ 2
- 1
pyad2usb/devices.py View File

@@ -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:


+ 65
- 53
pyad2usb/util.py View File

@@ -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



+ 0
- 4
test.py View File

@@ -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)

Loading…
Cancel
Save