1

I have a built a simple serial communication shared library which I am trying to use in python using the ctypes library. The steps that I am following are:

  1. Load the .so file by calling ctypes.cdll.loadlibrary().
  2. Initialize the serial communication module.
  3. Send 0xFF as sync bytes.

When I do the above steps I am not getting only junk data at the other end. Funnily when I use the .so file in C and do the same operation it works absolutely fine. So My question is does the ctypes module manipulate the loaded library in any way? I am quite new to using C in Python and I am drawing blank here. Any suggestions would be very helpful.

#!/usr/bin/env python3
import ctypes
test_lib = ctypes.cdll.LoadLibrary("./demo_static.so")
string2 = "/dev/ttyS2"
# create byte objects from the strings
UART_RIGHT = string2.encode('utf-8')
baud = 500000
test_lib.serial_com_init(0, UART_RIGHT, baud)

The (relevant) C code:

int serial_com_init(char *left, char *right, int baudrate) {
    int fd_l, fd_r;
    uart_t uart_left, uart_right;
    uint8_t flags_l, flags_r;
    if (left) {
        fd_l = uart_init_linux(left, baudrate);
        uart_left->fd = fd_l;
    }
    if (right) {
        fd_r = uart_init_linux(right, baudrate);
        uart_right->fd = fd_r;
    }
    serial_com_init_cr(uart_left, uart_right, flags_l, flags_r); 
    serial_com_hello_init();
    return 0;
}
CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • Could you post your code? – 101 Jun 25 '18 at 09:35
  • `#!/usr/bin/env python3 import ctypes test_lib = ctypes.cdll.LoadLibrary("./demo_static.so") string2 = "/dev/ttyS2"" # create byte objects from the strings UART_RIGHT = string2.encode('utf-8') baud = 500000 test_lib.serial_com_init(0,UART_RIGHT,baud)` – sandeepmanjunath22 Jun 25 '18 at 10:01
  • There's no code that actually sends data. What lib are you using? Does `serial_com_init` return something? Could you also post the (*C*) working code? – CristiFati Jun 25 '18 at 12:03
  • The serial_com_init function initializes the the serial com with the correct baud and then sends 0xFF as a hello ping which if sent correctly i can see it on the console. But it is sending junk value for some reason.`int serial_com_init(char* left,char* right,int baudrate){ int fd_l,fd_r;uart_t uart_left,uart_right;uint8_t flags_l,flags_r; if (left){ fd_l=uart_init_linux(left, baudrate); uart_left->fd = fd_l; } if (right){ fd_r=uart_init_linux(right, baudrate); uart_right->fd=fd_r; } serial_com_init_cr(uart_left,uart_right,flags_l,flags_r); serial_com_hello_init(); return 0;}` – sandeepmanjunath22 Jun 25 '18 at 13:26
  • @sandeepmanjunath22 Don't post code in comments. Edit your question with the requested information and format it correctly. – Mark Tolonen Jun 26 '18 at 00:50
  • Sure.. I will do that..Thanks – sandeepmanjunath22 Jun 26 '18 at 14:11
  • Does this answer your question? [C function called from Python via ctypes returns incorrect value](https://stackoverflow.com/questions/58610333/c-function-called-from-python-via-ctypes-returns-incorrect-value) – CristiFati Jan 06 '23 at 22:38

1 Answers1

0

According to [Python.Docs]: ctypes - Specifying the required argument types (function prototypes), for a function like:

int serial_com_init(char *left, char *right, int baudrate);

you need to specify argument types (before the function call). This makes a crucial difference on 064bit Python. Check [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer) for more details:

test_lib.serial_com_init.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int)
test_lib.serial_com_init.restype = ctypes.c_int
init_result = test_lib.serial_com_init(None, UART_RIGHT, baud)

Notes:

  • This is one of the possible causes

  • Always check functions return codes (well, here it will always be 0, and this might hide errors in the C code)

  • You can use bytes literals directly: UART_RIGHT = b"/dev/ttyS2"

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • Thanks for the Tip. Although i have a different problem with my serial com module now, this worked. It initialized the serial Comm..So i have something to work with now.. – sandeepmanjunath22 Jun 26 '18 at 14:12
  • Then mark this answer as a solution to the question. If having further problems, feel free to post other questions (make sure to check the community first, to avoid asking an already existing question). – CristiFati Jun 28 '18 at 06:09