c结构到Python

2024-04-28 21:41:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下c代码:

rx_data_t* readBuffer()
{
    rx_data_t buffer[512];

    results = doSomething()

    rx_data_t* newBuf = malloc(results.count());

    for(i=0;i<results.count();i++)
    {
        newBuf[i].receive_time = buffer[i].receive_time;
        newBuf[i].low = buffer[i].low;
        newBuf[i].high = buffer[i].high;
    }

    return newBuf;
}

我在Python(3.4)中使用ctypes来获得这个函数的结果

 class BufferResults(ctypes.Structure):
    """ creates a struct to match rx_data_t
    """

    _fields_ = [
        ('receive_time', ctypes.c_ulong),
        ('low', ctypes.c_ushort),
        ('high', ctypes.c_ushort)
    ]

self.lib.readBuffer.restype = ctypes.POINTER(BufferResults)

res = self.lib.readBuffer()

这是可行的,我从c结构中得到了所有的结果,但是如果我遍历res,会得到成百上千的结果,而不是我所期望的一两个结果

有什么我做错了还是没做

谢谢


Tags: selfdatatimebuffercountctypesrxresults