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.

83 lines
2.7 KiB

  1. #!/usr/bin/env python
  2. import os
  3. import sys, time
  4. import traceback
  5. import alarmdecoder
  6. def handle_firmware(stage, **kwargs):
  7. if stage == alarmdecoder.util.Firmware.STAGE_START:
  8. handle_firmware.wait_tick = 0
  9. handle_firmware.upload_tick = 0
  10. elif stage == alarmdecoder.util.Firmware.STAGE_WAITING:
  11. if handle_firmware.wait_tick == 0:
  12. sys.stdout.write('Waiting for device.')
  13. handle_firmware.wait_tick += 1
  14. sys.stdout.write('.')
  15. sys.stdout.flush()
  16. elif stage == alarmdecoder.util.Firmware.STAGE_BOOT:
  17. if handle_firmware.wait_tick > 0: print("")
  18. print("Rebooting device..")
  19. elif stage == alarmdecoder.util.Firmware.STAGE_LOAD:
  20. print('Waiting for boot loader..')
  21. elif stage == alarmdecoder.util.Firmware.STAGE_UPLOADING:
  22. if handle_firmware.upload_tick == 0:
  23. sys.stdout.write('Uploading firmware.')
  24. handle_firmware.upload_tick += 1
  25. if handle_firmware.upload_tick % 30 == 0:
  26. sys.stdout.write('.')
  27. sys.stdout.flush()
  28. elif stage == alarmdecoder.util.Firmware.STAGE_DONE:
  29. print("\r\nDone!")
  30. elif stage == alarmdecoder.util.Firmware.STAGE_ERROR:
  31. print("\r\nError: {0}".format(kwargs.get("error", "")))
  32. elif stage == alarmdecoder.util.Firmware.STAGE_DEBUG:
  33. print("\r\nDBG: {0}".format(kwargs.get("data", "")))
  34. def main():
  35. device = '/dev/ttyUSB0'
  36. firmware = None
  37. baudrate = 115200
  38. if len(sys.argv) < 3:
  39. print("Syntax: {0} <firmware> [device path or hostname:port] [baudrate=115200]".format(sys.argv[0]))
  40. sys.exit(1)
  41. firmware = sys.argv[1]
  42. device = sys.argv[2]
  43. if len(sys.argv) > 3:
  44. baudrate = sys.argv[3]
  45. debug = os.environ.get("ALARMDECODER_DEBUG") is not None
  46. print("Flashing device: {0} - {2} baud\r\nFirmware: {1}".format(device, firmware, baudrate))
  47. dev = None
  48. try:
  49. if ':' in device:
  50. hostname, port = device.split(':')
  51. dev = alarmdecoder.devices.SocketDevice(interface=(hostname, int(port)))
  52. else:
  53. dev = alarmdecoder.devices.SerialDevice(interface=device)
  54. dev.open(baudrate=baudrate, no_reader_thread=True)
  55. time.sleep(3)
  56. alarmdecoder.util.Firmware.upload(dev, firmware, handle_firmware, debug=debug)
  57. except alarmdecoder.util.NoDeviceError as ex:
  58. print("Error: Could not find device: {0}".format(ex))
  59. except alarmdecoder.util.UploadError as ex:
  60. print("Error: Error uploading firmware: {0}".format(ex))
  61. except Exception as ex:
  62. print("Error: {0}: {1}".format(ex, traceback.format_exc()))
  63. finally:
  64. if dev is not None:
  65. dev.close()
  66. if __name__ == "__main__":
  67. main()