Skip to main content

build-status Latest version released on PyPi

Installation

$ pip install serialstruct

Motivation

When sending a structured binary packet over Serial, the only way (that I’m aware of) to guarantee packet alignment with arbitrary data is to send a header that’s larger than any of the elements and add padding between each element. Here’s an example:

struct Packet {
        int sensor1;
        int sensor2;
}

If we send this over the wire and start reading at an arbitrary time, it’s impossible to know what byte of the packet we’re reading. To mitigate this we can add a header and some padding.

struct Packet {
        char header[5]; // Any sequence without a '\0'
        int sensor1;
        char pad1;      // '\0'
        int sensor2;
        char pad2;      // '\0'
}

Now we just need to wait until the header sequence is read and we can consume the rest of the packet without worrying about alignment.

pySerial only implements a FramedPacket which expects a unicode sequence that starts with a ‘(’ (0x28) and ends with a ‘)’ (0x29). This means the bytes 0x28 and 0x29 cannot appear anywhere in the binary data.

Usage

Subclass serialstruct.StructuredPacket to specify the data size in the packet and to implement the handle_packet() callback function.

import struct

import serial
import serialstruct
import time


PACKET_STRUCT = struct.Struct("<IxIx")

class MyPacket(serialstruct.StructuredPacket):

    DATA_SIZE = 10 # Excluding header: 4+1+4+1

    def __init__(self):
        super(MyPacket, self).__init__(self.DATA_SIZE)

    def handle_packet(self, packet):
        print(PACKET_STRUCT.unpack(packet))

    def send_packet(self, packet):
        self.transport.write(self.HEADER)
        self.transport.write(packet)


ser = serial.serial_for_url("loop://", baudrate=115200, timeout=1)
with serial.threaded.ReaderThread(ser, MyPacket) as protocol:
    # unsigned int, pad, unsigned int, pad; with no alignment
    packet = PACKET_STRUCT.pack(1, 2)
    protocol.send_packet(packet)
    time.sleep(1)

Prints:

(1, 2)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serialstruct-1.1.0.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file serialstruct-1.1.0.tar.gz.

File metadata

  • Download URL: serialstruct-1.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for serialstruct-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f6ee62ba5b37dc0d4a1c76060c0f3a2fd034b62eba79369a03ca92a182422da9
MD5 8567e3828295faf2dedd1cf490cb403b
BLAKE2b-256 4909a7aaf9b295109c76ca18c230f619c9ea7e9538acda8c247e4c2456c18042

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.0 This release

1 file

1.0.24

1 file

1.0.23

1 file

1.0.22

1 file

1.0.21

1 file

1.0.20

1 file

1.0.19

1 file

1.0.18

1 file

1.0.17

1 file

1.0.16

1 file

1.0.15

1 file

1.0.14

1 file

1.0.13

1 file

1.0.12

1 file

1.0.11

1 file

1.0.10

1 file

1.0.9

1 file

1.0.8

1 file

1.0.7

1 file

1.0.6

1 file

1.0.5

1 file

1.0.4

1 file

1.0.3

1 file

1.0.2

1 file

1.0.1

1 file

1.0.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page