我有一个C函数
void read_FIFO_AI0(int16_t** input, size_t size, NiFpga_Session* session, NiFpga_Status* status)
{
*input = (int16_t*) malloc (size*sizeof(int16_t));
// function that populates the array *input
}
它填充数组“*input”。现在我想将该数组中的数据传递给 python 进行进一步处理。我尝试使用 ctypes 来做到这一点:
def read_FIFO_AI0(size,session,status):
_libfpga.read_FIFO_AI0.argtypes = [POINTER(ARRAY(c_int16, size)), c_int, POINTER(c_uint32), POINTER(c_int32)]
_libfpga.read_FIFO_AI0.restype = None
values = (c_int16*size)()
_libfpga.read_FIFO_AI0(byref(values),size,byref(session),byref(status))
return values
代码执行但我在数组中得到错误的结果。当我尝试从 C 内部使用 C 函数时,我得到了正确的结果:
size_t size=20;
int16_t* input;
read_FIFO_AI0(&input, size, &session, &status);
填充数组以便我可以在 Python 中访问数据的正确方法是什么?我并不局限于使用指向填充数组的指针,我也可以在 C 函数中创建数组并将其作为返回发送给 Python,但我也没有开始工作。