在C中使用自定义类型调用函数

在C中使用自定义类型调用函数

本文介绍了Python CTYPE:在C中使用自定义类型调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试包装预先存在的c代码,以便在Linux中的Python中使用.我对c的经验很少,目前正在使用ctypes处理此问题.我的C函数需要一个带有自定义类型条目的2d数组,而且我不知道如何在python中重新创建该数组以将其传递给c函数.

I'm trying to wrap pre-existing c code for use in Python in Linux. I have little experience with c, and I'm currently approaching this problem using ctypes. My C function requires a 2d array with custom type entries and I don't know how to recreate this in python to pass it to the c function.

这是我尝试调用的函数:

Here is the function I'm attempting to call:

void usbBuildGainTableAI_USB1808(libusb_device_handle *udev, Calibration_AIN table[NCHAN_1808][NGAINS_1808])
{

  int i, j;
  uint16_t address = 0x7000;  // base address of ADC calibration coefficients.

  for (i = 0; i < NCHAN_1808; i++) {
    for (j = 0; j < NGAINS_1808; j++) {
      usbMemAddressW_USB1808(udev, address);
      usbMemoryR_USB1808(udev, (uint8_t *) &table[i][j].slope, sizeof(float));
      address += 4;
      usbMemAddressW_USB1808(udev, address);
      usbMemoryR_USB1808(udev, (uint8_t *) &table[i][j].offset, sizeof(float));
      address += 4;
    }
  }
  return;
}

头文件已定义

typedef struct Calibration_AIN_t {
  float slope;
  float offset;
} Calibration_AIN;

其中NCHAN_18081和NGAINS_1808是常量,而udev是整数.我遵循了有关多维数组的较早的问题,并尝试制作类似C代码中的一个.

where NCHAN_18081 and NGAINS_1808 are constants and udev is an integer. I've followed an older question regarding multidimensional arrays and attempted to make a structure like the one in the c code.

_1808 = CDLL(os.path.abspath("lib1808.so"))

NCHAN_1808 = 8  # max number of A/D channels in the device
NGAINS_1808 = 4  # max number of gain levels

class Calibration_AIN(Structure):
    _fields_ = [("slope", c_float), ("offset", c_float)]

class AINarray(Structure):
    _fields_ = [("array", (Calibration_AIN() * NCHAN_1808) * NGAINS_1808)]

table_AIN = AINarray()

_1808.usbBuildGainTableAI_USB1808(udev, table_AIN)

但是这有两个问题:自定义类型Calibration_AIN不能像int或float那样用运算符*填充到数组中,并且我无法将自定义类型传递给c.我也尝试过使用Python中的列表列表来制作数组,但是我无法将其转换为可通过ctypes传递给c的任何有用信息.

But there's a couple problems with this: the custom type Calibration_AIN can't be populated in an array with the operator * like an int or float can, and I can't pass a custom type through to c. I've also tried making the array with a list of lists in Python, but I can't convert that to anything useful to c passable through ctypes.

如何在不修改C代码的情况下从python调用此函数?任何帮助将不胜感激,也请让我知道我是否应该学习c并尝试用c或Cython编写程序. Ctypes可能不是最好的方法.

How can I call this function from python without modifying the c code? Any help would be much appreciated, and also let me know if I should just learn c and try to write my program in c or Cython. Ctypes may not be the best way of doing this.

推荐答案

该行:

_fields_ = [("array", (Calibration_AIN() * NCHAN_1808) * NGAINS_1808)]

应该引发 TypeError ,因为您实例化 Calibration_AIN .我相信您的意思是:

should raise a TypeError, since you instantiate Calibration_AIN. I believe that what you meant was:

_fields_ = [("array", (Calibration_AIN * NCHAN_1808) * NGAINS_1808)]

但是即使如此,您也不需要 AINarray 包装器.根据 [Python]:数组,您可以执行以下操作:

But even so, you don't need the AINarray wrapper. According to [Python]: Arrays, you could do something like:

Calibration_AIN_Table = (Calibration_AIN * NCHAN_1808) * NGAINS_1808

然后,为了初始化实例,请执行以下操作:

and then, in order to initialize an instance do smth like:

>>> cat = Calibration_AIN_Table()
>>> for i in range(NGAINS_1808):
...     for j in range(NCHAN_1808):
...         cat[i][j].slope = i * j
...         cat[i][j].offset = i * i * j
...
>>> cat[2][3].slope
6.0

这篇关于Python CTYPE:在C中使用自定义类型调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:17