Browse Source

Reworked timeouts for firmware upload.

pyserial_fix
Scott Petersen 11 years ago
parent
commit
74c4c42640
2 changed files with 23 additions and 10 deletions
  1. +20
    -7
      pyad2usb/util.py
  2. +3
    -3
      test.py

+ 20
- 7
pyad2usb/util.py View File

@@ -5,6 +5,7 @@ Provides utility classes for the AD2USB devices.
import ad2usb import ad2usb
import time import time
import traceback import traceback
import threading


class NoDeviceError(Exception): class NoDeviceError(Exception):
""" """
@@ -71,7 +72,7 @@ class Firmware(object):


if line[0] == ':': if line[0] == ':':
dev.write(line + "\r") dev.write(line + "\r")
res = dev.read_line()
res = dev.read_line(timeout=10.0)


if progress_callback is not None: if progress_callback is not None:
progress_callback(Firmware.STAGE_UPLOADING) progress_callback(Firmware.STAGE_UPLOADING)
@@ -82,11 +83,20 @@ class Firmware(object):
""" """
Read characters until a specific pattern is found or the timeout is hit. Read characters until a specific pattern is found or the timeout is hit.
""" """
start_time = time.time()
def timeout_event():
timeout_event.reading = False

timeout_event.reading = True

timer = None
if timeout > 0:
timer = threading.Timer(timeout, timeout_event)
timer.start()

buf = '' buf = ''
position = 0 position = 0


while True:
while timeout_event.reading:
try: try:
char = dev.read() char = dev.read()


@@ -101,8 +111,11 @@ class Firmware(object):
except Exception, err: except Exception, err:
pass pass


if timeout > 0 and time.time() - start_time > timeout:
raise TimeoutError('Timed out waiting for pattern: {0}'.format(pattern))
if timer:
if timer.is_alive():
timer.cancel()
else:
raise TimeoutError('Timeout while waiting for line terminator.')


def stage_callback(stage): def stage_callback(stage):
if progress_callback is not None: if progress_callback is not None:
@@ -124,13 +137,13 @@ class Firmware(object):
# Reboot the device and wait for the boot loader. # Reboot the device and wait for the boot loader.
stage_callback(Firmware.STAGE_BOOT) stage_callback(Firmware.STAGE_BOOT)
dev.write("=") dev.write("=")
read_until('!boot', timeout=10.0)
read_until('!boot', timeout=15.0)


# Get ourselves into the boot loader and wait for indication # Get ourselves into the boot loader and wait for indication
# that it's ready for the firmware upload. # that it's ready for the firmware upload.
stage_callback(Firmware.STAGE_LOAD) stage_callback(Firmware.STAGE_LOAD)
dev.write("=") dev.write("=")
read_until('!load', timeout=10.0)
read_until('!load', timeout=15.0)


# And finally do the upload. # And finally do the upload.
do_upload() do_upload()


+ 3
- 3
test.py View File

@@ -79,7 +79,7 @@ def upload_usb():
dev.close() dev.close()


def upload_serial(): def upload_serial():
dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0')
dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB2')


dev.open() dev.open()
pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware) pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
@@ -281,7 +281,7 @@ try:
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)


#test_serial() #test_serial()
#upload_serial()
upload_serial()


#test_usb() #test_usb()
#test_usb_serial() #test_usb_serial()
@@ -293,7 +293,7 @@ try:
#test_socket() #test_socket()
#upload_socket() #upload_socket()


test_no_read_thread()
#test_no_read_thread()
#test_serial_grep() #test_serial_grep()


#test_double_panel_write() #test_double_panel_write()


Loading…
Cancel
Save