emad
emad

Reputation: 3099

defining ctype equivalent structure in python

I have a struct within a struct in the define.h file in this way:

typedef struct
{
 byte iVersion;
 long iMTPL;
 byte iMPR;
 byte iTempCompIndex;
 byte iTempCompRemainder;

} Message_Tx_Datapath;

typedef struct
{
 byte               iNumTxPaths;
 Message_Tx_Datapath datapath[NUM_TX_PATHS];
 } Message_Tx;

And I want to define an equivalent structure using ctypes in python for this so that when I use the dll I can pass this structure to fetch data in python.

How can I define this in python. I know how to define a single level struct but this is a struct within a struct and I'm not sure how I can define that. Please help.

Here's how I've started my code:

class Message_Tx(ctypes.Structure):
   _fields_ = [("iNumTxPaths",c_byte),("datapath",????)]

Upvotes: 1

Views: 482

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208405

That would look something like this:

import ctypes

NUM_TX_PATHS = 4    # replace with whatever the actual value is

class Message_Tx_Datapath(ctypes.Structure):
    _fields_ = [('iVersion', ctypes.c_byte),
                ('iMTPL', ctypes.c_long),
                ('iMPR', ctypes.c_byte),
                ('iTempCompIndex', ctypes.c_byte),
                ('iTempCompRemainder', ctypes.c_byte)]

class Message_Tx(ctypes.Structure):
    _fields_ = [('iNumTxPaths', ctypes.c_byte),
                ('datapath', Message_Tx_Datapath*NUM_TX_PATHS)]

See the ctypes documentation on arrays.

Upvotes: 1

Related Questions