Glib and Serial Port Help

So I want to write a small program to read and write data to a serial port (115200 baud, 8N1) using GIOChannel. No matter what I try, I can't seem to get input callbacks from the channel. :( Does anyone have any small working examples of using a GIOChannel to drive a serial port I can look at?

NP: Mungo's Hi-Fi at Exodus

14:15 Sunday, 09 Dec 2007 [#] [computers] (13 comments)

Posted by Anders at Sun Dec 9 14:46:52 2007:
This works for me...

fd = open("/dev/ttyS0", O_RDWR|O_NOCTTY|O_NONBLOCK);

(all the usual tcsets...)

io = g_io_channel_unix_new(fd);

g_io_add_watch(io, G_IO_IN, has_data, ptr);
Posted by Ross at Sun Dec 9 15:08:58 2007:
Anders, can you add the usual tcsets, I have a load but may be missing some.

  tcgetattr (fd, &tio);
  tio.c_cflag = CREAD | CLOCAL | B115200 | CS8;
  tio.c_iflag = IGNPAR | IGNBRK;
  tio.c_oflag = 0;
  tio.c_lflag = 0;
  tio.c_cc[VTIME] = 0;
  tio.c_cc[VMIN]  = 1;
  tcsetattr (fd, TCSANOW, &tio);
  tcflush (fd, TCIFLUSH);
  tcflush (fd, TCOFLUSH);
Posted by Markku Vire at Sun Dec 9 15:22:21 2007:
GIOChannel assumes by default that the data is UTF8 -formatted text. If that's not the case for you, perhaps setting

  g_io_channel_set_encoding(channel, NULL);
  g_io_channel_set_buffered(channel, FALSE);

works for you?
Posted by Ross at Sun Dec 9 15:31:36 2007:
Yeah, got that.
Posted by loic at Sun Dec 9 17:51:17 2007:
Do you flush the i/o channel using g_io_channel_flush after the write?
Posted by Ross at Sun Dec 9 18:52:18 2007:
Yes.
Posted by Luca Cavalli at Mon Dec 10 23:50:34 2007:
Ross, did you solve the problem? Otherwise I have some working code at work and tomorrow I should be able to extract a small example from it.
BTW, is your cable the correct one (null-modem/direct)?
Posted by Ross at Tue Dec 11 09:47:39 2007:
No, it's not working yet.  I think I need to do something weird, a plain write() and then read() to wait for the response of the write doesn't even work.  The serial device is on a USB bus (/dev/ttyACM0), so the cable isn't a problem.
Posted by Luca Cavalli at Tue Dec 11 12:36:25 2007:
Hi Ross, this code is take from a working project (I only removed all error checks). I used a real serial connection (I can't remember if it was a RS-232 or 488, but it shouldn't be important). The termios flags aren't mine, but come from a previous project, so maybe they can be compacted a bit.
Hope it helps in some way.
Ciao.

acs_fd = g_open ("/dev/ttyS0", O_NOCTTY | O_RDONLY);
tcgetattr (acs_fd, &acs_termios);
cfsetispeed (&acs_termios, B19200);
cfsetospeed (&acs_termios, B19200);
acs_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY);
acs_termios.c_oflag &= ~OPOST;
acs_termios.c_lflag &= ~(ECHO | ECHONL | ECHOE | ICANON | ISIG | IEXTEN);
acs_termios.c_cflag &= ~(CRTSCTS | CSTOPB | PARENB | INPCK | ISTRIP | CSIZE);
acs_termios.c_cflag |= CREAD | CLOCAL | CS8;
acs_termios.c_cc[VMIN] = 1;
acs_termios.c_cc[VTIME] = 0;
tcflush (acs_fd, TCIOFLUSH);
tcsetattr (acs_fd, TCSANOW, &acs_termios);
acs_io = g_io_channel_unix_new (acs_fd);
g_io_channel_set_encoding (acs_io, NULL, NULL);
g_io_add_watch (acs_io, G_IO_IN, acs_scoe_process, (gpointer) &acs_scoe_pkt);
Posted by Coleman at Tue Jan 15 03:47:12 2008:
Hi Ross,

Did you resolve this?  I'm having a similar problem.

My configuration is:

newtio.c_cflag = baud | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
//all others are zero. 

If I write "AT\n" to /dev/ttyACM0 and then immediately read, read() always returns 0.
Posted by Ross at Tue Jan 15 08:24:31 2008:
Yes, my hardware was SO BAD that it can only handle single byte writes.  I have to change write("foo") to write("f"); write("o"); write("o"); and now it works fine.
Posted by Coleman at Mon Jan 28 16:30:20 2008:
This turned out to be a bug in my read code.  I wasn't passing the correct byte count as the last parameter to read.  It was a copy and paste (er, refactoring) error when I created my "modemRx" function. 

Was the hardware problem your USB port or the modem?
Posted by Ross at Mon Jan 28 16:50:00 2008:
It was in the device itself, it's not a modem but a hardware sensor with a very basic serial controller bolted on.

Name:


E-mail:


URL:


Add 8 and 8 (required):


Comment: