0

I have been having some problem with the serial communication in ubuntu. I am writing C++ code to connect to my zigbee node but the problem is not the zigbee configuration. Since GTKTERM (an ordinary serial terminal for ubuntu) reads in the data correct.

Configurations for my serial connection are simple:

8N1 9600 baud no CTSRTS, no software flow control. There must be some small error in my settings since most of the characters are correct but some are wrong: 91 becomes 11 (10010001 -> 00010001, so only 1 bit off).

This is the code for setting up the connection:

int fd = open(adres , O_RDWR | O_NOCTTY | O_NDELAY);

struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
// no parity 8N1
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

tcsetattr(fd, TCSANOW, &options);

I also noticed that when you change these configurations and change you program and run it again that these configurations are not set to a default. So unless you change them they will be the same between independent executions of a program. Maybe someone has a list of all the defaults so I can always restore default values on start-up. (I got a second program using fopen() to read from the serial communication stream and this program only 'works' when I first run this code to set the right configurations)

Any idea what's going wrong here? I've read this: http://www.easysw.com/~mike/serial/serial.html but can't figure it out.

Flags I also tried are the following:

options.c_cflag |= CRTSCTS;   // hardware flow control
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //raw input mode
options.c_oflag &= ~OPOST;                  // raw output mode
options.c_iflag |= (INPCK | ISTRIP);    // parity check
options.c_iflag &= ~(IXON | IXOFF | IXANY); //disable flow control

Without a good result. If you want more info just ask.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Silver
  • 1,075
  • 3
  • 12
  • 37
  • When I rebooted it works. So there must have been some sort of configuration I changed once while programming that messed up everything. Since closing the file doesn't restore defaults this configuration stayed wrong until reboot. I'm guessing that was the problem unless anyone thinks it was something else? Please let me know, I don't want to have it again. – Silver Jan 30 '13 at 15:21

1 Answers1

0

If you're using an XBee module, you might want to take a look at this ANSI C XBee Host library Digi has released as Open Source for communicating with their XBee hardware. Even if you're not using the XBee, you could probably re-use their serial library -- they support POSIX, Win32 and even DOS (COM1 only).

tomlogic
  • 11,489
  • 3
  • 33
  • 59
  • thanks, as I said before, rebooting solved it. I must have set a configuration in one of my programs once and it sticked with me until I rebooted. Thanks for the info – Silver Feb 06 '13 at 21:23