A clone of: https://github.com/nutechsoftware/alarmdecoder This is requires as they dropped support for older firmware releases w/o building in backward compatibility code, and they had previously hardcoded pyserial to a python2 only version.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.3 KiB

  1. import ad2usb
  2. import time
  3. import traceback
  4. class NoDeviceError(Exception):
  5. pass
  6. class CommError(Exception):
  7. pass
  8. class TimeoutError(Exception):
  9. pass
  10. class Firmware(object):
  11. STAGE_BOOT = 1
  12. STAGE_LOAD = 2
  13. STAGE_UPLOADING = 3
  14. STAGE_DONE = 4
  15. def __init__(self):
  16. pass
  17. def __del__(self):
  18. pass
  19. @staticmethod
  20. def upload(dev, filename, progress_callback=None):
  21. def do_upload():
  22. with open(filename) as f:
  23. for line in f:
  24. line = line.rstrip()
  25. if line[0] == ':':
  26. dev.write(line + "\r")
  27. res = dev.read_line()
  28. if progress_callback is not None:
  29. progress_callback(Firmware.STAGE_UPLOADING)
  30. time.sleep(0.05)
  31. def read_until(pattern, timeout=0.0):
  32. start_time = time.time()
  33. buf = ''
  34. position = 0
  35. while True:
  36. try:
  37. char = dev.read()
  38. if char is not None and char != '':
  39. if char == pattern[position]:
  40. position = position + 1
  41. if position == len(pattern):
  42. break
  43. else:
  44. position = 0
  45. except Exception, err:
  46. pass
  47. if timeout > 0 and time.time() - start_time > timeout:
  48. raise TimeoutError('Timed out waiting for pattern: {0}'.format(pattern))
  49. if dev is None:
  50. raise NoDeviceError('No device specified for firmware upload.')
  51. dev.close_reader()
  52. time.sleep(5)
  53. try:
  54. dev.write("=\r\n")
  55. if progress_callback is not None:
  56. progress_callback(Firmware.STAGE_BOOT)
  57. read_until('!boot', timeout=10.0)
  58. dev.write("=\r\n")
  59. if progress_callback is not None:
  60. progress_callback(Firmware.STAGE_LOAD)
  61. read_until('!load', timeout=10.0)
  62. do_upload()
  63. if progress_callback is not None:
  64. progress_callback(Firmware.STAGE_DONE)
  65. except TimeoutError, err:
  66. print traceback.print_exc(err)
  67. pass