문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top