c结构到Python
我有以下c代码:c结构到Python,python,c,ctypes,Python,C,Ctypes,我有以下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;
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;
}
这是可行的,我从c结构中得到了所有的结果,但是如果我遍历res,会有数百个结果,而不是我所期望的一两个
有什么我做错了还是没做
谢谢。我自己没有这样做,所以我不想发布答案,但。。。python是否需要一个数据结构列表?是否需要在其中某个位置返回长度为1的值?另外,那些
self.lib
调用应该在哪里?根据缩进,它们不是类的一部分;该类只是定义BufferResults
结构。
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()