0

I have a large piece of C code on top of which i am writing Python Wrappers. I have segregated the code compiled the shared library successfully and able to load it as well from the Python code.

Now I wanted to call a method which takes the pointer to a structure. The code works fine if I dont call the method , which means I am probably correct in converting the c data structure to equivalent ctypes, but when I am calling the method with the pointer i am getting error :

Below is the c data structure :

typedef struct
{
    pwm_properties  pwm_prop;
    uint8_t             bytecount;
    uint32_t            portnum;
    uint8_t             *return_value;
    uint16_t        channels;
    uint8_t                 temporrary_buffer[1024];
} pwm_configuration;


typedef struct
{   
    uint16_t    delta_sigma;
    uint8_t     duty_cycle;
    uint32_t    sv_m;
    uint8_t     duty_cycle;
    int     id;

} pwm_properties;

Below is the python code ;

class pwm_properties(Structure):
    _fields_ = [
        ("delta_sigma",c_uint16),
        ("duty_cycle", c_uint8),
        ("sv_m", c_uint32),
        ("duty_cycle", c_uint8),
        ("id", c_int)
        ]
        
        
class pwm_configuration(Structure):
    _fields_ = [
        ("pwm_prop",pwm_properties),
        ("bytecount", c_uint8),
        ("portnum", c_uint32),
        ("return_value", POINTER(c_uint8)),
        ("channels", c_uint16),
        ("temporrary_buffer", c_uint8*1024)
    ]

Use #1 :

    l = pwm_properties()
    p = byref(l)
    status = add_lib.__init_(p)


    ***Segmentation fault (core dumped)***

Anyone know how can I achieve calling below c method : init(&pwm_configuration.pwm_properties)

in python.

EDIT : It worked as @user673679 has suggested.

Raulp
  • 7,758
  • 20
  • 93
  • 155
  • pwm_configuration is a class ("type object" as the error says). You need to create an actual instance of that type to use it. – user673679 Jan 28 '22 at 08:28
  • @user673679 doesnt make any difference , i did , l = pwm_configuration , p = l.pwm_prop – Raulp Jan 28 '22 at 09:20
  • 1
    `l = pwm_configuration()` the brackets are important. – user673679 Jan 28 '22 at 10:36
  • Might want to take a look at: [\[SO\]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)](https://stackoverflow.com/questions/58610333/c-function-called-from-python-via-ctypes-returns-incorrect-value/58611011#58611011). Also why did you modify the question to include the answer? That would make the question invalid. Post an answer instead. – CristiFati Feb 02 '22 at 06:36

0 Answers0