Unable to get ctypes C types parsed correctly

Viewed 25

I need to run this in ctypes however cannot get it to work.

int function_open(const char *port_name, int baud_rate);

I have tried:

from ctypes import *
import ctypes
import time

load=cdll.LoadLibrary(r"[absolute path redacted]")
original_string="/dev/ttyS4"
b = ctypes.c_char_p(str.encode(original_string))
load.function_open(b, ctypes.c_int(9600))

It returns the exception as defined in C but I think the wrong argument types are being passed or the method is incorrect. Please can someone take a look? Thanks in advance. I am loading a .so shared library. I will send 5$ to the a solution that works via paypal / venmo.

1 Answers

I tested with main.c:

int function_open(const char *port_name, int baud_rate) {
    printf("port_name = %s and baud_rate = %d\n", port_name, baud_rate);
    return 1;
}

I compiled it like the following.

gcc -c main.c -o main.o
gcc main.o -shared -o main.so

Your Python code snippet worked just fine. So the problem must be something else besides argument types.

Related