|
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
- <title>pyad2usb.devices — pyad2usb documentation</title>
-
- <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
- <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
-
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: '../../',
- VERSION: '',
- COLLAPSE_INDEX: false,
- FILE_SUFFIX: '.html',
- HAS_SOURCE: true
- };
- </script>
- <script type="text/javascript" src="../../_static/jquery.js"></script>
- <script type="text/javascript" src="../../_static/underscore.js"></script>
- <script type="text/javascript" src="../../_static/doctools.js"></script>
- <link rel="top" title="pyad2usb documentation" href="../../index.html" />
- <link rel="up" title="Module code" href="../index.html" />
- </head>
- <body>
- <div class="related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="../../genindex.html" title="General Index"
- accesskey="I">index</a></li>
- <li class="right" >
- <a href="../../py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><a href="../../index.html">pyad2usb documentation</a> »</li>
- <li><a href="../index.html" accesskey="U">Module code</a> »</li>
- </ul>
- </div>
-
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body">
-
- <h1>Source code for pyad2usb.devices</h1><div class="highlight"><pre>
- <span class="sd">"""</span>
- <span class="sd">Contains different types of devices belonging to the AD2USB family.</span>
- <span class="sd">"""</span>
-
- <span class="kn">import</span> <span class="nn">usb.core</span>
- <span class="kn">import</span> <span class="nn">usb.util</span>
- <span class="kn">import</span> <span class="nn">time</span>
- <span class="kn">import</span> <span class="nn">threading</span>
- <span class="kn">import</span> <span class="nn">serial</span>
- <span class="kn">import</span> <span class="nn">serial.tools.list_ports</span>
- <span class="kn">import</span> <span class="nn">socket</span>
- <span class="kn">from</span> <span class="nn">pyftdi.pyftdi.ftdi</span> <span class="kn">import</span> <span class="o">*</span>
- <span class="kn">from</span> <span class="nn">pyftdi.pyftdi.usbtools</span> <span class="kn">import</span> <span class="o">*</span>
- <span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">util</span>
- <span class="kn">from</span> <span class="nn">.event</span> <span class="kn">import</span> <span class="n">event</span>
-
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Generic parent device to all AD2USB products.</span>
- <span class="sd"> """</span>
-
- <span class="c"># Generic device events</span>
- <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">'Called when the device has been opened'</span><span class="p">)</span>
- <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">'Called when the device has been closed'</span><span class="p">)</span>
- <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">'Called when a line has been read from the device'</span><span class="p">)</span>
- <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">'Called when data has been written to the device'</span><span class="p">)</span>
-
- <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="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="s">''</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="s">''</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="bp">None</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_device</span> <span class="o">=</span> <span class="bp">None</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
- <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>
-
- <span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="k">pass</span>
-
- <span class="nd">@property</span>
- <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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_id</span>
-
- <span class="nd">@id.setter</span>
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="n">value</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Indicates whether or not the reader thread is alive.</span>
- <span class="sd"> """</span>
- <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>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Stops the reader thread.</span>
- <span class="sd"> """</span>
- <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>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Reader thread which processes messages from the device.</span>
- <span class="sd"> """</span>
-
- <span class="n">READ_TIMEOUT</span> <span class="o">=</span> <span class="mi">10</span>
-
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Constructor</span>
- <span class="sd"> """</span>
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">_device</span> <span class="o">=</span> <span class="n">device</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
-
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Stops the running thread.</span>
- <span class="sd"> """</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> The actual read process.</span>
- <span class="sd"> """</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">True</span>
-
- <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_running</span><span class="p">:</span>
- <span class="k">try</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">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>
- <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>
- <span class="k">pass</span>
-
- <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>
- </div></div></div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> AD2USB device exposed with PyFTDI's interface.</span>
- <span class="sd"> """</span>
-
- <span class="c"># Constants</span>
- <span class="n">FTDI_VENDOR_ID</span> <span class="o">=</span> <span class="mh">0x0403</span>
- <span class="n">FTDI_PRODUCT_ID</span> <span class="o">=</span> <span class="mh">0x6001</span>
- <span class="n">BAUDRATE</span> <span class="o">=</span> <span class="mi">115200</span>
-
- <span class="nd">@staticmethod</span>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Returns all FTDI devices matching our vendor and product IDs.</span>
- <span class="sd"> """</span>
- <span class="n">devices</span> <span class="o">=</span> <span class="p">[]</span>
-
- <span class="k">try</span><span class="p">:</span>
- <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>
- <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>
- <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">'Error enumerating AD2USB devices: {0}'</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>
-
- <span class="k">return</span> <span class="n">devices</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Constructor</span>
- <span class="sd"> """</span>
- <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>
-
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_vendor_id</span> <span class="o">=</span> <span class="n">vid</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_product_id</span> <span class="o">=</span> <span class="n">pid</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_serial_number</span> <span class="o">=</span> <span class="n">serial</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_description</span> <span class="o">=</span> <span class="n">description</span>
-
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Opens the device.</span>
- <span class="sd"> """</span>
- <span class="c"># Set up defaults</span>
- <span class="k">if</span> <span class="n">baudrate</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
- <span class="n">baudrate</span> <span class="o">=</span> <span class="n">USBDevice</span><span class="o">.</span><span class="n">BAUDRATE</span>
-
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="mi">0</span>
-
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
-
- <span class="k">if</span> <span class="n">index</span> <span class="ow">is</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="c"># Open the device and start up the thread.</span>
- <span class="k">try</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">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>
- <span class="bp">self</span><span class="o">.</span><span class="n">_product_id</span><span class="p">,</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span><span class="p">,</span>
- <span class="n">index</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>
-
- <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>
-
- <span class="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="s">'USB {0}:{1}'</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>
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
-
- <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">'Error opening AD2USB device: {0}'</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>
- <span class="k">else</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">True</span>
- <span class="k">if</span> <span class="ow">not</span> <span class="n">no_reader_thread</span><span class="p">:</span>
- <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>
-
- <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>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Closes the device.</span>
- <span class="sd"> """</span>
- <span class="k">try</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
- <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>
-
- <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>
-
- <span class="c"># HACK: Probably should fork pyftdi and make this call in .close().</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">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>
- <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>
- <span class="k">pass</span>
-
- <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Writes data to the device.</span>
- <span class="sd"> """</span>
- <span class="k">try</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">write_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
-
- <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>
- <span class="k">except</span> <span class="n">FtdiError</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
- <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">'Error writing to AD2USB device.'</span><span class="p">)</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Reads a single character from the device.</span>
- <span class="sd"> """</span>
- <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>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Reads a line from the device.</span>
- <span class="sd"> """</span>
- <span class="k">def</span> <span class="nf">timeout_event</span><span class="p">():</span>
- <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">False</span>
-
- <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">True</span>
-
- <span class="n">got_line</span> <span class="o">=</span> <span class="bp">False</span>
- <span class="n">ret</span> <span class="o">=</span> <span class="bp">None</span>
-
- <span class="n">timer</span> <span class="o">=</span> <span class="bp">None</span>
- <span class="k">if</span> <span class="n">timeout</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
- <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>
- <span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
-
- <span class="k">try</span><span class="p">:</span>
- <span class="k">while</span> <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span><span class="p">:</span>
- <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>
-
- <span class="k">if</span> <span class="n">buf</span> <span class="o">!=</span> <span class="s">''</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">+=</span> <span class="n">buf</span>
-
- <span class="k">if</span> <span class="n">buf</span> <span class="o">==</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">:</span>
- <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">1</span><span class="p">:</span>
- <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">"</span><span class="se">\r</span><span class="s">"</span><span class="p">:</span>
- <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>
-
- <span class="c"># ignore if we just got \r\n with nothing else in the buffer.</span>
- <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>
- <span class="n">got_line</span> <span class="o">=</span> <span class="bp">True</span>
- <span class="k">break</span>
- <span class="k">else</span><span class="p">:</span>
- <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>
-
- <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>
-
- <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>
- <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
-
- <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">'Error reading from AD2USB device: {0}'</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>
- <span class="k">else</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">got_line</span><span class="p">:</span>
- <span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="s">''</span>
-
- <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>
-
- <span class="k">if</span> <span class="n">timer</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">timer</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
- <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
- <span class="k">else</span><span class="p">:</span>
- <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">'Timeout while waiting for line terminator.'</span><span class="p">)</span>
-
- <span class="k">return</span> <span class="n">ret</span>
-
- </div></div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> AD2USB or AD2SERIAL device exposed with the pyserial interface.</span>
- <span class="sd"> """</span>
-
- <span class="c"># Constants</span>
- <span class="n">BAUDRATE</span> <span class="o">=</span> <span class="mi">19200</span>
-
- <span class="nd">@staticmethod</span>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Returns all serial ports present.</span>
- <span class="sd"> """</span>
- <span class="n">devices</span> <span class="o">=</span> <span class="p">[]</span>
-
- <span class="k">try</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">pattern</span><span class="p">:</span>
- <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>
- <span class="k">else</span><span class="p">:</span>
- <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>
- <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
- <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">'Error enumerating AD2SERIAL devices: {0}'</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>
-
- <span class="k">return</span> <span class="n">devices</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Constructor</span>
- <span class="sd"> """</span>
- <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>
-
- <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="n">interface</span>
- <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>
-
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Opens the device.</span>
- <span class="sd"> """</span>
- <span class="c"># Set up the defaults</span>
- <span class="k">if</span> <span class="n">baudrate</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
- <span class="n">baudrate</span> <span class="o">=</span> <span class="n">SerialDevice</span><span class="o">.</span><span class="n">BAUDRATE</span>
-
- <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>
- <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">'No AD2SERIAL device interface specified.'</span><span class="p">)</span>
-
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</span>
-
- <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>
-
- <span class="c"># Open the device and start up the reader thread.</span>
- <span class="k">try</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">open</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">baudrate</span> <span class="o">=</span> <span class="n">baudrate</span> <span class="c"># NOTE: Setting the baudrate before opening the</span>
- <span class="c"># port caused issues with Moschip 7840/7820</span>
- <span class="c"># USB Serial Driver converter. (mos7840)</span>
- <span class="c">#</span>
- <span class="c"># Moving it to this point seems to resolve</span>
- <span class="c"># all issues with it.</span>
-
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
-
- <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">'Error opening AD2SERIAL device on port {0}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">interface</span><span class="p">))</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">True</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">on_open</span><span class="p">((</span><span class="s">'N/A'</span><span class="p">,</span> <span class="s">"AD2SERIAL"</span><span class="p">))</span>
-
- <span class="k">if</span> <span class="ow">not</span> <span class="n">no_reader_thread</span><span class="p">:</span>
- <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>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Closes the device.</span>
- <span class="sd"> """</span>
- <span class="k">try</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
- <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>
-
- <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>
- <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
- <span class="k">pass</span>
-
- <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Writes data to the device.</span>
- <span class="sd"> """</span>
- <span class="k">try</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">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
- <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>
- <span class="k">pass</span>
- <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>
- <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">'Error writing to serial device.'</span><span class="p">)</span>
- <span class="k">else</span><span class="p">:</span>
- <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>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Reads a single character from the device.</span>
- <span class="sd"> """</span>
- <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>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Reads a line from the device.</span>
- <span class="sd"> """</span>
- <span class="k">def</span> <span class="nf">timeout_event</span><span class="p">():</span>
- <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">False</span>
-
- <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">True</span>
-
- <span class="n">got_line</span> <span class="o">=</span> <span class="bp">False</span>
- <span class="n">ret</span> <span class="o">=</span> <span class="bp">None</span>
-
- <span class="n">timer</span> <span class="o">=</span> <span class="bp">None</span>
- <span class="k">if</span> <span class="n">timeout</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
- <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>
- <span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
-
- <span class="k">try</span><span class="p">:</span>
- <span class="k">while</span> <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span><span class="p">:</span>
- <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>
-
- <span class="k">if</span> <span class="n">buf</span> <span class="o">!=</span> <span class="s">''</span> <span class="ow">and</span> <span class="n">buf</span> <span class="o">!=</span> <span class="s">"</span><span class="se">\xff</span><span class="s">"</span><span class="p">:</span> <span class="c"># AD2SERIAL specifically apparently sends down \xFF on boot.</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">+=</span> <span class="n">buf</span>
-
- <span class="k">if</span> <span class="n">buf</span> <span class="o">==</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">:</span>
- <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">1</span><span class="p">:</span>
- <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">"</span><span class="se">\r</span><span class="s">"</span><span class="p">:</span>
- <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>
-
- <span class="c"># ignore if we just got \r\n with nothing else in the buffer.</span>
- <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>
- <span class="n">got_line</span> <span class="o">=</span> <span class="bp">True</span>
- <span class="k">break</span>
- <span class="k">else</span><span class="p">:</span>
- <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>
-
- <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>
-
- <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>
- <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
-
- <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">'Error reading from AD2SERIAL device: {0}'</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>
- <span class="k">else</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">got_line</span><span class="p">:</span>
- <span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="s">''</span>
-
- <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>
-
- <span class="k">if</span> <span class="n">timer</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">timer</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
- <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
- <span class="k">else</span><span class="p">:</span>
- <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">'Timeout while waiting for line terminator.'</span><span class="p">)</span>
-
- <span class="k">return</span> <span class="n">ret</span>
- </div></div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Device that supports communication with an AD2USB that is exposed via ser2sock or another</span>
- <span class="sd"> Serial to IP interface.</span>
- <span class="sd"> """</span>
-
- <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">"localhost"</span><span class="p">,</span> <span class="mi">10000</span><span class="p">)):</span>
- <span class="sd">"""</span>
- <span class="sd"> Constructor</span>
- <span class="sd"> """</span>
- <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>
-
- <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</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="o">=</span> <span class="n">interface</span>
-
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Opens the device.</span>
- <span class="sd"> """</span>
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">_interface</span> <span class="o">=</span> <span class="n">interface</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="o">=</span> <span class="n">interface</span>
-
- <span class="k">try</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">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>
- <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>
-
- <span class="bp">self</span><span class="o">.</span><span class="n">_id</span> <span class="o">=</span> <span class="s">'{0}:{1}'</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>
-
- <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>
- <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
-
- <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">'Error opening AD2SOCKET device at {0}:{1}'</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>
- <span class="k">else</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">True</span>
-
- <span class="bp">self</span><span class="o">.</span><span class="n">on_open</span><span class="p">((</span><span class="s">'N/A'</span><span class="p">,</span> <span class="s">"AD2SOCKET"</span><span class="p">))</span>
-
- <span class="k">if</span> <span class="ow">not</span> <span class="n">no_reader_thread</span><span class="p">:</span>
- <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>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Closes the device.</span>
- <span class="sd"> """</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_running</span> <span class="o">=</span> <span class="bp">False</span>
-
- <span class="k">try</span><span class="p">:</span>
- <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>
- <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>
- <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>
- <span class="k">except</span><span class="p">:</span>
- <span class="k">pass</span>
-
- <span class="bp">self</span><span class="o">.</span><span class="n">on_close</span><span class="p">()</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Writes data to the device.</span>
- <span class="sd"> """</span>
- <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>
-
- <span class="k">if</span> <span class="n">data_sent</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
- <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">'Error while sending data.'</span><span class="p">)</span>
- <span class="k">else</span><span class="p">:</span>
- <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>
-
- <span class="k">return</span> <span class="n">data_sent</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Reads a single character from the device.</span>
- <span class="sd"> """</span>
- <span class="k">try</span><span class="p">:</span>
- <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>
- <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>
- <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">'Error while reading from device: {0}'</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>
-
- <span class="k">return</span> <span class="n">data</span>
- </div>
- <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>
- <span class="sd">"""</span>
- <span class="sd"> Reads a line from the device.</span>
- <span class="sd"> """</span>
- <span class="k">def</span> <span class="nf">timeout_event</span><span class="p">():</span>
- <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">False</span>
-
- <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">True</span>
-
- <span class="n">got_line</span> <span class="o">=</span> <span class="bp">False</span>
- <span class="n">ret</span> <span class="o">=</span> <span class="bp">None</span>
-
- <span class="n">timer</span> <span class="o">=</span> <span class="bp">None</span>
- <span class="k">if</span> <span class="n">timeout</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
- <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>
- <span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
-
- <span class="k">try</span><span class="p">:</span>
- <span class="k">while</span> <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span><span class="p">:</span>
- <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>
-
- <span class="k">if</span> <span class="n">buf</span> <span class="o">!=</span> <span class="s">''</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">+=</span> <span class="n">buf</span>
-
- <span class="k">if</span> <span class="n">buf</span> <span class="o">==</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">:</span>
- <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">1</span><span class="p">:</span>
- <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">"</span><span class="se">\r</span><span class="s">"</span><span class="p">:</span>
- <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>
-
- <span class="c"># ignore if we just got \r\n with nothing else in the buffer.</span>
- <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>
- <span class="n">got_line</span> <span class="o">=</span> <span class="bp">True</span>
- <span class="k">break</span>
- <span class="k">else</span><span class="p">:</span>
- <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>
-
- <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>
-
- <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>
- <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
-
- <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">'Error reading from Socket device: {0}'</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>
- <span class="k">else</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">got_line</span><span class="p">:</span>
- <span class="n">ret</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_buffer</span> <span class="o">=</span> <span class="s">''</span>
-
- <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>
-
- <span class="k">if</span> <span class="n">timer</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">timer</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
- <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
- <span class="k">else</span><span class="p">:</span>
- <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">'Timeout while waiting for line terminator.'</span><span class="p">)</span>
-
- <span class="k">return</span> <span class="n">ret</span></div></div>
- </pre></div>
-
- </div>
- </div>
- </div>
- <div class="sphinxsidebar">
- <div class="sphinxsidebarwrapper">
- <div id="searchbox" style="display: none">
- <h3>Quick search</h3>
- <form class="search" action="../../search.html" method="get">
- <input type="text" name="q" />
- <input type="submit" value="Go" />
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- <p class="searchtip" style="font-size: 90%">
- Enter search terms or a module, class or function name.
- </p>
- </div>
- <script type="text/javascript">$('#searchbox').show(0);</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="../../genindex.html" title="General Index"
- >index</a></li>
- <li class="right" >
- <a href="../../py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><a href="../../index.html">pyad2usb documentation</a> »</li>
- <li><a href="../index.html" >Module code</a> »</li>
- </ul>
- </div>
- <div class="footer">
- © Copyright 2013, Author.
- Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
- </div>
- </body>
- </html>
|