相关疑难解决方法(0)

使用ctypes传递数组

我有一个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
}
Run Code Online (Sandbox Code Playgroud)

填充数组"*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
Run Code Online (Sandbox Code Playgroud)

代码执行但我在数组中得到错误的结果.当我尝试在CI中使用C函数时获得正确的结果:

size_t size=20;
int16_t* input;

read_FIFO_AI0(&input, size, &session, &status);
Run Code Online (Sandbox Code Playgroud)

填充数组的正确方法是什么,以便我可以在Python中访问数据?我没有依赖于使用指向已填充的数组的指针,我也可以在C函数中创建数组并将其作为返回Python发送,但我也没有开始工作.

c python arrays ctypes

6
推荐指数
1
解决办法
8379
查看次数

标签 统计

arrays ×1

c ×1

ctypes ×1

python ×1