Two Dimensional Array + ctypes

S

Sparky

Hello! I am trying to call this method:

long _stdcall AIBurst(long *idnum,
long demo,
long stateIOin,
long updateIO,
long ledOn,
long numChannels,
long *channels,
long *gains,
float *scanRate,
long disableCal,
long triggerIO,
long triggerState,
long numScans,
long timeout,
float (*voltages)[4],
long *stateIOout,
long *overVoltage,
long transferMode);

I am having some problems with that float (*voltages)[4]. Here is
what I tried:

def aiBurst(self, channels, scanRate, numScans, idNum=None, demo=0,
stateIOin=[0, 0, 0, 0], updateIO=0, ledOn=0, gains=[0, 0, 0, 0],
disableCal=0, triggerIO=0, triggerState=0, timeout=1, transferMode=0):

if idNum is None:
idNum = self.id

idNum = ctypes.c_long(idNum)

numChannels = len(channels)

stateIOArray = listToCArray(stateIOin, ctypes.c_long)
channelsArray = listToCArray(channels, ctypes.c_long)
gainsArray = listToCArray(gains, ctypes.c_long)
scanRate = ctypes.c_float(scanRate)
pointerArray = (ctypes.c_void_p * 4)
voltages = pointerArray(ctypes.cast(ctypes.pointer
((ctypes.c_long * 4096)()), ctypes.c_void_p), ctypes.cast
(ctypes.pointer((ctypes.c_long * 4096)()), ctypes.c_void_p),
ctypes.cast(ctypes.pointer((ctypes.c_long * 4096)()),
ctypes.c_void_p), ctypes.cast(ctypes.pointer((ctypes.c_long * 4096)
()), ctypes.c_void_p))
stateIOout = (ctypes.c_long * 4096)()
overVoltage = ctypes.c_long(999)

staticLib.AIBurst(ctypes.byref(idNum), demo, stateIOArray,
updateIO, ledOn, numChannels, ctypes.byref(channelsArray), ctypes.byref
(gainsArray), ctypes.byref(scanRate), disableCal, triggerIO,
triggerState, numScans, timeout, ctypes.byref(voltages), ctypes.byref
(stateIOout), ctypes.byref(overVoltage), transferMode)

The program runs but the values that come back in the array are not
right. However, I am not sure if I am passing the array or if I am
just not reading them well afterwards. What is the best way to put
this in and what is the best way to cast it and read it back?

Thanks,
Sam
 
G

Gabriel Genellina

Hello! I am trying to call this method:

long _stdcall AIBurst(long *idnum, [...]
long timeout,
float (*voltages)[4],
long *stateIOout,
long *overVoltage,
long transferMode);

I am having some problems with that float (*voltages)[4].
pointerArray = (ctypes.c_void_p * 4)
voltages = pointerArray(ctypes.cast(ctypes.pointer
((ctypes.c_long * 4096)()), ctypes.c_void_p), ctypes.cast
(ctypes.pointer((ctypes.c_long * 4096)()), ctypes.c_void_p),
ctypes.cast(ctypes.pointer((ctypes.c_long * 4096)()),
ctypes.c_void_p), ctypes.cast(ctypes.pointer((ctypes.c_long * 4096)

Why c_long and not c_float?
Anyway, this way looks much more clear to me (and doesn't require a cast):

arr4096_type = ctypes.c_float * 4096
voltages_type = arr4096_type * 4
voltages = voltages_type()
The program runs but the values that come back in the array are not
right.

Thay might be due to the long/float confusion.
 
S

Sparky

En Wed, 05 Aug 2009 20:12:09 -0300, Sparky <[email protected]> escribió:




Hello! I am trying to call this method:
long _stdcall AIBurst(long *idnum, [...]
                    long timeout,
                    float (*voltages)[4],
                    long *stateIOout,
                    long *overVoltage,
                    long transferMode);
I am having some problems with that  float (*voltages)[4].
        pointerArray = (ctypes.c_void_p * 4)
        voltages = pointerArray(ctypes.cast(ctypes.pointer
((ctypes.c_long * 4096)()), ctypes.c_void_p), ctypes.cast
(ctypes.pointer((ctypes.c_long * 4096)()), ctypes.c_void_p),
ctypes.cast(ctypes.pointer((ctypes.c_long * 4096)()),
ctypes.c_void_p), ctypes.cast(ctypes.pointer((ctypes.c_long * 4096)

Why c_long and not c_float?
Anyway, this way looks much more clear to me (and doesn't require a cast):

arr4096_type = ctypes.c_float * 4096
voltages_type = arr4096_type * 4
voltages = voltages_type()
The program runs but the values that come back in the array are not
right.

Thay might be due to the long/float confusion.

Brilliant! Your code is much cleaner and the problem must have been
float vs long.

Thanks,
Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top