{ "info": { "author": "Eric Oswald", "author_email": "eoswald39@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "============\r\nserialstruct\r\n============\r\n\r\n|build-status| |pypi|\r\n\r\nInstallation\r\n============\r\n\r\n.. code-block:: bash\r\n\r\n $ pip install serialstruct\r\n\r\n\r\nMotivation\r\n==========\r\nWhen sending a structured binary packet over Serial, the only way (that I'm aware\r\nof) to guarantee packet alignment with arbitrary data is to send a header that's\r\nlarger than any of the elements and add padding between each element. Here's an\r\nexample:\r\n\r\n.. code-block:: c\r\n\r\n struct Packet {\r\n int sensor1;\r\n int sensor2;\r\n }\r\n\r\nIf we send this over the wire and start reading at an arbitrary time, it's\r\nimpossible to know what byte of the packet we're reading. To mitigate this we can\r\nadd a header and some padding.\r\n\r\n.. code-block:: c\r\n\r\n struct Packet {\r\n char header[5]; // Any sequence without a '\\0'\r\n int sensor1;\r\n char pad1; // '\\0'\r\n int sensor2;\r\n char pad2; // '\\0'\r\n }\r\n\r\nNow we just need to wait until the header sequence is read and we can consume the\r\nrest of the packet without worrying about alignment.\r\n\r\npySerial only implements a FramedPacket which expects a unicode sequence that\r\nstarts with a '(' (0x28) and ends with a ')' (0x29). This means the bytes 0x28 and\r\n0x29 cannot appear anywhere in the binary data.\r\n\r\nUsage\r\n=====\r\nSubclass ``serialstruct.StructuredPacket`` to specify the data size in the packet\r\nand to implement the ``handle_packet()`` callback function.\r\n\r\n.. code-block:: python\r\n\r\n import struct\r\n\r\n import serial\r\n import serialstruct\r\n import time\r\n\r\n\r\n PACKET_STRUCT = struct.Struct(\"