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.

126 lines
3.1 KiB

  1. """
  2. Provides utility classes for the AD2USB devices.
  3. """
  4. import ad2usb
  5. import time
  6. import traceback
  7. class NoDeviceError(Exception):
  8. """
  9. No devices found.
  10. """
  11. pass
  12. class CommError(Exception):
  13. """
  14. There was an error communicating with the device.
  15. """
  16. pass
  17. class TimeoutError(Exception):
  18. """
  19. There was a timeout while trying to communicate with the device.
  20. """
  21. pass
  22. class Firmware(object):
  23. """
  24. Represents firmware for the AD2USB/AD2SERIAL devices.
  25. """
  26. STAGE_START = 0
  27. STAGE_WAITING = 1
  28. STAGE_BOOT = 2
  29. STAGE_LOAD = 3
  30. STAGE_UPLOADING = 4
  31. STAGE_DONE = 5
  32. def __init__(self):
  33. """
  34. Constructor
  35. """
  36. pass
  37. def __del__(self):
  38. """
  39. Destructor
  40. """
  41. pass
  42. @staticmethod
  43. def upload(dev, filename, progress_callback=None):
  44. """
  45. Uploads firmware to an AD2USB/AD2SERIAL device.
  46. """
  47. def do_upload():
  48. with open(filename) as f:
  49. for line in f:
  50. line = line.rstrip()
  51. if line[0] == ':':
  52. dev.write(line + "\r")
  53. res = dev.read_line()
  54. if progress_callback is not None:
  55. progress_callback(Firmware.STAGE_UPLOADING)
  56. time.sleep(0.05)
  57. def read_until(pattern, timeout=0.0):
  58. start_time = time.time()
  59. buf = ''
  60. position = 0
  61. while True:
  62. try:
  63. char = dev.read()
  64. if char is not None and char != '':
  65. if char == pattern[position]:
  66. position = position + 1
  67. if position == len(pattern):
  68. break
  69. else:
  70. position = 0
  71. except Exception, err:
  72. traceback.print_exc(err)
  73. if timeout > 0 and time.time() - start_time > timeout:
  74. raise TimeoutError('Timed out waiting for pattern: {0}'.format(pattern))
  75. if dev is None:
  76. raise NoDeviceError('No device specified for firmware upload.')
  77. if progress_callback is not None:
  78. progress_callback(Firmware.STAGE_START)
  79. dev.close_reader()
  80. while dev._read_thread.is_alive():
  81. if progress_callback is not None:
  82. progress_callback(Firmware.STAGE_WAITING)
  83. time.sleep(1)
  84. try:
  85. if progress_callback is not None:
  86. progress_callback(Firmware.STAGE_BOOT)
  87. dev.write("=")
  88. read_until('!boot', timeout=10.0)
  89. if progress_callback is not None:
  90. progress_callback(Firmware.STAGE_LOAD)
  91. dev.write("=")
  92. read_until('!load', timeout=10.0)
  93. do_upload()
  94. if progress_callback is not None:
  95. progress_callback(Firmware.STAGE_DONE)
  96. except TimeoutError, err:
  97. print traceback.print_exc(err)
  98. pass