Question

I'm trying to send/receive data with an embedded device using a custom protocol. struct.pack() and unpack() seem like they'd do the job, but the fmt argument doesn't seem to be flexible enough to handle variable length arrays of data.

Example: device sends [event report] [number of events] ([event type] [event value], [event type] [event value], ...)

Where:

  • event report (unsigned 8-bit)
  • number of events (unsigned 16-bit)
  • event type (unsigned 8-bit)
  • event value (signed 16-bit)

What would be the best way to decode this data into Python? Also, how would I encode similar data structures to send to the device?

Was it helpful?

Solution

Consider using python-bitstring to implement binary protocols. It's quite flexible and easy to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top