在 Python 中解压复杂的嵌套 C 结构

jpf*_*der 4 c python struct nested unpack

这是我上一个问题“在 Python 中解压嵌套 C 结构”的后续问题。我正在使用的结构变得更加复杂,我再次不确定如何完全解压和处理它们。

在C端,标头的相关部分如下所示:

typedef struct {
    uint8_t seq;
    uint8_t type;
    uint16_t flags;
    uint16_t upTimestamp;
}__attribute__ ((packed)) mps_packet_header;

typedef struct {
    mps_packet_header header;
    int16_t x[6];
    int16_t y[6];
    int16_t z[6];
    uint16_t lowTimestmp[6];
}__attribute__ ((packed)) mps_acc_packet_t;
typedef mps_acc_packet_t accpacket_t;

typedef struct {
    int16_t hb1;
    int16_t hb2;
} acam_hb_data_set;

typedef struct __attribute__ ((packed)) {
    mps_packet_header header;
    uint16_t temp;
    uint16_t lowTimestmp[8];
    acam_hb_data_set dms_data[8];
} mps_dms_packet_t;
Run Code Online (Sandbox Code Playgroud)

由此产生了两个挑战。首先,我收到的数据包(以二进制形式)可以是mps_acc_packet_tmps_dms_packet_t- 区分它们的唯一方法是读取两种数据包类型type所具有的字段mps_packet_header。这意味着我需要在知道数据包的完整内容之前将其解压,但我不知道如何干净地完成此操作,因为(如果我没有记错的话)这两种数据包类型有不同(calcsize分别为 54 和 56)。第二个挑战是解压mps_dms_packet_t;从结构体的定义中可以看出,该数据包有一个由 8 个实例组成的数组acam_hb_data_set,而该数组又是一个由两个int16值组成的结构体。我不知道如何为此制定正确的格式字符串。

我以前的代码(在mps_dms_packet_t介绍之前)看起来像这样,正如falsetru 在回答我之前的问题时所建议的那样:

s = struct.Struct('= B B H H 6h 6h 6h 6H')
fields = s.unpack(packet_data)
seq, _type, flags, upTimestamp = fields[:4]
x = fields[4:10]
y = fields[10:16]
z = fields[16:22]
lowTimestamp = fields[22:]
Run Code Online (Sandbox Code Playgroud)

这很好用。现在,我需要以某种方式能够读取标头(为此我需要解压结构),然后根据其类型正确解压结构。

我该怎么做呢?

小智 5

尝试ctypes。它更具可读性并且更容易打包/解包数据。

import ctypes

class mps_packet_header(ctypes.Structure):
    _fields_ = [
        ("seq", ctypes.c_uint8),
        ("type", ctypes.c_uint8),
        ("flags", ctypes.c_uint16),
        ("upTimestamp", ctypes.c_uint16)
    ]

class mps_acc_packet_t(ctypes.Structure):
    _fields_ = [
        ("header", mps_packet_header),
        ("x", ctypes.c_int16 * 6),
        ("y", ctypes.c_int16 * 6),
        ("z", ctypes.c_int16 * 6),
        ("lowTimestmp", ctypes.c_uint16 * 6)
    ]

class acam_hb_data_set(ctypes.Structure):
    _fields_ = [
        ("hb1", ctypes.c_int16),
        ("hb2", ctypes.c_int16),
    ]


class mps_dms_packet_t(ctypes.Structure):
    _fields_ = [
        ("header", mps_packet_header),
        ("temp", ctypes.c_uint16),
        ("lowTimestmp", ctypes.c_uint16 * 8),
        ("dms_data", acam_hb_data_set * 8)
    ]
Run Code Online (Sandbox Code Playgroud)