<font face="courier new,monospace">Hi @all!</font><div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">I&#39;m trying to write a python library module for a special</font></div><div>
<font face="courier new,monospace">serial </font><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; ">communication protocol called IMPBUS. To use the serial</span></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; ">interface for sending and receiving packets as for now I&#39;m</span></div>
<div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; ">sub-classing pyserial. My code looks like this:</span></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; "><span class="Apple-style-span" style="color: rgb(82, 82, 82); font-family: &#39;Lucida Grande&#39;, Verdana, Arial, sans-serif; font-size: 12px; border-collapse: collapse; line-height: 16px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><pre style="font: normal normal normal 11px/normal &#39;Bitstream Vera Sans Mono&#39;, Monaco, &#39;Courier New&#39;, Courier, monospace; line-height: 1.4em !important; ">
<span class="kn">from</span> <font class="Apple-style-span" color="#555555">serial import </font><span class="n">Serial, SerialException</span>
<a name="cl-25" style="color: rgb(43, 84, 125); text-decoration: none; "></a><span class="kn">from</span> <span class="nn" style="color: rgb(85, 85, 85); ">serial</span> <span class="kn">import</span> <span class="n">EIGHTBITS</span><span class="p">,</span> <span class="n">PARITY_ODD</span><span class="p">,</span> <span class="n">STOPBITS_TWO</span>
<a name="cl-26" style="color: rgb(43, 84, 125); text-decoration: none; "></a><span class="kn">import</span> <span class="nn" style="color: rgb(85, 85, 85); ">binascii</span>
<a name="cl-27" style="color: rgb(43, 84, 125); text-decoration: none; "></a>
<a name="cl-28" style="color: rgb(43, 84, 125); text-decoration: none; "></a><span class="k" style="font-weight: bold; ">class</span> <span class="nc" style="color: rgb(68, 85, 136); font-weight: bold; ">SerialDevice</span><span class="p">(</span><span class="n">Serial</span><span class="p">):</span>
<a name="cl-29" style="color: rgb(43, 84, 125); text-decoration: none; "></a>
<a name="cl-30" style="color: rgb(43, 84, 125); text-decoration: none; "></a>    <span class="k" style="font-weight: bold; ">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold; ">__init__</span><span class="p">(</span><span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="p">,</span> <span class="n">port</span><span class="p">):</span>
<a name="cl-31" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="n">Serial</span><span class="o" style="font-weight: bold; ">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="p">)</span>
<a name="cl-32" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">port</span> <span class="o" style="font-weight: bold; ">=</span> <span class="n">port</span>
<a name="cl-33" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">baudrate</span> <span class="o" style="font-weight: bold; ">=</span> <span class="mi" style="color: rgb(0, 153, 153); ">57600</span>
<a name="cl-34" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">bytesize</span> <span class="o" style="font-weight: bold; ">=</span> <span class="n">EIGHTBITS</span>
<a name="cl-35" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">parity</span> <span class="o" style="font-weight: bold; ">=</span> <span class="n">PARITY_ODD</span>
<a name="cl-36" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">stopbits</span> <span class="o" style="font-weight: bold; ">=</span> <span class="n">STOPBITS_TWO</span>
<a name="cl-37" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">timeout</span> <span class="o" style="font-weight: bold; ">=</span> <span class="mi" style="color: rgb(0, 153, 153); ">0</span>
<a name="cl-38" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">xonxoff</span> <span class="o" style="font-weight: bold; ">=</span> <span class="mi" style="color: rgb(0, 153, 153); ">0</span>
<a name="cl-39" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">rtscts</span> <span class="o" style="font-weight: bold; ">=</span> <span class="mi" style="color: rgb(0, 153, 153); ">0</span>
<a name="cl-40" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">dsrdtr</span> <span class="o" style="font-weight: bold; ">=</span> <span class="mi" style="color: rgb(0, 153, 153); ">0</span>
<a name="cl-41" style="color: rgb(43, 84, 125); text-decoration: none; "></a>    
<a name="cl-42" style="color: rgb(43, 84, 125); text-decoration: none; "></a>    <span class="k" style="font-weight: bold; ">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold; ">_write</span><span class="p">(</span><span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="cl-43" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="n">fileno</span> <span class="o" style="font-weight: bold; ">=</span> <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">fileno</span><span class="p">()</span>
<a name="cl-44" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="k" style="font-weight: bold; ">while</span> <span class="bp" style="color: rgb(153, 153, 153); ">True</span><span class="p">:</span>
<a name="cl-45" style="color: rgb(43, 84, 125); text-decoration: none; "></a>            <span class="n">readable</span><span class="p">,</span> <span class="n">writeable</span><span class="p">,</span> <span class="n">excepts</span> <span class="o" style="font-weight: bold; ">=</span> <span class="n">select</span><span class="p">([],</span> <span class="p">[</span><span class="n">fileno</span><span class="p">],</span> <span class="p">[],</span> <span class="mf" style="color: rgb(0, 153, 153); ">0.1</span><span class="p">)</span>
<a name="cl-46" style="color: rgb(43, 84, 125); text-decoration: none; "></a>            <span class="k" style="font-weight: bold; ">if</span> <span class="n">fileno</span> <span class="ow" style="font-weight: bold; ">in</span> <span class="n">writeable</span><span class="p">:</span>
<a name="cl-47" style="color: rgb(43, 84, 125); text-decoration: none; "></a>                <span class="n">length</span> <span class="o" style="font-weight: bold; ">=</span> <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">write</span><span class="p">(</span><span class="n">packet</span><span class="p">)</span>
<a name="cl-48" style="color: rgb(43, 84, 125); text-decoration: none; "></a>                <span class="k" style="font-weight: bold; ">break</span>
<a name="cl-49" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="k" style="font-weight: bold; ">return</span> <span class="n">length</span>
<a name="cl-50" style="color: rgb(43, 84, 125); text-decoration: none; "></a>    
<a name="cl-51" style="color: rgb(43, 84, 125); text-decoration: none; "></a>    <span class="k" style="font-weight: bold; ">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold; ">_read</span><span class="p">(</span><span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="p">):</span>
<a name="cl-52" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="n">fileno</span> <span class="o" style="font-weight: bold; ">=</span> <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">fileno</span><span class="p">()</span>
<a name="cl-53" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="k" style="font-weight: bold; ">while</span> <span class="bp" style="color: rgb(153, 153, 153); ">True</span><span class="p">:</span>
<a name="cl-54" style="color: rgb(43, 84, 125); text-decoration: none; "></a>            <span class="n">readable</span><span class="p">,</span> <span class="n">writeable</span><span class="p">,</span> <span class="n">excepts</span> <span class="o" style="font-weight: bold; ">=</span> <span class="n">select</span><span class="p">([],</span> <span class="p">[</span><span class="n">fileno</span><span class="p">],</span> <span class="p">[],</span> <span class="mf" style="color: rgb(0, 153, 153); ">0.1</span><span class="p">)</span>
<a name="cl-55" style="color: rgb(43, 84, 125); text-decoration: none; "></a>            <span class="k" style="font-weight: bold; ">if</span> <span class="n">fileno</span> <span class="ow" style="font-weight: bold; ">in</span> <span class="n">readable</span><span class="p">:</span>
<a name="cl-56" style="color: rgb(43, 84, 125); text-decoration: none; "></a>                <span class="n">header</span> <span class="o" style="font-weight: bold; ">=</span> <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">read</span><span class="p">(</span><span class="mi" style="color: rgb(0, 153, 153); ">7</span><span class="p">)</span>
<a name="cl-57" style="color: rgb(43, 84, 125); text-decoration: none; "></a>                <span class="n">length</span> <span class="o" style="font-weight: bold; ">=</span> <span class="nb" style="color: rgb(153, 153, 153); ">int</span><span class="p">(</span><span class="n">binascii</span><span class="o" style="font-weight: bold; ">.</span><span class="n">b2a_hex</span><span class="p">(</span><span class="n">header</span><span class="p">[</span><span class="mi" style="color: rgb(0, 153, 153); ">3</span><span class="p">]),</span> <span class="mi" style="color: rgb(0, 153, 153); ">16</span><span class="p">)</span>
<a name="cl-58" style="color: rgb(43, 84, 125); text-decoration: none; "></a>                <span class="n">data</span> <span class="o" style="font-weight: bold; ">=</span> <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">read</span><span class="p">(</span><span class="n">length</span><span class="p">)</span>
<a name="cl-59" style="color: rgb(43, 84, 125); text-decoration: none; "></a>                <span class="n">packet</span> <span class="o" style="font-weight: bold; ">=</span> <span class="n">header</span> <span class="o" style="font-weight: bold; ">+</span> <span class="n">data</span>
<a name="cl-60" style="color: rgb(43, 84, 125); text-decoration: none; "></a>                <span class="k" style="font-weight: bold; ">break</span>
<a name="cl-61" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="k" style="font-weight: bold; ">return</span> <span class="n">packet</span>
<a name="cl-62" style="color: rgb(43, 84, 125); text-decoration: none; "></a>    
<a name="cl-63" style="color: rgb(43, 84, 125); text-decoration: none; "></a>    <span class="k" style="font-weight: bold; ">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold; ">talk</span><span class="p">(</span><span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="p">,</span> <span class="n">packet</span><span class="p">):</span>
<a name="cl-64" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">_write</span><span class="p">(</span><span class="n">packet</span><span class="p">)</span>
<a name="cl-65" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="n">responce</span> <span class="o" style="font-weight: bold; ">=</span> <span class="bp" style="color: rgb(153, 153, 153); ">self</span><span class="o" style="font-weight: bold; ">.</span><span class="n">_read</span><span class="p">()</span>
<a name="cl-66" style="color: rgb(43, 84, 125); text-decoration: none; "></a>        <span class="k" style="font-weight: bold; ">return</span> <span class="n">responce</span></pre></span></span></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">But the problem is that I can&#39;t use select with pyserial on Windows,</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">because it don&#39;t provide the </font><span class="Apple-style-span" style="font-family: &#39;Bitstream Vera Sans Mono&#39;, Monaco, &#39;Courier New&#39;, Courier, monospace; font-size: 11px; border-collapse: collapse; color: rgb(82, 82, 82); line-height: 15px; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><span class="n">fileno</span><span class="p">() <font class="Apple-style-span" color="#000000"><span class="Apple-style-span" style="border-collapse: separate; line-height: normal; white-space: normal; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; font-size: small;"><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">methode. So after some googling</font></span></font></span></span></div>
<div><span class="Apple-style-span" style="font-family: &#39;Bitstream Vera Sans Mono&#39;, Monaco, &#39;Courier New&#39;, Courier, monospace; font-size: 11px; border-collapse: collapse; color: rgb(82, 82, 82); line-height: 15px; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><span class="p"><font class="Apple-style-span" color="#000000"><span class="Apple-style-span" style="border-collapse: separate; line-height: normal; white-space: normal; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; font-size: small;"><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">I found </font></span></font></span></span><span class="Apple-style-span" style="font-family: &#39;Bitstream Vera Sans Mono&#39;, Monaco, &#39;Courier New&#39;, Courier, monospace; font-size: 11px; border-collapse: collapse; color: rgb(85, 85, 85); line-height: 15px; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">twisted.internet.serialport </span><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&quot;A select()able serial device, acting</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">as a transport.&quot; </font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">I never used twisted before so I&#39;m a little overwhelmed by how I can</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">replace pyserial with twisted in the code above ... </font><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">maybe someone can</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">point me to the right direction. It seems I need a &quot;Protocol&quot; and a</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">&quot;receiver&quot; ...</font> </div>
<div><br></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">- Markus</font></div><div><br>-- <br>__________________________________________________________________<br><br>IMKO Micromodultechnik GmbH<br>
Markus Hubig<br>System Administration &amp; Development<br>Im Stoeck 2<br>D-76275 Ettlingen / GERMANY<br><br>HR: HRB 360936 Amtsgericht Mannheim<br>President: Dipl.-Ing. (FH) Kurt Koehler<br><br>Tel: 0049-(0)7243-5921-26<br>
Fax: 0049-(0)7243-5921-40<br>e-mail: <a href="mailto:mhubig@imko.de">mhubig@imko.de</a><br>internet: <a href="http://www.imko.de">www.imko.de</a><br>_________________________________________________________________<br>
</div>