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:
- Load the .so file by calling ctypes.cdll.loadlibrary().
- Initialize the serial communication module.
- 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;
}