Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中解包复杂的嵌套C结构_Python_C_Struct_Nested_Unpack - Fatal编程技术网

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

在Python中解包复杂的嵌套C结构,python,c,struct,nested,unpack,Python,C,Struct,Nested,Unpack,这是我们的后续问题。我正在使用的结构已经变得更加复杂,我再一次不确定如何完全解包和处理它们 在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 head

这是我们的后续问题。我正在使用的结构已经变得更加复杂,我再一次不确定如何完全解包和处理它们

在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;
由此产生了两个挑战。首先,我接收的数据包(以二进制形式)可以是
mps\u acc\u packet\u t
mps\u dms\u packet\u t
——区分它们的唯一方法是读取
mps\u packet\u报头
中的
类型
字段。这意味着我需要在知道数据包的全部内容之前将其解包,我不知道如何将其彻底解包(如果我没有弄错的话),因为这两种数据包类型具有不同的
calcsize
(分别为54和56)。第二个挑战是打开一个
mps\u-dms\u数据包\u t
;从结构的定义可以看出,此数据包有一个数组,由8个
acam\u hb\u data\u set
实例组成,而这又是一个由两个
int16
值组成的结构。我不知道如何为它制定正确的格式字符串

我以前的代码(在引入mps\U dms\U packet\t之前)如下所示:

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


我该怎么做呢?

为什么不试试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)
    ]
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)
    ]