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.

116 lines
2.7 KiB

  1. from pyftdi.pyftdi.ftdi import *
  2. from pyftdi.pyftdi.usbtools import *
  3. import time
  4. import usb.core
  5. import usb.util
  6. class AD2USB(object):
  7. @classmethod
  8. def find_all(cls):
  9. cls.__devices = Device.find_all()
  10. return cls.__devices
  11. def __init__(self):
  12. self._device = None
  13. AD2USB.find_all()
  14. def __del__(self):
  15. pass
  16. def open(self, device=None):
  17. if len(cls.__devices) == 0:
  18. raise NoDeviceError
  19. if device is None:
  20. self._device = cls.__devices[0]
  21. else
  22. self._device = device
  23. self._device.open()
  24. def close(self):
  25. self._device.close()
  26. self._device = None
  27. class Device(object):
  28. FTDI_VENDOR_ID = 0x0403
  29. FTDI_PRODUCT_ID = 0x6001
  30. BAUDRATE = 115200
  31. @staticmethod
  32. def find_all():
  33. devices = []
  34. try:
  35. devices = Ftdi.find_all([(FTDI_VENDOR_ID, FTDI_PRODUCT_ID)], nocache=True)
  36. except usb.core.USBError, e:
  37. pass
  38. return devices
  39. def __init__(self, vid=FTDI_VENDOR_ID, pid=FTDI_PRODUCT_ID, serial=None, description=None):
  40. self._vendor_id = vid
  41. self._product_id = pid
  42. self._serial_number = serial
  43. self._description = description
  44. self._buffer = ''
  45. self._device = Ftdi()
  46. def open(self, baudrate=BAUDRATE, interface=0, index=0):
  47. self._device.open(self._vendor_id,
  48. self._product_id,
  49. interface,
  50. index,
  51. self._serial_number,
  52. self._description)
  53. self.device.set_baudrate(baudrate)
  54. def close(self):
  55. try:
  56. self._device.close()
  57. except FtdiError, e:
  58. pass
  59. def write(self, data):
  60. self._device.write_data(data)
  61. def read_line(self, timeout=0.0):
  62. start_time = time.time()
  63. got_line = False
  64. ret = None
  65. try:
  66. while 1:
  67. buf = self._device.read_data(1)
  68. self._buffer += buf
  69. if buf == "\n":
  70. if len(self._buffer) > 1:
  71. if self._buffer[-2] == "\r":
  72. self._buffer = self._buffer[:-2]
  73. # ignore if we just got \r\n with nothing else in the buffer.
  74. if len(self._buffer) != 0:
  75. got_line = True
  76. break
  77. else:
  78. self._buffer = self._buffer[:-1]
  79. if timeout > 0 and time.time() - start_time > timeout:
  80. break
  81. time.sleep(0.01)
  82. except FtdiError, e:
  83. pass
  84. else:
  85. if got_line:
  86. ret = self._buffer
  87. self._buffer = ''
  88. return ret