0

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!

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Marek
  • 1,413
  • 2
  • 20
  • 36

2 Answers2

1

This is the signature of g_io_channel_read_chars as per the documentation:

GIOStatus           g_io_channel_read_chars             (GIOChannel *channel,
                                                         gchar *buf,
                                                         gsize count,
                                                         gsize *bytes_read,
                                                         GError **error);

You are passing &msg as the argument buf, thus you are passing a value of type gchar** where the function expects gchar*. You should pass msg instead. You should also first allocate the memory as it is the caller's responsibility and not the function's.

Adrian Panasiuk
  • 7,249
  • 5
  • 33
  • 54
  • Thank you Adrian. I added `malloc` and changed `&msg` for `msg` and this solved the problem with `Segmentation fault`. But I still have the problem with comunication, I need to open the device with `cutecom` terminal and only then my C program will be able to receive messages from the device. I forgot to say that I add the module to the kernel so I am sure the device exist in the system, it is note added by the `cutecom` terminal. – Marek May 29 '13 at 11:04
1

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
fetasail
  • 51
  • 3
  • Thank you very much. This works. I would also like to ask how to make it pernament, should I add this to the boot rc script so that he would execute this after each boot? – Marek May 31 '13 at 10:06
  • @Marek Yes, e.g. you can use `/etc/rc.local`. A better approach would be to spawn the `stty` call at beginning of your program, via `g_spawn_command_line_sync()` for instance. The best solution would be to directly call the `termios` APIs to get the same configuration but this would involve much more work. – fetasail Jun 04 '13 at 09:49