Python ctypes: calling a function with custom types in c

前端 未结 1 1239
悲哀的现实
悲哀的现实 2021-01-23 02:52

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 requ

1条回答
  •  臣服心动
    2021-01-23 03:33

    The line:

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

    should raise a TypeError, since you instantiate Calibration_AIN. 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. 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
    

    0 讨论(0)
提交回复
热议问题