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.

268 lines
7.4 KiB

  1. import usb.core
  2. import usb.util
  3. import time
  4. import threading
  5. import serial
  6. import serial.tools.list_ports
  7. import traceback
  8. from pyftdi.pyftdi.ftdi import *
  9. from pyftdi.pyftdi.usbtools import *
  10. from . import util
  11. from .event import event
  12. class Device(object):
  13. on_open = event.Event('Called when the device has been opened')
  14. on_close = event.Event('Called when the device has been closed')
  15. on_read = event.Event('Called when a line has been read from the device')
  16. on_write = event.Event('Called when data has been written to the device')
  17. def __init__(self):
  18. pass
  19. def __del__(self):
  20. pass
  21. class ReadThread(threading.Thread):
  22. def __init__(self, device):
  23. threading.Thread.__init__(self)
  24. self._device = device
  25. self._running = False
  26. def stop(self):
  27. self._running = False
  28. def run(self):
  29. self._running = True
  30. while self._running:
  31. try:
  32. self._device.read_line()
  33. except util.CommError, err:
  34. self.stop()
  35. time.sleep(0.01)
  36. class USBDevice(Device):
  37. FTDI_VENDOR_ID = 0x0403
  38. FTDI_PRODUCT_ID = 0x6001
  39. BAUDRATE = 115200
  40. @staticmethod
  41. def find_all():
  42. devices = []
  43. try:
  44. devices = Ftdi.find_all([(USBDevice.FTDI_VENDOR_ID, USBDevice.FTDI_PRODUCT_ID)], nocache=True)
  45. except (usb.core.USBError, FtdiError), err:
  46. raise util.CommError('Error enumerating AD2USB devices: {0}'.format(str(err)))
  47. return devices
  48. def __init__(self, vid=FTDI_VENDOR_ID, pid=FTDI_PRODUCT_ID, serial=None, description=None):
  49. Device.__init__(self)
  50. self._vendor_id = vid
  51. self._product_id = pid
  52. self._serial_number = serial
  53. self._description = description
  54. self._buffer = ''
  55. self._device = Ftdi()
  56. self._running = False
  57. self._read_thread = Device.ReadThread(self)
  58. def open(self, baudrate=BAUDRATE, interface=0, index=0):
  59. self._running = True
  60. if baudrate is None:
  61. baudrate = BAUDRATE
  62. if interface is None:
  63. interface = 0
  64. if index is None:
  65. index = 0
  66. try:
  67. self._device.open(self._vendor_id,
  68. self._product_id,
  69. interface,
  70. index,
  71. self._serial_number,
  72. self._description)
  73. self._device.set_baudrate(baudrate)
  74. except (usb.core.USBError, FtdiError), err:
  75. self.on_close()
  76. raise util.CommError('Error opening AD2USB device: {0}'.format(str(err)))
  77. else:
  78. self._read_thread.start()
  79. self.on_open((self._serial_number, self._description))
  80. def close(self):
  81. try:
  82. self._running = False
  83. self._read_thread.stop()
  84. self._device.close()
  85. except (FtdiError, usb.core.USBError):
  86. pass
  87. self.on_close()
  88. def write(self, data):
  89. self._device.write_data(data)
  90. self.on_write(data)
  91. def read_line(self, timeout=0.0):
  92. start_time = time.time()
  93. got_line = False
  94. ret = None
  95. try:
  96. while self._running:
  97. buf = self._device.read_data(1)
  98. if buf != '':
  99. self._buffer += buf
  100. if buf == "\n":
  101. if len(self._buffer) > 1:
  102. if self._buffer[-2] == "\r":
  103. self._buffer = self._buffer[:-2]
  104. # ignore if we just got \r\n with nothing else in the buffer.
  105. if len(self._buffer) != 0:
  106. got_line = True
  107. break
  108. else:
  109. self._buffer = self._buffer[:-1]
  110. if timeout > 0 and time.time() - start_time > timeout:
  111. break
  112. time.sleep(0.01)
  113. except (usb.core.USBError, FtdiError), err:
  114. self.close()
  115. raise util.CommError('Error reading from AD2USB device: {0}'.format(str(err)))
  116. else:
  117. if got_line:
  118. ret = self._buffer
  119. self._buffer = ''
  120. self.on_read(ret)
  121. return ret
  122. class SerialDevice(Device):
  123. BAUDRATE = 19200
  124. @staticmethod
  125. def find_all():
  126. devices = []
  127. try:
  128. devices = serial.tools.list_ports.comports()
  129. except Exception, err:
  130. raise util.CommError('Error enumerating AD2SERIAL devices: {0}'.format(str(err)))
  131. return devices
  132. def __init__(self):
  133. Device.__init__(self)
  134. self._device = serial.Serial(timeout=0) # Timeout = non-blocking to match pyftdi.
  135. self._read_thread = Device.ReadThread(self)
  136. self._buffer = ''
  137. self._running = False
  138. def __del__(self):
  139. pass
  140. def open(self, baudrate=BAUDRATE, interface=0, index=0):
  141. if baudrate is None:
  142. baudrate = BAUDRATE
  143. if interface is None:
  144. interface = 0
  145. if index is None:
  146. index = 0
  147. self._device.baudrate = baudrate
  148. self._device.port = interface
  149. try:
  150. self._device.open()
  151. self._running = True
  152. except (serial.SerialException, ValueError), err:
  153. self.on_close()
  154. raise util.NoDeviceError('Error opening AD2SERIAL device on port {0}.'.format(interface))
  155. else:
  156. self.on_open((None, "AD2SERIAL")) # TODO: Fixme.
  157. self._read_thread.start()
  158. def close(self):
  159. try:
  160. self._running = False
  161. self._read_thread.stop()
  162. self._device.close()
  163. except Exception, err:
  164. pass
  165. self.on_close()
  166. def write(self, data):
  167. try:
  168. self._device.write(data)
  169. except serial.SerialTimeoutException, err:
  170. pass
  171. else:
  172. self.on_write(data)
  173. def read_line(self, timeout=0.0):
  174. start_time = time.time()
  175. got_line = False
  176. ret = None
  177. try:
  178. while self._running:
  179. buf = self._device.read(1)
  180. if buf != '' and buf != "\xff": # WTF is this \xff and why is it in my buffer?!
  181. self._buffer += buf
  182. if buf == "\n":
  183. if len(self._buffer) > 1:
  184. if self._buffer[-2] == "\r":
  185. self._buffer = self._buffer[:-2]
  186. # ignore if we just got \r\n with nothing else in the buffer.
  187. if len(self._buffer) != 0:
  188. got_line = True
  189. break
  190. else:
  191. self._buffer = self._buffer[:-1]
  192. if timeout > 0 and time.time() - start_time > timeout:
  193. break
  194. time.sleep(0.01)
  195. except serial.SerialException, err:
  196. self.close()
  197. raise util.CommError('Error reading from AD2SERIAL device: {0}'.format(str(err)))
  198. else:
  199. if got_line:
  200. ret = self._buffer
  201. self._buffer = ''
  202. self.on_read(ret)
  203. return ret