<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[UART binary issue - might help others]]></title><description><![CDATA[<p dir="auto">I recently moved some code over from a project I had running on HLK-7688A devices.<br />
It uses RS485 to communicate with other devices.</p>
<p dir="auto">I simply re-compiled the code using the Onion cross-compiler and it all seemed to work nicely.</p>
<p dir="auto">Except one thing...<br />
Every now and then I would get a corrupt message from a remote device. A chksum embedded in the protocol would not match, and ocasionally a length byte in the received bytes would be wrong too.<br />
I thought it might be just a noisy RS485 line but when probing the actual RX line going into the Omega showed a perfectly valid signal.</p>
<p dir="auto">On the wire the bytes would look something like this (in hex):</p>
<pre><code>21 24 56 58 0D 00 01 00
</code></pre>
<p dir="auto">But in the app the bytes being read() from the port would be something like:</p>
<pre><code>21 24 56 58 0A 00 01 00
</code></pre>
<p dir="auto">Thing is that the error was consistently the same error. Not like noise or random bits being flipped. Always the exact same corruption. I had never seen this before, this code had run fine on the HLK-7688A</p>
<p dir="auto">It turns out that it was the terminal options I was setting on the file descriptor of the port I opened using open().<br />
After opening the port with</p>
<pre><code>fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
</code></pre>
<p dir="auto">I then use <em>tcgetattr</em> and <em>tcsetattr</em> to get the port options and modify things like the baudrate, parity, flow-control etc. I was using this:</p>
<pre><code>options.c_iflag &amp;= ~(IXON | IXOFF | IXANY);
</code></pre>
<p dir="auto">But I had to change it to :</p>
<pre><code>options.c_iflag &amp;= ~(IXON | IXOFF | IXANY | ISTRIP | ICRNL);
</code></pre>
<p dir="auto">To stop the pre-parser stripping the 8th bit from bytes and to stop it converting 0D to 0A (ie. CR to NL).</p>
<p dir="auto">This is a bit of a simplified explanation, but it shows it pays to be careful with the options flags!<br />
Funny how this never came up on the HLK-7688A - but they do use a very different build from OpenWRT.</p>
]]></description><link>http://community.onion.io/topic/4857/uart-binary-issue-might-help-others</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Jul 2026 08:27:45 GMT</lastBuildDate><atom:link href="http://community.onion.io/topic/4857.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 26 Jul 2022 11:11:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to UART binary issue - might help others on Tue, 26 Jul 2022 11:11:09 GMT]]></title><description><![CDATA[<p dir="auto">I recently moved some code over from a project I had running on HLK-7688A devices.<br />
It uses RS485 to communicate with other devices.</p>
<p dir="auto">I simply re-compiled the code using the Onion cross-compiler and it all seemed to work nicely.</p>
<p dir="auto">Except one thing...<br />
Every now and then I would get a corrupt message from a remote device. A chksum embedded in the protocol would not match, and ocasionally a length byte in the received bytes would be wrong too.<br />
I thought it might be just a noisy RS485 line but when probing the actual RX line going into the Omega showed a perfectly valid signal.</p>
<p dir="auto">On the wire the bytes would look something like this (in hex):</p>
<pre><code>21 24 56 58 0D 00 01 00
</code></pre>
<p dir="auto">But in the app the bytes being read() from the port would be something like:</p>
<pre><code>21 24 56 58 0A 00 01 00
</code></pre>
<p dir="auto">Thing is that the error was consistently the same error. Not like noise or random bits being flipped. Always the exact same corruption. I had never seen this before, this code had run fine on the HLK-7688A</p>
<p dir="auto">It turns out that it was the terminal options I was setting on the file descriptor of the port I opened using open().<br />
After opening the port with</p>
<pre><code>fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
</code></pre>
<p dir="auto">I then use <em>tcgetattr</em> and <em>tcsetattr</em> to get the port options and modify things like the baudrate, parity, flow-control etc. I was using this:</p>
<pre><code>options.c_iflag &amp;= ~(IXON | IXOFF | IXANY);
</code></pre>
<p dir="auto">But I had to change it to :</p>
<pre><code>options.c_iflag &amp;= ~(IXON | IXOFF | IXANY | ISTRIP | ICRNL);
</code></pre>
<p dir="auto">To stop the pre-parser stripping the 8th bit from bytes and to stop it converting 0D to 0A (ie. CR to NL).</p>
<p dir="auto">This is a bit of a simplified explanation, but it shows it pays to be careful with the options flags!<br />
Funny how this never came up on the HLK-7688A - but they do use a very different build from OpenWRT.</p>
]]></description><link>http://community.onion.io/post/24718</link><guid isPermaLink="true">http://community.onion.io/post/24718</guid><dc:creator><![CDATA[SpiderKenny]]></dc:creator><pubDate>Tue, 26 Jul 2022 11:11:09 GMT</pubDate></item></channel></rss>