Here is piece of my code. I will explain the problem right after.
GIOStatus ret;
GError *err = NULL;
gchar *msg;
gsize *len;
ret = g_io_channel_read_chars (koncentrator, &msg, 13, &len, err);
printf ("Read %u bytes: %hx\n", len, msg);
My device sends chains of 13 bytes over the serial port. Unfortunately only first two bytes are read correctly. Maybe this is because only first two bytes can be interpreted as ASCII characters, this would be strange because I set the encoding as NULL with the function g_io_channel_set_encoding(koncentrator, NULL, NULL);
If I try to access the bytes after first two I get Segmentation fault
, which is also strange because variable ret
after reading contains 13
which means it had read 13 bytes.
I tryed to use g_io_channel_read_line
and g_io_channel_read_to_end
. With those two the program stucks at infinite loop (even when I verify the condition G_IO_STATUS_EOF
).
The last problem, and maybe the most annoying is that it doesn't work at first time, I need to open the serial port with the terminal, like cutecom
for example. Otherwise I can send the data but it doesn't detect any callback. I am sure of it because the device has diodes to indicate states of RX
and TX
.
I have no idea what is wrong, I will be very greatfull for giving me some tips or links to the suitable example. I feel like there is nothing more on google I could find. Thanks, cheers!
When you are dealing with serial ports you must configure them properly before starting to read. My guess is cutecom
is setting some vital serial parameter, making the port usable. In other words the code you posted is not the problem. The missing code is.
A good way to check this is by using stty
. This is just an example: use man stty
to have the (admittely long) list of options.
stty -F /dev/ttyS0 raw 9600 -parity