Pointers and ctypes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rubbishemail@web.de

    #1

    Pointers and ctypes

    Hello,
    i've got a problem with pointers in the following function which i want
    to use:

    I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value)

    The function is supposed to read out the status of a digital port of
    analog digital interface card.
    I got this function from Dask.h which came with the card. The relevant
    lines concerning this function are the following:

    typedef short I16;
    typedef unsigned short U16;
    typedef unsigned long U32;

    I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value)

    I tried to implement this function into python:
    # I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value);
    ReadOPort = dask.DO_ReadPor t
    ReadOPort.argty pes = [c_ushort, c_ushort, c_ulong]
    ReadOPort.resty pe = c_short

    I can't handle the pointer "Value" which should be an unsigned long
    pointer. I'd be very happy, if u could give me a hint how to implement
    this pointer into python.

    Thanks a lot

    Carlo and Pierre

  • F. Petitjean

    #2
    Re: Pointers and ctypes

    Le 29 Aug 2005 06:19:17 -0700, rubbishemail@we b.de a écrit :[color=blue]
    > Hello,
    > i've got a problem with pointers in the following function which i want
    > to use:
    >
    > I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value)
    >
    > The function is supposed to read out the status of a digital port of
    > analog digital interface card.
    > I got this function from Dask.h which came with the card. The relevant
    > lines concerning this function are the following:
    >
    > typedef short I16;
    > typedef unsigned short U16;
    > typedef unsigned long U32;
    >
    > I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value)
    >
    > I tried to implement this function into python:
    > # I16 __stdcall DO_ReadPort (U16 CardNumber, U16 Port, U32 *Value);
    > ReadOPort = dask.DO_ReadPor t
    > ReadOPort.argty pes = [c_ushort, c_ushort, c_ulong]
    > ReadOPort.resty pe = c_short
    >
    > I can't handle the pointer "Value" which should be an unsigned long
    > pointer. I'd be very happy, if u could give me a hint how to implement
    > this pointer into python.[/color]

    You can use the ctypes.byref() function (as it is in an argulent list):

    ReadOPort.argty pes = (c_ushort, c_ushort, ctypes.POINTER( c_ulong) )
    ReadOPort.resty pe = c_short
    status = c_ulong() # status value to be read
    number = c_ushort(1) # CardNumber = 1
    port = c_ushort(11)
    rc = ReadOPort(numbe r, port, ctypes.byref(st atus))
    print rc, ststus[color=blue]
    >
    > Thanks a lot
    >
    > Carlo and Pierre
    >[/color]

    Comment

    • rubbishemail@web.de

      #3
      Re: Pointers and ctypes

      thanks a bunch, i just got the answer myself. next time i think about
      it a little longer.
      thanks again
      carlo

      Comment

      • Mike C. Fletcher

        #4
        Re: Pointers and ctypes

        F. Petitjean wrote:
        [color=blue]
        >Le 29 Aug 2005 06:19:17 -0700, rubbishemail@we b.de a écrit :
        >
        >[/color]
        ....
        [color=blue]
        >You can use the ctypes.byref() function (as it is in an argulent list):
        >
        > ReadOPort.argty pes = (c_ushort, c_ushort, ctypes.POINTER( c_ulong) )
        > ReadOPort.resty pe = c_short
        > status = c_ulong() # status value to be read
        > number = c_ushort(1) # CardNumber = 1
        > port = c_ushort(11)
        > rc = ReadOPort(numbe r, port, ctypes.byref(st atus))
        > print rc, ststus
        >
        >[/color]
        Just as an FYI, Thomas has recently added code which does the byref
        automatically. The version of ctypes in CVS HEAD will allow you to pass
        in a c_ulong where the argtype is ctypes.POINTER( c_ulong).

        Have fun,
        Mike

        --
        _______________ _______________ _______________ ___
        Mike C. Fletcher
        Designer, VR Plumber, Coder



        Comment

        Working...