Robert Marciniak
Robert Marciniak

Reputation: 303

Python3 Struct unpack format string

I am using the python3 struct module to unpack byte data I extracted from a serial com. (With help) I've figured out how to unpack most the data into human readable form. I am having difficult with the format string on a group header struct group_hdr (please see attached screenshot document). I have a byte data (b). I know the character string for "word" is "H" but it's unclear to me from the document what phd_status is. It hasn't been defined anywhere else in the Data structure document. Any ideas?. Thank you in advance.

struct group_hdr
{
union phdb_status  status
word               label
}

subrecord = struct.unpack_from('<??H', b)

Data structure

Upvotes: 0

Views: 679

Answers (1)

Jongware
Jongware

Reputation: 22437

As is explained under Status, it is a simple bitfield with a width of 32 bits. The union is probably defined elsewhere in C (or a similar language) as

union phdb_status {
    unsigned int bit_0:1;
    unsigned int bit_1:1;
};

The following Python code will store your values:

status, label = struct.unpack_from('<IH', b)

and you can test the individual bits of status with status & 1 and status & 2.

Upvotes: 1

Related Questions