<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Structures, Serialisation, XOR Checksums etc. - C++				            </title>
            <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/</link>
            <description>Discussion board for Robotics, Arduino, Raspberry Pi and other DIY electronics and modules. Join us today!</description>
            <language>en-US</language>
            <lastBuildDate>Mon, 20 Apr 2026 03:36:03 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/7/#post-6416</link>
                        <pubDate>Wed, 18 Dec 2019 13:24:03 +0000</pubDate>
                        <description><![CDATA[@frogandtoadHere&#039;s a bit of info on struct packing,After reading the article, I changed the sketch again to:typedef struct{
  float afloat = 15.4;
  uint8_t abyte = 12;  
  uint8_t bbyte ...]]></description>
                        <content:encoded><![CDATA[<p>@frogandtoad</p><blockquote><p>Here's a bit of info on struct packing,</p></blockquote><p>After reading the article, I changed the sketch again to:</p><pre><span style="color: #00979c">typedef</span> <span style="color: #00979c">struct</span><span style="color: #000000">{</span>
  <span style="color: #00979c">float</span> <span style="color: #000000">afloat</span> <span style="color: #434f54">=</span> <span style="color: #000000">15.4</span><span style="color: #000000">;</span>
  <span style="color: #00979c">uint8_t</span> <span style="color: #000000">abyte</span> <span style="color: #434f54">=</span> <span style="color: #000000">12</span><span style="color: #000000">;</span>  
  <span style="color: #00979c">uint8_t</span> <span style="color: #000000">bbyte</span> <span style="color: #434f54">=</span> <span style="color: #000000">5</span><span style="color: #000000">;</span>
  <span style="color: #000000">}</span><span style="color: #000000">data</span><span style="color: #000000">;</span>

<span style="color: #000000">data</span> <span style="color: #000000">myData</span><span style="color: #000000">;</span> 

</pre><p>and the Python code to:</p><pre>while True:<br /><br />    data = ser.read(6)<br /><br />    #struct.unpack(format, bytestring)<br />    tup = struct.unpack('fcc', data)<br />    print(data)<br />    print(tup)<br /><br />ser.close()</pre><p>And got the following aligned output:</p><pre class="p1"><span class="s1">(15.399999618530273, b'\x0c', b'\x05')</span><br /><span class="s1">b'ffvA\x0c\x05'</span><br /><span class="s1">(15.399999618530273, b'\x0c', b'\x05')</span><br /><span class="s1">b'ffvA\x0c\x05'</span></pre><p>I guess I can get back to my pre-Xmas stress now ? </p><p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/7/#post-6416</guid>
                    </item>
				                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6414</link>
                        <pubDate>Wed, 18 Dec 2019 13:09:07 +0000</pubDate>
                        <description><![CDATA[@pugwash, @ZeFerbyGood one, ZeFerby!Professor pugwash... you&#039;ve taken this to the next level :)Here&#039;s a bit of info on struct packing, as it can be done on the C/C++ side too:struct-packingN...]]></description>
                        <content:encoded><![CDATA[<p>@pugwash, @ZeFerby</p><p>Good one, ZeFerby!</p><p>Professor pugwash... you've taken this to the next level :)<br />Here's a bit of info on struct packing, as it can be done on the C/C++ side too:</p><p><a href="http://www.joshcaratelli.com/blog/struct-packing" target="true">struct-packing</a></p><p><strong>Note: -</strong> There are also maco preprocessor directives (compiler specific), that you can include in your code to pack structs for specific needs.</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>frogandtoad</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6414</guid>
                    </item>
				                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6413</link>
                        <pubDate>Wed, 18 Dec 2019 12:39:48 +0000</pubDate>
                        <description><![CDATA[@zeferbyExcellent, that worked!The output is aligned now.(b&#039;\x0c&#039;, 15.399999618530273, b&#039;\x05&#039;)(b&#039;\x0c&#039;, 15.399999618530273, b&#039;\x05&#039;)(b&#039;\x0c&#039;, 15.399999618530273, b&#039;\x05&#039;)Thanks for the othe...]]></description>
                        <content:encoded><![CDATA[<p>@zeferby</p><p>Excellent, that worked!</p><p>The output is aligned now.</p><pre class="p1"><span class="s1">(b'\x0c', 15.399999618530273, b'\x05')</span><br /><span class="s1">(b'\x0c', 15.399999618530273, b'\x05')</span><br /><span class="s1">(b'\x0c', 15.399999618530273, b'\x05')</span></pre><p>Thanks for the other tip, it wasn't as difficult as I first thought!</p><pre>Serial.write((const byte*)&amp;myData, sizeof(myData));</pre><p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6413</guid>
                    </item>
				                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6410</link>
                        <pubDate>Wed, 18 Dec 2019 12:24:56 +0000</pubDate>
                        <description><![CDATA[@pugwashHum, I think there are &quot;alignment&quot; considerations here...&gt;&gt;&gt; import struct&gt;&gt;&gt; size = struct.calcsize(&#039;c&#039;)&gt;&gt;&gt; print(size)1&gt;&gt;&gt; size = struct.calcsiz...]]></description>
                        <content:encoded><![CDATA[<p>@pugwash</p><p>Hum, I think there are "alignment" considerations here...</p><pre>&gt;&gt;&gt; import struct<br />&gt;&gt;&gt; size = struct.calcsize('c')<br />&gt;&gt;&gt; print(size)<br />1<br />&gt;&gt;&gt; size = struct.calcsize('f')<br />&gt;&gt;&gt; print(size)<br />4<br />&gt;&gt;&gt; size = struct.calcsize('cc')<br />&gt;&gt;&gt; print(size)<br />2<br />&gt;&gt;&gt; size = struct.calcsize('cf')<br />&gt;&gt;&gt; print(size)<br />8<br />&gt;&gt;&gt; size = struct.calcsize('fc')<br />&gt;&gt;&gt; print(size)<br />5<br />&gt;&gt;&gt; size = struct.calcsize('ccf')<br />&gt;&gt;&gt; print(size)<br />8<br />&gt;&gt;&gt; size = struct.calcsize('fcc')<br />&gt;&gt;&gt; print(size)<br />6<br />&gt;&gt;&gt; size = struct.calcsize('cfc')<br />&gt;&gt;&gt; print(size)<br />9<br />&gt;&gt;&gt; exit()<br /><br />From Python 3 "struct" doc :</pre><blockquote><p class="admonition-title">Note</p><p> </p><p>By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct. To handle platform-independent data formats or omit implicit pad bytes, use <code class="docutils literal notranslate"><span class="pre">standard</span></code> size and alignment instead of <code class="docutils literal notranslate"><span class="pre">native</span></code> size and alignment: see <a class="reference internal" href="https://docs.python.org/3/library/struct.html#struct-alignment"><span class="std std-ref">Byte Order, Size, and Alignment</span></a> for details.</p></blockquote><p>So, using the "=" modifier to specify No Alignment :</p><pre>&gt;&gt;&gt; import struct<br />&gt;&gt;&gt; size = struct.calcsize('=cf')<br />&gt;&gt;&gt; print(size)<br />5<br />&gt;&gt;&gt; size = struct.calcsize('=fc')<br />&gt;&gt;&gt; print(size)<br />5<br />&gt;&gt;&gt; size = struct.calcsize('=ccf')<br />&gt;&gt;&gt; print(size)<br />6<br />&gt;&gt;&gt; size = struct.calcsize('=fcc')<br />&gt;&gt;&gt; print(size)<br />6<br />&gt;&gt;&gt; size = struct.calcsize('=cfc')<br />&gt;&gt;&gt; print(size)<br />6<br />&gt;&gt;&gt; exit()</pre>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>ZeFerby</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6410</guid>
                    </item>
				                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6409</link>
                        <pubDate>Wed, 18 Dec 2019 11:08:24 +0000</pubDate>
                        <description><![CDATA[@frogandtoadI ran this and it also came back 9size = struct.calcsize(&#039;cfc&#039;) print(size)Ergo, I have size problem (don&#039;t even think it ? )The Arduino is sending out 6 bytes and Python wants 9...]]></description>
                        <content:encoded><![CDATA[<p>@frogandtoad</p><p>I ran this and it also came back 9</p><pre>size = struct.calcsize(<span class="hljs-string">'cfc'</span>) <br /><span class="hljs-built_in">print</span>(size)</pre><p>Ergo, I have size problem (don't even think it ? )</p><p>The Arduino is sending out 6 bytes and Python wants 9 bytes!</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6409</guid>
                    </item>
				                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6408</link>
                        <pubDate>Wed, 18 Dec 2019 10:57:53 +0000</pubDate>
                        <description><![CDATA[@frogandtoadProgress so far is quite interesting. The story so far!!The data throwing sketch is this:typedef struct{
  uint8_t abyte = 12;
  float afloat = 15.4;
  uint8_t bbyte = 5;
  }...]]></description>
                        <content:encoded><![CDATA[<p>@frogandtoad</p><p>Progress so far is quite interesting. The story so far!!</p><p>The data throwing sketch is this:</p><pre><span style="color: #00979c">typedef</span> <span style="color: #00979c">struct</span><span style="color: #000000">{</span>
  <span style="color: #00979c">uint8_t</span> <span style="color: #000000">abyte</span> <span style="color: #434f54">=</span> <span style="color: #000000">12</span><span style="color: #000000">;</span>
  <span style="color: #00979c">float</span> <span style="color: #000000">afloat</span> <span style="color: #434f54">=</span> <span style="color: #000000">15.4</span><span style="color: #000000">;</span>
  <span style="color: #00979c">uint8_t</span> <span style="color: #000000">bbyte</span> <span style="color: #434f54">=</span> <span style="color: #000000">5</span><span style="color: #000000">;</span>
  <span style="color: #000000">}</span><span style="color: #000000">data</span><span style="color: #000000">;</span>

<span style="color: #000000">data</span> <span style="color: #000000">myData</span><span style="color: #000000">;</span> 


<span style="color: #00979c">void</span> <span style="color: #5e6d03">setup</span><span style="color: #000000">(</span><span style="color: #000000">)</span> <span style="color: #000000">{</span>
  <strong><span style="color: #d35400">Serial</span></strong><span style="color: #434f54">.</span><span style="color: #d35400">begin</span><span style="color: #000000">(</span><span style="color: #000000">115200</span><span style="color: #000000">)</span><span style="color: #000000">;</span>

<span style="color: #000000">}</span>

<span style="color: #00979c">void</span> <span style="color: #5e6d03">loop</span><span style="color: #000000">(</span><span style="color: #000000">)</span> <span style="color: #000000">{</span> 

  <span style="color: #d35400">delay</span><span style="color: #000000">(</span><span style="color: #000000">5000</span><span style="color: #000000">)</span><span style="color: #000000">;</span>
  <strong><span style="color: #d35400">Serial</span></strong><span style="color: #434f54">.</span><span style="color: #d35400">write</span><span style="color: #000000">(</span><span style="color: #000000">(</span><span style="color: #00979c">const</span> <span style="color: #00979c">byte</span><span style="color: #434f54">*</span><span style="color: #000000">)</span><span style="color: #434f54">&amp;</span><span style="color: #000000">myData</span><span style="color: #434f54">,</span> <span style="color: #00979c">sizeof</span><span style="color: #000000">(</span><span style="color: #000000">myData</span><span style="color: #000000">)</span><span style="color: #000000">)</span><span style="color: #000000">;</span>

<span style="color: #000000">}</span>

</pre><p>And the receiving program is this (without indents):</p><p> </p><pre>import serial<br />import struct<br /><br />my_comm_port = '/dev/cu.wchusbserial1460'; # *nix comm port notation<br /><br />try:<br /><br /># Change the baud rate here if different than 115200<br />ser = serial.Serial(<br />port='/dev/cu.wchusbserial1460',\<br />baudrate=115200,\<br />parity=serial.PARITY_NONE,\<br />stopbits=serial.STOPBITS_ONE,\<br />bytesize=serial.EIGHTBITS,\<br />timeout=None<br />)<br /><br />print("connected to: " + ser.portstr)<br /><br />except IOError:<br /><br />print("Invalid comm port!")<br />exit(1)<br /><br /><br />while True:<br /><br />data = ser.read(6)<br /><br />#struct.unpack(format, bytestring)<br />tup = struct.unpack('cfc', data)<br /><br />print(tup)<br /><br />ser.close()</pre><p>'cfc' is theoretically 6 bytes long but I am getting "<span class="s1">struct.error: unpack requires a bytes object of length 9</span>"</p><p>when I change to ser.read(9), I am getting the following screen output</p><blockquote><p class="p1"><span class="s1">seaswallow:serial_read stevec$ Python3 struct_catcher.py</span></p><p class="p1"><span class="s1">connected to: /dev/cu.wchusbserial1460</span></p><p class="p1"><span class="s1">(b'\x0c', 1.6530705626643284e+23, b'f')</span></p><p class="p1"><span class="s1">(b'v', 15.399999618530273, b'\x05')</span></p><p class="p1"><span class="s1">(b'\x0c', 1.6530705626643284e+23, b'f')</span></p><p class="p1"><span class="s1">(b'v', 15.399999618530273, b'\x05')</span></p><p class="p1"><span class="s1">(b'\x0c', 1.6530705626643284e+23, b'f')</span></p><p class="p1"><span class="s1">(b'v', 15.399999618530273, b'\x05')</span></p></blockquote><p>As you can see on every alternate line the float is correct and the two bytes are correct, so I guess there has to be some fine-tuning to sync the byte strings.</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6408</guid>
                    </item>
				                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6392</link>
                        <pubDate>Tue, 17 Dec 2019 14:16:18 +0000</pubDate>
                        <description><![CDATA[@frogandtoadInteresting, so did your update work?No, not yet, too much pre-Xmas stress. I have to modify the receiver sketch first and then write the data catcher in Python.JSON did cross my...]]></description>
                        <content:encoded><![CDATA[<p>@frogandtoad</p><blockquote><p>Interesting, so did your update work?</p></blockquote><p>No, not yet, too much pre-Xmas stress. I have to modify the receiver sketch first and then write the data catcher in Python.</p><p>JSON did cross my mind but uncrossed it just as fast! If I had gone down the javascript route and used Electron as a front end, it might have been interesting but I guess I will just stick with Python and Tkinter for the time being.</p><p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6392</guid>
                    </item>
				                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6390</link>
                        <pubDate>Tue, 17 Dec 2019 13:53:46 +0000</pubDate>
                        <description><![CDATA[@pugwash Posted by: @pugwash Edited: &quot;qbffffb&quot; changed to &quot;Lcffffc&quot; This seems to be the correct format!Interesting, so did your update work?Similarly, it sounds like you may want to work wi...]]></description>
                        <content:encoded><![CDATA[<p>@pugwash</p><blockquote data-userid="30" data-postid="6382" data-mention="pugwash"><div class="wpforo-post-quote-author"><strong> Posted by: @pugwash </strong></div><p>Edited: "qbffffb" changed to "Lcffffc" This seems to be the correct format!</p></blockquote><p>Interesting, so did your update work?</p><p>Similarly, it sounds like you may want to work with JSON objects, which have been mostly adopted for portable communication between different systems over a network, and quite versatile:</p><p><a href="https://www.w3schools.com/python/python_json.asp" target="true">python-json</a></p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>frogandtoad</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6390</guid>
                    </item>
				                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6383</link>
                        <pubDate>Tue, 17 Dec 2019 11:12:21 +0000</pubDate>
                        <description><![CDATA[@pugwashSorry I never used Python. According to the doc, &quot;q&quot; is for signed, 8-byte/64-bit integers.  I don&#039;t know what structure you are passing between Arduino/C++ and Python though]]></description>
                        <content:encoded><![CDATA[<p>@pugwash</p><p>Sorry I never used Python. According to the doc, "q" is for signed, 8-byte/64-bit integers.  I don't know what structure you are passing between Arduino/C++ and Python though.</p><p><a href="https://docs.python.org/2/library/struct.html">https://docs.python.org/2/library/struct.html</a></p><p><a href="https://docs.python.org/3/library/struct.html">https://docs.python.org/3/library/struct.html</a></p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>ZeFerby</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6383</guid>
                    </item>
				                    <item>
                        <title>RE: Structures, Serialisation, XOR Checksums etc.</title>
                        <link>https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6382</link>
                        <pubDate>Tue, 17 Dec 2019 11:01:00 +0000</pubDate>
                        <description><![CDATA[@frogandtoad, @zeferbyOn the data receiving end, I am looking at using this python code and method(struct.unpack):import serialmy_comm_port = &#039;/dev/cu.usbserial-00000000&#039;; # *nix comm port n...]]></description>
                        <content:encoded><![CDATA[<p>@frogandtoad, @zeferby</p><p>On the data receiving end, I am looking at using this python code and method(struct.unpack):</p><pre>import serial<br /><br />my_comm_port = '/dev/cu.usbserial-00000000'; # *nix comm port notation<br /><br />try:<br /><br />   # Change the baud rate here if different than 9600<br />   ser = serial.Serial(port=my_comm_port, baudrate=115200)<br /><br />except IOError:<br /><br />   print("Invalid comm port!")<br />   exit(1)<br /><br />def main():<br /><br />   while 1:<br /><br />      data = ser.readline()<br /><br />      # struct.unpack(format, bytestring)<br />      tup = struct.unpack(Lcffffc, data)<br /><br />      print(tup)</pre><p>????? I am not entirely sure whether "q" is the short form for an unsigned long integer!</p><p>Edited: "qbffffb" changed to "Lcffffc" This seems to be the correct format!</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/c-plus-plus/">C++</category>                        <dc:creator>Pugwash</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/c-plus-plus/structures-serialisation-xor-checksums-etc/paged/6/#post-6382</guid>
                    </item>
							        </channel>
        </rss>
		