Pointers and ctypes

Discussion in 'Python' started by rubbishemail@web.de, Aug 29, 2005.

  1. Guest

    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_ReadPort
    ReadOPort.argtypes = [c_ushort, c_ushort, c_ulong]
    ReadOPort.restype = 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
    , Aug 29, 2005
    #1
    1. Advertising

  2. F. Petitjean Guest

    Le 29 Aug 2005 06:19:17 -0700, a écrit :
    > 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_ReadPort
    > ReadOPort.argtypes = [c_ushort, c_ushort, c_ulong]
    > ReadOPort.restype = 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.


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

    ReadOPort.argtypes = (c_ushort, c_ushort, ctypes.POINTER(c_ulong) )
    ReadOPort.restype = c_short
    status = c_ulong() # status value to be read
    number = c_ushort(1) # CardNumber = 1
    port = c_ushort(11)
    rc = ReadOPort(number, port, ctypes.byref(status))
    print rc, ststus
    >
    > Thanks a lot
    >
    > Carlo and Pierre
    >
    F. Petitjean, Aug 29, 2005
    #2
    1. Advertising

  3. Guest

    thanks a bunch, i just got the answer myself. next time i think about
    it a little longer.
    thanks again
    carlo
    , Aug 29, 2005
    #3
  4. F. Petitjean wrote:

    >Le 29 Aug 2005 06:19:17 -0700, a écrit :
    >
    >

    ....

    >You can use the ctypes.byref() function (as it is in an argulent list):
    >
    > ReadOPort.argtypes = (c_ushort, c_ushort, ctypes.POINTER(c_ulong) )
    > ReadOPort.restype = c_short
    > status = c_ulong() # status value to be read
    > number = c_ushort(1) # CardNumber = 1
    > port = c_ushort(11)
    > rc = ReadOPort(number, port, ctypes.byref(status))
    > print rc, ststus
    >
    >

    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
    http://www.vrplumber.com
    http://blog.vrplumber.com
    Mike C. Fletcher, Aug 29, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henk Punt
    Replies:
    0
    Views:
    394
    Henk Punt
    Jul 23, 2004
  2. Metalone

    ctypes pointers and SendMessage

    Metalone, May 31, 2006, in forum: Python
    Replies:
    2
    Views:
    428
    Metalone
    Jun 1, 2006
  3. Richard Jones

    ctypes, arrays and pointers

    Richard Jones, Oct 4, 2006, in forum: Python
    Replies:
    2
    Views:
    2,577
    Richard Jones
    Oct 7, 2006
  4. Replies:
    0
    Views:
    502
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    673
Loading...

Share This Page