Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Two Dimensional Array + ctypes

120 views
Skip to first unread message

Sparky

unread,
Aug 5, 2009, 7:12:09 PM8/5/09
to
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

Gabriel Genellina

unread,
Aug 6, 2009, 1:19:24 AM8/6/09
to pytho...@python.org
En Wed, 05 Aug 2009 20:12:09 -0300, Sparky <Samns...@gmail.com> 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.

--
Gabriel Genellina

Sparky

unread,
Aug 6, 2009, 12:40:23 PM8/6/09
to
On Aug 5, 11:19 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

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

Thanks,
Sam

0 new messages