Here is piece of my code. I will explain the problem right after.
GError *err = NULL;
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!
以上就是GIOChannel sometimes it reads sometimes not的详细内容，更多请关注web前端其它相关文章！