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.

76 lines
1.9 KiB

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