将指针传递给结构中的结构中的ndarray(ctypes)

时间:2017-06-06 10:09:50

标签: python python-2.7 numpy struct ctypes

我有一个ctypes函数,它有一个struct,包含另一个struct,作为它的一个参数。 我创建了结构如下;

class ChannelDataBuffers(Structure):
_pack_ = PACK
_fields_ = [
            ("bufferMax",  c_void_p),
            ("bufferMin", c_void_p),
            ("dataType", c_int32)
            ]


class DataBuffers(Structure):
_pack_ = PACK
_fields_ =  [
                ("channel",               c_int32),
                ("waveform",              c_uint64),
                ("downSampleRatioMode",   c_int32),
                ("read",                  c_int32),
                ("buffers",               ChannelDataBuffers),
                ("nbuffers",              c_uint32),
                ("nDistributionPoints",   c_uint32)
            ]
def __init__(self, channel, waveform, ratiomode, read, ref_buffermax, ref_buffermin, datatype, nbuffers, nDistributionPoints):

    self.channel = c_int32(channel)
    self.waveform = c_uint64(waveform)
    self.downSampleRatioMode = c_int32(ratiomode)
    self.read = c_int32(read)
    self.buffers.bufferMax = (ref_buffermax)
    self.buffers.bufferMin = (ref_buffermin)
    self.buffers.datatype = c_int32(datatype)
    self.nbuffers = c_uint32(nbuffers)
    self.nDistributionPoints = c_uint32(nDistributionPoints)

bufferMax和bufferMin都是numpy数组,使用.ctypes参数传递 - 即定义如下:

bufMax = self._buffers[bufIndex].data.ctypes

这会导致对象被传递到

的DataBuffers init函数中
numpy.core._internal._ctypes object at 0x08D99D90

这似乎是明智的。 然而,我接着是

的例外
 cannot be converted to pointer <type 'exceptions.TypeError'>

self.buffers.bufferMax = (ref_buffermax) 

任何人都知道我能做些什么来解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可能需要使用ctypes.cast

self.buffers.bufferMax = ctypes.cast(ref_buffermax, c_void_p)