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

标签 python

我以这种方式在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/

相关文章:

python - 如何根据条件合并两个不同大小的 Pandas DataFrame

python - 不使用kv语言如何在kivy中绘制对象?

python - 让 Pyinstaller 识别 Kivy Garden Matplotlib 模块的路径

Python:值 > 值在显然不是时返回 true

python - 如何让Python中的函数超时

python - 在管道中的多个位置使用相同的规则(带或不带通配符)

python - Django 在 stdimage 处理图像之前修改图像

python - 如何打开并迭代 CSV 文件列表 - Python

python - 如何检查sklearn模型是分类器还是回归器

python - 调整pyplot中颜色条的颜色