简体   繁体   English

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

[英]Python ctypes: calling a function with custom types in c

I'm trying to wrap pre-existing c code for use in Python in Linux. 我正在尝试包装先前存在的C代码以在Linux中的Python中使用。 I have little experience with c, and I'm currently approaching this problem using ctypes. 我对c的经验很少,目前正在使用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. 我的C函数需要带有自定义类型条目的2d数组,而且我不知道如何在python中重新创建此数组以将其传递给c函数。

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;
}

The header file has defined 头文件已定义

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

where NCHAN_18081 and NGAINS_1808 are constants and udev is an integer. 其中NCHAN_18081和NGAINS_1808是常量,而udev是整数。 I've followed an older question regarding multidimensional arrays and attempted to make a structure like the one in the c code. 我遵循了有关多维数组的一个较旧的问题 ,并试图在c代码中创建一种类似于多维数组的结构。

_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)

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

How can I call this function from python without modifying the c code? 如何在不修改C代码的情况下从python调用此函数? 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. 任何帮助将不胜感激,也请让我知道我是否应该学习c并尝试用c或Cython编写程序。 Ctypes may not be the best way of doing this. Ctypes可能不是最好的方法。

The line: 该行:

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

should raise a TypeError , since you instantiate Calibration_AIN . 应该实例化 Calibration_AIN ,所以应该引发TypeError I believe that what you meant was: 我相信您的意思是:

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

But even so, you don't need the AINarray wrapper. 但是即使如此,您也不需要AINarray包装器。 According to [Python]: Arrays , you could do something like: 根据[Python]:Arrays ,您可以执行以下操作:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM