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.

230 lines
17 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.util &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.util</h1><div class="highlight"><pre>
  43. <span class="sd">&quot;&quot;&quot;</span>
  44. <span class="sd">Provides utility classes for the AD2USB devices.</span>
  45. <span class="sd">&quot;&quot;&quot;</span>
  46. <span class="kn">import</span> <span class="nn">ad2usb</span>
  47. <span class="kn">import</span> <span class="nn">time</span>
  48. <span class="kn">import</span> <span class="nn">traceback</span>
  49. <span class="kn">import</span> <span class="nn">threading</span>
  50. <div class="viewcode-block" id="NoDeviceError"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.NoDeviceError">[docs]</a><span class="k">class</span> <span class="nc">NoDeviceError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
  51. <span class="sd">&quot;&quot;&quot;</span>
  52. <span class="sd"> No devices found.</span>
  53. <span class="sd"> &quot;&quot;&quot;</span>
  54. <span class="k">pass</span>
  55. </div>
  56. <div class="viewcode-block" id="CommError"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.CommError">[docs]</a><span class="k">class</span> <span class="nc">CommError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
  57. <span class="sd">&quot;&quot;&quot;</span>
  58. <span class="sd"> There was an error communicating with the device.</span>
  59. <span class="sd"> &quot;&quot;&quot;</span>
  60. <span class="k">pass</span>
  61. </div>
  62. <div class="viewcode-block" id="TimeoutError"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.TimeoutError">[docs]</a><span class="k">class</span> <span class="nc">TimeoutError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
  63. <span class="sd">&quot;&quot;&quot;</span>
  64. <span class="sd"> There was a timeout while trying to communicate with the device.</span>
  65. <span class="sd"> &quot;&quot;&quot;</span>
  66. <span class="k">pass</span>
  67. </div>
  68. <div class="viewcode-block" id="InvalidMessageError"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.InvalidMessageError">[docs]</a><span class="k">class</span> <span class="nc">InvalidMessageError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
  69. <span class="sd">&quot;&quot;&quot;</span>
  70. <span class="sd"> The format of the panel message was invalid.</span>
  71. <span class="sd"> &quot;&quot;&quot;</span>
  72. <span class="k">pass</span>
  73. </div>
  74. <div class="viewcode-block" id="Firmware"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.Firmware">[docs]</a><span class="k">class</span> <span class="nc">Firmware</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  75. <span class="sd">&quot;&quot;&quot;</span>
  76. <span class="sd"> Represents firmware for the AD2USB/AD2SERIAL devices.</span>
  77. <span class="sd"> &quot;&quot;&quot;</span>
  78. <span class="c"># Constants</span>
  79. <span class="n">STAGE_START</span> <span class="o">=</span> <span class="mi">0</span>
  80. <span class="n">STAGE_WAITING</span> <span class="o">=</span> <span class="mi">1</span>
  81. <span class="n">STAGE_BOOT</span> <span class="o">=</span> <span class="mi">2</span>
  82. <span class="n">STAGE_LOAD</span> <span class="o">=</span> <span class="mi">3</span>
  83. <span class="n">STAGE_UPLOADING</span> <span class="o">=</span> <span class="mi">4</span>
  84. <span class="n">STAGE_DONE</span> <span class="o">=</span> <span class="mi">5</span>
  85. <span class="nd">@staticmethod</span>
  86. <div class="viewcode-block" id="Firmware.upload"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.Firmware.upload">[docs]</a> <span class="k">def</span> <span class="nf">upload</span><span class="p">(</span><span class="n">dev</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">progress_callback</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  87. <span class="sd">&quot;&quot;&quot;</span>
  88. <span class="sd"> Uploads firmware to an AD2USB/AD2SERIAL device.</span>
  89. <span class="sd"> &quot;&quot;&quot;</span>
  90. <span class="k">def</span> <span class="nf">do_upload</span><span class="p">():</span>
  91. <span class="sd">&quot;&quot;&quot;</span>
  92. <span class="sd"> Perform the actual firmware upload to the device.</span>
  93. <span class="sd"> &quot;&quot;&quot;</span>
  94. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  95. <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
  96. <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
  97. <span class="k">if</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;:&#39;</span><span class="p">:</span>
  98. <span class="n">dev</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\r</span><span class="s">&quot;</span><span class="p">)</span>
  99. <span class="n">res</span> <span class="o">=</span> <span class="n">dev</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="mf">10.0</span><span class="p">)</span>
  100. <span class="k">if</span> <span class="n">progress_callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  101. <span class="n">progress_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_UPLOADING</span><span class="p">)</span>
  102. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.05</span><span class="p">)</span>
  103. <span class="k">def</span> <span class="nf">read_until</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
  104. <span class="sd">&quot;&quot;&quot;</span>
  105. <span class="sd"> Read characters until a specific pattern is found or the timeout is hit.</span>
  106. <span class="sd"> &quot;&quot;&quot;</span>
  107. <span class="k">def</span> <span class="nf">timeout_event</span><span class="p">():</span>
  108. <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">False</span>
  109. <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">True</span>
  110. <span class="n">timer</span> <span class="o">=</span> <span class="bp">None</span>
  111. <span class="k">if</span> <span class="n">timeout</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
  112. <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>
  113. <span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  114. <span class="n">buf</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
  115. <span class="n">position</span> <span class="o">=</span> <span class="mi">0</span>
  116. <span class="k">while</span> <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span><span class="p">:</span>
  117. <span class="k">try</span><span class="p">:</span>
  118. <span class="n">char</span> <span class="o">=</span> <span class="n">dev</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  119. <span class="k">if</span> <span class="n">char</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">char</span> <span class="o">!=</span> <span class="s">&#39;&#39;</span><span class="p">:</span>
  120. <span class="k">if</span> <span class="n">char</span> <span class="o">==</span> <span class="n">pattern</span><span class="p">[</span><span class="n">position</span><span class="p">]:</span>
  121. <span class="n">position</span> <span class="o">=</span> <span class="n">position</span> <span class="o">+</span> <span class="mi">1</span>
  122. <span class="k">if</span> <span class="n">position</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">pattern</span><span class="p">):</span>
  123. <span class="k">break</span>
  124. <span class="k">else</span><span class="p">:</span>
  125. <span class="n">position</span> <span class="o">=</span> <span class="mi">0</span>
  126. <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
  127. <span class="k">pass</span>
  128. <span class="k">if</span> <span class="n">timer</span><span class="p">:</span>
  129. <span class="k">if</span> <span class="n">timer</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
  130. <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
  131. <span class="k">else</span><span class="p">:</span>
  132. <span class="k">raise</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>
  133. <span class="k">def</span> <span class="nf">stage_callback</span><span class="p">(</span><span class="n">stage</span><span class="p">):</span>
  134. <span class="k">if</span> <span class="n">progress_callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  135. <span class="n">progress_callback</span><span class="p">(</span><span class="n">stage</span><span class="p">)</span>
  136. <span class="k">if</span> <span class="n">dev</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
  137. <span class="k">raise</span> <span class="n">NoDeviceError</span><span class="p">(</span><span class="s">&#39;No device specified for firmware upload.&#39;</span><span class="p">)</span>
  138. <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_START</span><span class="p">)</span>
  139. <span class="k">if</span> <span class="n">dev</span><span class="o">.</span><span class="n">is_reader_alive</span><span class="p">():</span>
  140. <span class="c"># Close the reader thread and wait for it to die, otherwise</span>
  141. <span class="c"># it interferes with our reading.</span>
  142. <span class="n">dev</span><span class="o">.</span><span class="n">stop_reader</span><span class="p">()</span>
  143. <span class="k">while</span> <span class="n">dev</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>
  144. <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_WAITING</span><span class="p">)</span>
  145. <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  146. <span class="c"># Reboot the device and wait for the boot loader.</span>
  147. <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_BOOT</span><span class="p">)</span>
  148. <span class="n">dev</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;=&quot;</span><span class="p">)</span>
  149. <span class="n">read_until</span><span class="p">(</span><span class="s">&#39;!boot&#39;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">15.0</span><span class="p">)</span>
  150. <span class="c"># Get ourselves into the boot loader and wait for indication</span>
  151. <span class="c"># that it&#39;s ready for the firmware upload.</span>
  152. <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_LOAD</span><span class="p">)</span>
  153. <span class="n">dev</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;=&quot;</span><span class="p">)</span>
  154. <span class="n">read_until</span><span class="p">(</span><span class="s">&#39;!load&#39;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">15.0</span><span class="p">)</span>
  155. <span class="c"># And finally do the upload.</span>
  156. <span class="n">do_upload</span><span class="p">()</span>
  157. <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_DONE</span><span class="p">)</span></div></div>
  158. </pre></div>
  159. </div>
  160. </div>
  161. </div>
  162. <div class="sphinxsidebar">
  163. <div class="sphinxsidebarwrapper">
  164. <div id="searchbox" style="display: none">
  165. <h3>Quick search</h3>
  166. <form class="search" action="../../search.html" method="get">
  167. <input type="text" name="q" />
  168. <input type="submit" value="Go" />
  169. <input type="hidden" name="check_keywords" value="yes" />
  170. <input type="hidden" name="area" value="default" />
  171. </form>
  172. <p class="searchtip" style="font-size: 90%">
  173. Enter search terms or a module, class or function name.
  174. </p>
  175. </div>
  176. <script type="text/javascript">$('#searchbox').show(0);</script>
  177. </div>
  178. </div>
  179. <div class="clearer"></div>
  180. </div>
  181. <div class="related">
  182. <h3>Navigation</h3>
  183. <ul>
  184. <li class="right" style="margin-right: 10px">
  185. <a href="../../genindex.html" title="General Index"
  186. >index</a></li>
  187. <li class="right" >
  188. <a href="../../py-modindex.html" title="Python Module Index"
  189. >modules</a> |</li>
  190. <li><a href="../../index.html">pyad2usb documentation</a> &raquo;</li>
  191. <li><a href="../index.html" >Module code</a> &raquo;</li>
  192. </ul>
  193. </div>
  194. <div class="footer">
  195. &copy; Copyright 2013, Author.
  196. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
  197. </div>
  198. </body>
  199. </html>