0

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

asked Apr 22, 2020 at 8:06
1
  • I don't think this is related to Python. If you can't find the definition of this union, then you won't be able to proceed until you do find it. Or read further and notice that the status field is 32 bit wide, so you can replace it with any 32-bit datatype for now. Commented Apr 22, 2020 at 8:12

1 Answer 1

1

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.

answered Apr 22, 2020 at 8:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.