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.

652 lines
71 KiB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>pyad2usb.devices &mdash; pyad2usb documentation</title>
  7. <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
  8. <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../../',
  12. VERSION: '',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="../../_static/jquery.js"></script>
  19. <script type="text/javascript" src="../../_static/underscore.js"></script>
  20. <script type="text/javascript" src="../../_static/doctools.js"></script>
  21. <link rel="top" title="pyad2usb documentation" href="../../index.html" />
  22. <link rel="up" title="Module code" href="../index.html" />
  23. </head>
  24. <body>
  25. <div class="related">
  26. <h3>Navigation</h3>
  27. <ul>
  28. <li class="right" style="margin-right: 10px">
  29. <a href="../../genindex.html" title="General Index"
  30. accesskey="I">index</a></li>
  31. <li class="right" >
  32. <a href="../../py-modindex.html" title="Python Module Index"
  33. >modules</a> |</li>
  34. <li><a href="../../index.html">pyad2usb documentation</a> &raquo;</li>
  35. <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li>
  36. </ul>
  37. </div>
  38. <div class="document">
  39. <div class="documentwrapper">
  40. <div class="bodywrapper">
  41. <div class="body">
  42. <h1>Source code for pyad2usb.devices</h1><div class="highlight"><pre>
  43. <span class="sd">&quot;&quot;&quot;</span>
  44. <span class="sd">Contains different types of devices belonging to the AD2USB family.</span>
  45. <span class="sd">&quot;&quot;&quot;</span>
  46. <span class="kn">import</span> <span class="nn">usb.core</span>
  47. <span class="kn">import</span> <span class="nn">usb.util</span>
  48. <span class="kn">import</span> <span class="nn">time</span>
  49. <span class="kn">import</span> <span class="nn">threading</span>
  50. <span class="kn">import</span> <span class="nn">serial</span>
  51. <span class="kn">import</span> <span class="nn">serial.tools.list_ports</span>
  52. <span class="kn">import</span> <span class="nn">socket</span>
  53. <span class="kn">from</span> <span class="nn">pyftdi.pyftdi.ftdi</span> <span class="kn">import</span> <span class="o">*</span>
  54. <span class="kn">from</span> <span class="nn">pyftdi.pyftdi.usbtools</span> <span class="kn">import</span> <span class="o">*</span>
  55. <span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">util</span>
  56. <span class="kn">from</span> <span class="nn">.event</span> <span class="kn">import</span> <span class="n">event</span>
  57. <div class="viewcode-block" id="Device"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.Device">[docs]</a><span class="k">class</span> <span class="nc">Device</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  58. <span class="sd">&quot;&quot;&quot;</span>
  59. <span class="sd"> Generic parent device to all AD2USB products.</span>
  60. <span class="sd"> &quot;&quot;&quot;</span>
  61. <span class="c"># Generic device events</span>
  62. <span class="n">on_open</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the device has been opened&#39;</span><span class="p">)</span>
  63. <span class="n">on_close</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when the device has been closed&#39;</span><span class="p">)</span>
  64. <span class="n">on_read</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when a line has been read from the device&#39;</span><span class="p">)</span>
  65. <span class="n">on_write</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">(</span><span class="s">&#39;Called when data has been written to the device&#39;</span><span class="p">)</span>
  66. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  67. <span class="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
  68. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
  69. <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="bp">None</span>
  70. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span> <span class="o">=</span> <span class="bp">None</span>
  71. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
  72. <span class="bp">self</span><span class="o">.</span><span class="n">_read_thread</span> <span class="o">=</span> <span class="n">Device</span><span class="o">.</span><span class="n">ReadThread</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="c"># NOTE: not sure this is going to work..</span>
  73. <span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  74. <span class="k">pass</span>
  75. <span class="nd">@property</span>
  76. <span class="k">def</span> <span class="nf">id</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  77. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_id</span>
  78. <span class="nd">@id.setter</span>
  79. <div class="viewcode-block" id="Device.id"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.Device.id">[docs]</a> <span class="k">def</span> <span class="nf">id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  80. <span class="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="n">value</span>
  81. </div>
  82. <div class="viewcode-block" id="Device.is_reader_alive"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.Device.is_reader_alive">[docs]</a> <span class="k">def</span> <span class="nf">is_reader_alive</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  83. <span class="sd">&quot;&quot;&quot;</span>
  84. <span class="sd"> Indicates whether or not the reader thread is alive.</span>
  85. <span class="sd"> &quot;&quot;&quot;</span>
  86. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_read_thread</span><span class="o">.</span><span class="n">is_alive</span><span class="p">()</span>
  87. </div>
  88. <div class="viewcode-block" id="Device.stop_reader"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.Device.stop_reader">[docs]</a> <span class="k">def</span> <span class="nf">stop_reader</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  89. <span class="sd">&quot;&quot;&quot;</span>
  90. <span class="sd"> Stops the reader thread.</span>
  91. <span class="sd"> &quot;&quot;&quot;</span>
  92. <span class="bp">self</span><span class="o">.</span><span class="n">_read_thread</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  93. </div>
  94. <div class="viewcode-block" id="Device.ReadThread"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.Device.ReadThread">[docs]</a> <span class="k">class</span> <span class="nc">ReadThread</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
  95. <span class="sd">&quot;&quot;&quot;</span>
  96. <span class="sd"> Reader thread which processes messages from the device.</span>
  97. <span class="sd"> &quot;&quot;&quot;</span>
  98. <span class="n">READ_TIMEOUT</span> <span class="o">=</span> <span class="mi">10</span>
  99. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">device</span><span class="p">):</span>
  100. <span class="sd">&quot;&quot;&quot;</span>
  101. <span class="sd"> Constructor</span>
  102. <span class="sd"> &quot;&quot;&quot;</span>
  103. <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
  104. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span> <span class="o">=</span> <span class="n">device</span>
  105. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
  106. <div class="viewcode-block" id="Device.ReadThread.stop"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.Device.ReadThread.stop">[docs]</a> <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  107. <span class="sd">&quot;&quot;&quot;</span>
  108. <span class="sd"> Stops the running thread.</span>
  109. <span class="sd"> &quot;&quot;&quot;</span>
  110. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
  111. </div>
  112. <div class="viewcode-block" id="Device.ReadThread.run"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.Device.ReadThread.run">[docs]</a> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  113. <span class="sd">&quot;&quot;&quot;</span>
  114. <span class="sd"> The actual read process.</span>
  115. <span class="sd"> &quot;&quot;&quot;</span>
  116. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">True</span>
  117. <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_running</span><span class="p">:</span>
  118. <span class="k">try</span><span class="p">:</span>
  119. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">read_line</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">READ_TIMEOUT</span><span class="p">)</span>
  120. <span class="k">except</span> <span class="n">util</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  121. <span class="k">pass</span>
  122. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.01</span><span class="p">)</span>
  123. </div></div></div>
  124. <div class="viewcode-block" id="USBDevice"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.USBDevice">[docs]</a><span class="k">class</span> <span class="nc">USBDevice</span><span class="p">(</span><span class="n">Device</span><span class="p">):</span>
  125. <span class="sd">&quot;&quot;&quot;</span>
  126. <span class="sd"> AD2USB device exposed with PyFTDI&#39;s interface.</span>
  127. <span class="sd"> &quot;&quot;&quot;</span>
  128. <span class="c"># Constants</span>
  129. <span class="n">FTDI_VENDOR_ID</span> <span class="o">=</span> <span class="mh">0x0403</span>
  130. <span class="n">FTDI_PRODUCT_ID</span> <span class="o">=</span> <span class="mh">0x6001</span>
  131. <span class="n">BAUDRATE</span> <span class="o">=</span> <span class="mi">115200</span>
  132. <span class="nd">@staticmethod</span>
  133. <div class="viewcode-block" id="USBDevice.find_all"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.USBDevice.find_all">[docs]</a> <span class="k">def</span> <span class="nf">find_all</span><span class="p">():</span>
  134. <span class="sd">&quot;&quot;&quot;</span>
  135. <span class="sd"> Returns all FTDI devices matching our vendor and product IDs.</span>
  136. <span class="sd"> &quot;&quot;&quot;</span>
  137. <span class="n">devices</span> <span class="o">=</span> <span class="p">[]</span>
  138. <span class="k">try</span><span class="p">:</span>
  139. <span class="n">devices</span> <span class="o">=</span> <span class="n">Ftdi</span><span class="o">.</span><span class="n">find_all</span><span class="p">([(</span><span class="n">USBDevice</span><span class="o">.</span><span class="n">FTDI_VENDOR_ID</span><span class="p">,</span> <span class="n">USBDevice</span><span class="o">.</span><span class="n">FTDI_PRODUCT_ID</span><span class="p">)],</span> <span class="n">nocache</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  140. <span class="k">except</span> <span class="p">(</span><span class="n">usb</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">USBError</span><span class="p">,</span> <span class="n">FtdiError</span><span class="p">),</span> <span class="n">err</span><span class="p">:</span>
  141. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">(</span><span class="s">&#39;Error enumerating AD2USB devices: {0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">err</span><span class="p">)))</span>
  142. <span class="k">return</span> <span class="n">devices</span>
  143. </div>
  144. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">vid</span><span class="o">=</span><span class="n">FTDI_VENDOR_ID</span><span class="p">,</span> <span class="n">pid</span><span class="o">=</span><span class="n">FTDI_PRODUCT_ID</span><span class="p">,</span> <span class="n">serial</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">interface</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
  145. <span class="sd">&quot;&quot;&quot;</span>
  146. <span class="sd"> Constructor</span>
  147. <span class="sd"> &quot;&quot;&quot;</span>
  148. <span class="n">Device</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
  149. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span> <span class="o">=</span> <span class="n">Ftdi</span><span class="p">()</span>
  150. <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
  151. <span class="bp">self</span><span class="o">.</span><span class="n">_vendor_id</span> <span class="o">=</span> <span class="n">vid</span>
  152. <span class="bp">self</span><span class="o">.</span><span class="n">_product_id</span> <span class="o">=</span> <span class="n">pid</span>
  153. <span class="bp">self</span><span class="o">.</span><span class="n">_serial_number</span> <span class="o">=</span> <span class="n">serial</span>
  154. <span class="bp">self</span><span class="o">.</span><span class="n">_description</span> <span class="o">=</span> <span class="n">description</span>
  155. <div class="viewcode-block" id="USBDevice.open"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.USBDevice.open">[docs]</a> <span class="k">def</span> <span class="nf">open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">baudrate</span><span class="o">=</span><span class="n">BAUDRATE</span><span class="p">,</span> <span class="n">interface</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">no_reader_thread</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
  156. <span class="sd">&quot;&quot;&quot;</span>
  157. <span class="sd"> Opens the device.</span>
  158. <span class="sd"> &quot;&quot;&quot;</span>
  159. <span class="c"># Set up defaults</span>
  160. <span class="k">if</span> <span class="n">baudrate</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
  161. <span class="n">baudrate</span> <span class="o">=</span> <span class="n">USBDevice</span><span class="o">.</span><span class="n">BAUDRATE</span>
  162. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">interface</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
  163. <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="mi">0</span>
  164. <span class="k">if</span> <span class="n">interface</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  165. <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
  166. <span class="k">if</span> <span class="n">index</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
  167. <span class="n">index</span> <span class="o">=</span> <span class="mi">0</span>
  168. <span class="c"># Open the device and start up the thread.</span>
  169. <span class="k">try</span><span class="p">:</span>
  170. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_vendor_id</span><span class="p">,</span>
  171. <span class="bp">self</span><span class="o">.</span><span class="n">_product_id</span><span class="p">,</span>
  172. <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span><span class="p">,</span>
  173. <span class="n">index</span><span class="p">,</span>
  174. <span class="bp">self</span><span class="o">.</span><span class="n">_serial_number</span><span class="p">,</span>
  175. <span class="bp">self</span><span class="o">.</span><span class="n">_description</span><span class="p">)</span>
  176. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">set_baudrate</span><span class="p">(</span><span class="n">baudrate</span><span class="p">)</span>
  177. <span class="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="s">&#39;USB {0}:{1}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">usb_dev</span><span class="o">.</span><span class="n">bus</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">usb_dev</span><span class="o">.</span><span class="n">address</span><span class="p">)</span>
  178. <span class="k">except</span> <span class="p">(</span><span class="n">usb</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">USBError</span><span class="p">,</span> <span class="n">FtdiError</span><span class="p">),</span> <span class="n">err</span><span class="p">:</span>
  179. <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
  180. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">NoDeviceError</span><span class="p">(</span><span class="s">&#39;Error opening AD2USB device: {0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">err</span><span class="p">)))</span>
  181. <span class="k">else</span><span class="p">:</span>
  182. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">True</span>
  183. <span class="k">if</span> <span class="ow">not</span> <span class="n">no_reader_thread</span><span class="p">:</span>
  184. <span class="bp">self</span><span class="o">.</span><span class="n">_read_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  185. <span class="bp">self</span><span class="o">.</span><span class="n">on_open</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">_serial_number</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_description</span><span class="p">))</span>
  186. </div>
  187. <div class="viewcode-block" id="USBDevice.close"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.USBDevice.close">[docs]</a> <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  188. <span class="sd">&quot;&quot;&quot;</span>
  189. <span class="sd"> Closes the device.</span>
  190. <span class="sd"> &quot;&quot;&quot;</span>
  191. <span class="k">try</span><span class="p">:</span>
  192. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
  193. <span class="bp">self</span><span class="o">.</span><span class="n">_read_thread</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  194. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  195. <span class="c"># HACK: Probably should fork pyftdi and make this call in .close().</span>
  196. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">usb_dev</span><span class="o">.</span><span class="n">attach_kernel_driver</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_interface</span><span class="p">)</span>
  197. <span class="k">except</span> <span class="p">(</span><span class="n">FtdiError</span><span class="p">,</span> <span class="n">usb</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">USBError</span><span class="p">):</span>
  198. <span class="k">pass</span>
  199. <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
  200. </div>
  201. <div class="viewcode-block" id="USBDevice.write"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.USBDevice.write">[docs]</a> <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  202. <span class="sd">&quot;&quot;&quot;</span>
  203. <span class="sd"> Writes data to the device.</span>
  204. <span class="sd"> &quot;&quot;&quot;</span>
  205. <span class="k">try</span><span class="p">:</span>
  206. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">write_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  207. <span class="bp">self</span><span class="o">.</span><span class="n">on_write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  208. <span class="k">except</span> <span class="n">FtdiError</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  209. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">(</span><span class="s">&#39;Error writing to AD2USB device.&#39;</span><span class="p">)</span>
  210. </div>
  211. <div class="viewcode-block" id="USBDevice.read"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.USBDevice.read">[docs]</a> <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  212. <span class="sd">&quot;&quot;&quot;</span>
  213. <span class="sd"> Reads a single character from the device.</span>
  214. <span class="sd"> &quot;&quot;&quot;</span>
  215. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">read_data</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  216. </div>
  217. <div class="viewcode-block" id="USBDevice.read_line"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.USBDevice.read_line">[docs]</a> <span class="k">def</span> <span class="nf">read_line</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
  218. <span class="sd">&quot;&quot;&quot;</span>
  219. <span class="sd"> Reads a line from the device.</span>
  220. <span class="sd"> &quot;&quot;&quot;</span>
  221. <span class="k">def</span> <span class="nf">timeout_event</span><span class="p">():</span>
  222. <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">False</span>
  223. <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">True</span>
  224. <span class="n">got_line</span> <span class="o">=</span> <span class="bp">False</span>
  225. <span class="n">ret</span> <span class="o">=</span> <span class="bp">None</span>
  226. <span class="n">timer</span> <span class="o">=</span> <span class="bp">None</span>
  227. <span class="k">if</span> <span class="n">timeout</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
  228. <span class="n">timer</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="n">timeout</span><span class="p">,</span> <span class="n">timeout_event</span><span class="p">)</span>
  229. <span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  230. <span class="k">try</span><span class="p">:</span>
  231. <span class="k">while</span> <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span><span class="p">:</span>
  232. <span class="n">buf</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">read_data</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  233. <span class="k">if</span> <span class="n">buf</span> <span class="o">!=</span> <span class="s">&#39;&#39;</span><span class="p">:</span>
  234. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">+=</span> <span class="n">buf</span>
  235. <span class="k">if</span> <span class="n">buf</span> <span class="o">==</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">:</span>
  236. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
  237. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;</span><span class="se">\r</span><span class="s">&quot;</span><span class="p">:</span>
  238. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">[:</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
  239. <span class="c"># ignore if we just got \r\n with nothing else in the buffer.</span>
  240. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
  241. <span class="n">got_line</span> <span class="o">=</span> <span class="bp">True</span>
  242. <span class="k">break</span>
  243. <span class="k">else</span><span class="p">:</span>
  244. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
  245. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.001</span><span class="p">)</span>
  246. <span class="k">except</span> <span class="p">(</span><span class="n">usb</span><span class="o">.</span><span class="n">core</span><span class="o">.</span><span class="n">USBError</span><span class="p">,</span> <span class="n">FtdiError</span><span class="p">),</span> <span class="n">err</span><span class="p">:</span>
  247. <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
  248. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">(</span><span class="s">&#39;Error reading from AD2USB device: {0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">err</span><span class="p">)))</span>
  249. <span class="k">else</span><span class="p">:</span>
  250. <span class="k">if</span> <span class="n">got_line</span><span class="p">:</span>
  251. <span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span>
  252. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
  253. <span class="bp">self</span><span class="o">.</span><span class="n">on_read</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>
  254. <span class="k">if</span> <span class="n">timer</span><span class="p">:</span>
  255. <span class="k">if</span> <span class="n">timer</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
  256. <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
  257. <span class="k">else</span><span class="p">:</span>
  258. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">(</span><span class="s">&#39;Timeout while waiting for line terminator.&#39;</span><span class="p">)</span>
  259. <span class="k">return</span> <span class="n">ret</span>
  260. </div></div>
  261. <div class="viewcode-block" id="SerialDevice"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SerialDevice">[docs]</a><span class="k">class</span> <span class="nc">SerialDevice</span><span class="p">(</span><span class="n">Device</span><span class="p">):</span>
  262. <span class="sd">&quot;&quot;&quot;</span>
  263. <span class="sd"> AD2USB or AD2SERIAL device exposed with the pyserial interface.</span>
  264. <span class="sd"> &quot;&quot;&quot;</span>
  265. <span class="c"># Constants</span>
  266. <span class="n">BAUDRATE</span> <span class="o">=</span> <span class="mi">19200</span>
  267. <span class="nd">@staticmethod</span>
  268. <div class="viewcode-block" id="SerialDevice.find_all"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SerialDevice.find_all">[docs]</a> <span class="k">def</span> <span class="nf">find_all</span><span class="p">(</span><span class="n">pattern</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  269. <span class="sd">&quot;&quot;&quot;</span>
  270. <span class="sd"> Returns all serial ports present.</span>
  271. <span class="sd"> &quot;&quot;&quot;</span>
  272. <span class="n">devices</span> <span class="o">=</span> <span class="p">[]</span>
  273. <span class="k">try</span><span class="p">:</span>
  274. <span class="k">if</span> <span class="n">pattern</span><span class="p">:</span>
  275. <span class="n">devices</span> <span class="o">=</span> <span class="n">serial</span><span class="o">.</span><span class="n">tools</span><span class="o">.</span><span class="n">list_ports</span><span class="o">.</span><span class="n">grep</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span>
  276. <span class="k">else</span><span class="p">:</span>
  277. <span class="n">devices</span> <span class="o">=</span> <span class="n">serial</span><span class="o">.</span><span class="n">tools</span><span class="o">.</span><span class="n">list_ports</span><span class="o">.</span><span class="n">comports</span><span class="p">()</span>
  278. <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  279. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">(</span><span class="s">&#39;Error enumerating AD2SERIAL devices: {0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">err</span><span class="p">)))</span>
  280. <span class="k">return</span> <span class="n">devices</span>
  281. </div>
  282. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">interface</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  283. <span class="sd">&quot;&quot;&quot;</span>
  284. <span class="sd"> Constructor</span>
  285. <span class="sd"> &quot;&quot;&quot;</span>
  286. <span class="n">Device</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
  287. <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
  288. <span class="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="n">interface</span>
  289. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span> <span class="o">=</span> <span class="n">serial</span><span class="o">.</span><span class="n">Serial</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">writeTimeout</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="c"># Timeout = non-blocking to match pyftdi.</span>
  290. <div class="viewcode-block" id="SerialDevice.open"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SerialDevice.open">[docs]</a> <span class="k">def</span> <span class="nf">open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">baudrate</span><span class="o">=</span><span class="n">BAUDRATE</span><span class="p">,</span> <span class="n">interface</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">no_reader_thread</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
  291. <span class="sd">&quot;&quot;&quot;</span>
  292. <span class="sd"> Opens the device.</span>
  293. <span class="sd"> &quot;&quot;&quot;</span>
  294. <span class="c"># Set up the defaults</span>
  295. <span class="k">if</span> <span class="n">baudrate</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
  296. <span class="n">baudrate</span> <span class="o">=</span> <span class="n">SerialDevice</span><span class="o">.</span><span class="n">BAUDRATE</span>
  297. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">interface</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
  298. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">NoDeviceError</span><span class="p">(</span><span class="s">&#39;No AD2SERIAL device interface specified.&#39;</span><span class="p">)</span>
  299. <span class="k">if</span> <span class="n">interface</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  300. <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
  301. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span>
  302. <span class="c"># Open the device and start up the reader thread.</span>
  303. <span class="k">try</span><span class="p">:</span>
  304. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">open</span><span class="p">()</span>
  305. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">baudrate</span> <span class="o">=</span> <span class="n">baudrate</span> <span class="c"># NOTE: Setting the baudrate before opening the</span>
  306. <span class="c"># port caused issues with Moschip 7840/7820</span>
  307. <span class="c"># USB Serial Driver converter. (mos7840)</span>
  308. <span class="c">#</span>
  309. <span class="c"># Moving it to this point seems to resolve</span>
  310. <span class="c"># all issues with it.</span>
  311. <span class="k">except</span> <span class="p">(</span><span class="n">serial</span><span class="o">.</span><span class="n">SerialException</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">),</span> <span class="n">err</span><span class="p">:</span>
  312. <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
  313. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">NoDeviceError</span><span class="p">(</span><span class="s">&#39;Error opening AD2SERIAL device on port {0}.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">interface</span><span class="p">))</span>
  314. <span class="k">else</span><span class="p">:</span>
  315. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">True</span>
  316. <span class="bp">self</span><span class="o">.</span><span class="n">on_open</span><span class="p">((</span><span class="s">&#39;N/A&#39;</span><span class="p">,</span> <span class="s">&quot;AD2SERIAL&quot;</span><span class="p">))</span>
  317. <span class="k">if</span> <span class="ow">not</span> <span class="n">no_reader_thread</span><span class="p">:</span>
  318. <span class="bp">self</span><span class="o">.</span><span class="n">_read_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  319. </div>
  320. <div class="viewcode-block" id="SerialDevice.close"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SerialDevice.close">[docs]</a> <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  321. <span class="sd">&quot;&quot;&quot;</span>
  322. <span class="sd"> Closes the device.</span>
  323. <span class="sd"> &quot;&quot;&quot;</span>
  324. <span class="k">try</span><span class="p">:</span>
  325. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
  326. <span class="bp">self</span><span class="o">.</span><span class="n">_read_thread</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  327. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  328. <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  329. <span class="k">pass</span>
  330. <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
  331. </div>
  332. <div class="viewcode-block" id="SerialDevice.write"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SerialDevice.write">[docs]</a> <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  333. <span class="sd">&quot;&quot;&quot;</span>
  334. <span class="sd"> Writes data to the device.</span>
  335. <span class="sd"> &quot;&quot;&quot;</span>
  336. <span class="k">try</span><span class="p">:</span>
  337. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  338. <span class="k">except</span> <span class="n">serial</span><span class="o">.</span><span class="n">SerialTimeoutException</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  339. <span class="k">pass</span>
  340. <span class="k">except</span> <span class="n">serial</span><span class="o">.</span><span class="n">SerialException</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  341. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">(</span><span class="s">&#39;Error writing to serial device.&#39;</span><span class="p">)</span>
  342. <span class="k">else</span><span class="p">:</span>
  343. <span class="bp">self</span><span class="o">.</span><span class="n">on_write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  344. </div>
  345. <div class="viewcode-block" id="SerialDevice.read"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SerialDevice.read">[docs]</a> <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  346. <span class="sd">&quot;&quot;&quot;</span>
  347. <span class="sd"> Reads a single character from the device.</span>
  348. <span class="sd"> &quot;&quot;&quot;</span>
  349. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  350. </div>
  351. <div class="viewcode-block" id="SerialDevice.read_line"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SerialDevice.read_line">[docs]</a> <span class="k">def</span> <span class="nf">read_line</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
  352. <span class="sd">&quot;&quot;&quot;</span>
  353. <span class="sd"> Reads a line from the device.</span>
  354. <span class="sd"> &quot;&quot;&quot;</span>
  355. <span class="k">def</span> <span class="nf">timeout_event</span><span class="p">():</span>
  356. <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">False</span>
  357. <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">True</span>
  358. <span class="n">got_line</span> <span class="o">=</span> <span class="bp">False</span>
  359. <span class="n">ret</span> <span class="o">=</span> <span class="bp">None</span>
  360. <span class="n">timer</span> <span class="o">=</span> <span class="bp">None</span>
  361. <span class="k">if</span> <span class="n">timeout</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
  362. <span class="n">timer</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="n">timeout</span><span class="p">,</span> <span class="n">timeout_event</span><span class="p">)</span>
  363. <span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  364. <span class="k">try</span><span class="p">:</span>
  365. <span class="k">while</span> <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span><span class="p">:</span>
  366. <span class="n">buf</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  367. <span class="k">if</span> <span class="n">buf</span> <span class="o">!=</span> <span class="s">&#39;&#39;</span> <span class="ow">and</span> <span class="n">buf</span> <span class="o">!=</span> <span class="s">&quot;</span><span class="se">\xff</span><span class="s">&quot;</span><span class="p">:</span> <span class="c"># AD2SERIAL specifically apparently sends down \xFF on boot.</span>
  368. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">+=</span> <span class="n">buf</span>
  369. <span class="k">if</span> <span class="n">buf</span> <span class="o">==</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">:</span>
  370. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
  371. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;</span><span class="se">\r</span><span class="s">&quot;</span><span class="p">:</span>
  372. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">[:</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
  373. <span class="c"># ignore if we just got \r\n with nothing else in the buffer.</span>
  374. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
  375. <span class="n">got_line</span> <span class="o">=</span> <span class="bp">True</span>
  376. <span class="k">break</span>
  377. <span class="k">else</span><span class="p">:</span>
  378. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
  379. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.001</span><span class="p">)</span>
  380. <span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">serial</span><span class="o">.</span><span class="n">SerialException</span><span class="p">),</span> <span class="n">err</span><span class="p">:</span>
  381. <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
  382. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">(</span><span class="s">&#39;Error reading from AD2SERIAL device: {0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">err</span><span class="p">)))</span>
  383. <span class="k">else</span><span class="p">:</span>
  384. <span class="k">if</span> <span class="n">got_line</span><span class="p">:</span>
  385. <span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span>
  386. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
  387. <span class="bp">self</span><span class="o">.</span><span class="n">on_read</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>
  388. <span class="k">if</span> <span class="n">timer</span><span class="p">:</span>
  389. <span class="k">if</span> <span class="n">timer</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
  390. <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
  391. <span class="k">else</span><span class="p">:</span>
  392. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">(</span><span class="s">&#39;Timeout while waiting for line terminator.&#39;</span><span class="p">)</span>
  393. <span class="k">return</span> <span class="n">ret</span>
  394. </div></div>
  395. <div class="viewcode-block" id="SocketDevice"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SocketDevice">[docs]</a><span class="k">class</span> <span class="nc">SocketDevice</span><span class="p">(</span><span class="n">Device</span><span class="p">):</span>
  396. <span class="sd">&quot;&quot;&quot;</span>
  397. <span class="sd"> Device that supports communication with an AD2USB that is exposed via ser2sock or another</span>
  398. <span class="sd"> Serial to IP interface.</span>
  399. <span class="sd"> &quot;&quot;&quot;</span>
  400. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">interface</span><span class="o">=</span><span class="p">(</span><span class="s">&quot;localhost&quot;</span><span class="p">,</span> <span class="mi">10000</span><span class="p">)):</span>
  401. <span class="sd">&quot;&quot;&quot;</span>
  402. <span class="sd"> Constructor</span>
  403. <span class="sd"> &quot;&quot;&quot;</span>
  404. <span class="n">Device</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
  405. <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
  406. <span class="bp">self</span><span class="o">.</span><span class="n">_host</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_port</span> <span class="o">=</span> <span class="n">interface</span>
  407. <div class="viewcode-block" id="SocketDevice.open"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SocketDevice.open">[docs]</a> <span class="k">def</span> <span class="nf">open</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">baudrate</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">interface</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">no_reader_thread</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
  408. <span class="sd">&quot;&quot;&quot;</span>
  409. <span class="sd"> Opens the device.</span>
  410. <span class="sd"> &quot;&quot;&quot;</span>
  411. <span class="k">if</span> <span class="n">interface</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  412. <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
  413. <span class="bp">self</span><span class="o">.</span><span class="n">_host</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_port</span> <span class="o">=</span> <span class="n">interface</span>
  414. <span class="k">try</span><span class="p">:</span>
  415. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">socket</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">AF_INET</span><span class="p">,</span> <span class="n">socket</span><span class="o">.</span><span class="n">SOCK_STREAM</span><span class="p">)</span>
  416. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">connect</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">_host</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_port</span><span class="p">))</span>
  417. <span class="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="s">&#39;{0}:{1}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_host</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_port</span><span class="p">)</span>
  418. <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  419. <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
  420. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">NoDeviceError</span><span class="p">(</span><span class="s">&#39;Error opening AD2SOCKET device at {0}:{1}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_host</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_port</span><span class="p">))</span>
  421. <span class="k">else</span><span class="p">:</span>
  422. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">True</span>
  423. <span class="bp">self</span><span class="o">.</span><span class="n">on_open</span><span class="p">((</span><span class="s">&#39;N/A&#39;</span><span class="p">,</span> <span class="s">&quot;AD2SOCKET&quot;</span><span class="p">))</span>
  424. <span class="k">if</span> <span class="ow">not</span> <span class="n">no_reader_thread</span><span class="p">:</span>
  425. <span class="bp">self</span><span class="o">.</span><span class="n">_read_thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  426. </div>
  427. <div class="viewcode-block" id="SocketDevice.close"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SocketDevice.close">[docs]</a> <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  428. <span class="sd">&quot;&quot;&quot;</span>
  429. <span class="sd"> Closes the device.</span>
  430. <span class="sd"> &quot;&quot;&quot;</span>
  431. <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
  432. <span class="k">try</span><span class="p">:</span>
  433. <span class="bp">self</span><span class="o">.</span><span class="n">_read_thread</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
  434. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">shutdown</span><span class="p">(</span><span class="n">socket</span><span class="o">.</span><span class="n">SHUT_RDWR</span><span class="p">)</span> <span class="c"># Make sure that it closes immediately.</span>
  435. <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  436. <span class="k">except</span><span class="p">:</span>
  437. <span class="k">pass</span>
  438. <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
  439. </div>
  440. <div class="viewcode-block" id="SocketDevice.write"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SocketDevice.write">[docs]</a> <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
  441. <span class="sd">&quot;&quot;&quot;</span>
  442. <span class="sd"> Writes data to the device.</span>
  443. <span class="sd"> &quot;&quot;&quot;</span>
  444. <span class="n">data_sent</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  445. <span class="k">if</span> <span class="n">data_sent</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
  446. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">(</span><span class="s">&#39;Error while sending data.&#39;</span><span class="p">)</span>
  447. <span class="k">else</span><span class="p">:</span>
  448. <span class="bp">self</span><span class="o">.</span><span class="n">on_write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  449. <span class="k">return</span> <span class="n">data_sent</span>
  450. </div>
  451. <div class="viewcode-block" id="SocketDevice.read"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SocketDevice.read">[docs]</a> <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  452. <span class="sd">&quot;&quot;&quot;</span>
  453. <span class="sd"> Reads a single character from the device.</span>
  454. <span class="sd"> &quot;&quot;&quot;</span>
  455. <span class="k">try</span><span class="p">:</span>
  456. <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  457. <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  458. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">(</span><span class="s">&#39;Error while reading from device: {0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">err</span><span class="p">)))</span>
  459. <span class="k">return</span> <span class="n">data</span>
  460. </div>
  461. <div class="viewcode-block" id="SocketDevice.read_line"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.devices.SocketDevice.read_line">[docs]</a> <span class="k">def</span> <span class="nf">read_line</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
  462. <span class="sd">&quot;&quot;&quot;</span>
  463. <span class="sd"> Reads a line from the device.</span>
  464. <span class="sd"> &quot;&quot;&quot;</span>
  465. <span class="k">def</span> <span class="nf">timeout_event</span><span class="p">():</span>
  466. <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">False</span>
  467. <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">True</span>
  468. <span class="n">got_line</span> <span class="o">=</span> <span class="bp">False</span>
  469. <span class="n">ret</span> <span class="o">=</span> <span class="bp">None</span>
  470. <span class="n">timer</span> <span class="o">=</span> <span class="bp">None</span>
  471. <span class="k">if</span> <span class="n">timeout</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
  472. <span class="n">timer</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="n">timeout</span><span class="p">,</span> <span class="n">timeout_event</span><span class="p">)</span>
  473. <span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  474. <span class="k">try</span><span class="p">:</span>
  475. <span class="k">while</span> <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span><span class="p">:</span>
  476. <span class="n">buf</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_device</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  477. <span class="k">if</span> <span class="n">buf</span> <span class="o">!=</span> <span class="s">&#39;&#39;</span><span class="p">:</span>
  478. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">+=</span> <span class="n">buf</span>
  479. <span class="k">if</span> <span class="n">buf</span> <span class="o">==</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">:</span>
  480. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
  481. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;</span><span class="se">\r</span><span class="s">&quot;</span><span class="p">:</span>
  482. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">[:</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
  483. <span class="c"># ignore if we just got \r\n with nothing else in the buffer.</span>
  484. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
  485. <span class="n">got_line</span> <span class="o">=</span> <span class="bp">True</span>
  486. <span class="k">break</span>
  487. <span class="k">else</span><span class="p">:</span>
  488. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
  489. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.001</span><span class="p">)</span>
  490. <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  491. <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
  492. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">CommError</span><span class="p">(</span><span class="s">&#39;Error reading from Socket device: {0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">err</span><span class="p">)))</span>
  493. <span class="k">else</span><span class="p">:</span>
  494. <span class="k">if</span> <span class="n">got_line</span><span class="p">:</span>
  495. <span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span>
  496. <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
  497. <span class="bp">self</span><span class="o">.</span><span class="n">on_read</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>
  498. <span class="k">if</span> <span class="n">timer</span><span class="p">:</span>
  499. <span class="k">if</span> <span class="n">timer</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
  500. <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
  501. <span class="k">else</span><span class="p">:</span>
  502. <span class="k">raise</span> <span class="n">util</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">(</span><span class="s">&#39;Timeout while waiting for line terminator.&#39;</span><span class="p">)</span>
  503. <span class="k">return</span> <span class="n">ret</span></div></div>
  504. </pre></div>
  505. </div>
  506. </div>
  507. </div>
  508. <div class="sphinxsidebar">
  509. <div class="sphinxsidebarwrapper">
  510. <div id="searchbox" style="display: none">
  511. <h3>Quick search</h3>
  512. <form class="search" action="../../search.html" method="get">
  513. <input type="text" name="q" />
  514. <input type="submit" value="Go" />
  515. <input type="hidden" name="check_keywords" value="yes" />
  516. <input type="hidden" name="area" value="default" />
  517. </form>
  518. <p class="searchtip" style="font-size: 90%">
  519. Enter search terms or a module, class or function name.
  520. </p>
  521. </div>
  522. <script type="text/javascript">$('#searchbox').show(0);</script>
  523. </div>
  524. </div>
  525. <div class="clearer"></div>
  526. </div>
  527. <div class="related">
  528. <h3>Navigation</h3>
  529. <ul>
  530. <li class="right" style="margin-right: 10px">
  531. <a href="../../genindex.html" title="General Index"
  532. >index</a></li>
  533. <li class="right" >
  534. <a href="../../py-modindex.html" title="Python Module Index"
  535. >modules</a> |</li>
  536. <li><a href="../../index.html">pyad2usb documentation</a> &raquo;</li>
  537. <li><a href="../index.html" >Module code</a> &raquo;</li>
  538. </ul>
  539. </div>
  540. <div class="footer">
  541. &copy; Copyright 2013, Author.
  542. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
  543. </div>
  544. </body>
  545. </html>