gpt4 book ai didi

python - 在python中定义ctype等效结构

转载 作者:太空宇宙 更新时间:2023-11-03 19:18:21 25 4
gpt4 key购买 nike

我以这种方式在define.h文件的结构中包含一个结构:

typedef struct
{
byte iVersion;
long iMTPL;
byte iMPR;
byte iTempCompIndex;
byte iTempCompRemainder;

} Message_Tx_Datapath;

typedef struct
{
byte iNumTxPaths;
Message_Tx_Datapath datapath[NUM_TX_PATHS];
} Message_Tx;

我想在 python 中使用 ctypes 定义一个等效的结构,这样当我使用 dll 时,我可以传递这个结构来在 python 中获取数据。

如何在 python 中定义它。我知道如何定义单级结构,但这是结构中的结构,我不确定如何定义它。请帮忙。

以下是我开始编写代码的方式:

class Message_Tx(ctypes.Structure):
_fields_ = [("iNumTxPaths",c_byte),("datapath",????)]

最佳答案

看起来像这样:

import ctypes

NUM_TX_PATHS = 4 # replace with whatever the actual value is

class Message_Tx_Datapath(ctypes.Structure):
_fields_ = [('iVersion', ctypes.c_byte),
('iMTPL', ctypes.c_long),
('iMPR', ctypes.c_byte),
('iTempCompIndex', ctypes.c_byte),
('iTempCompRemainder', ctypes.c_byte)]

class Message_Tx(ctypes.Structure):
_fields_ = [('iNumTxPaths', ctypes.c_byte),
('datapath', Message_Tx_Datapath*NUM_TX_PATHS)]

请参阅ctypes documentation on arrays .

关于python - 在python中定义ctype等效结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10624091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com