I'm new to C and C++ and I'm using it to communicate with a device (a HD Radio) hooked up to the serial port. I know this should be simple stuff and that it's just a matter of opening the serial port like a file and reading from/writing to it. I have a program in Perl that can send commands to the device and another that reads the replies from the device. I can run them both at the same time in separate windows and watch the listener give me the reply codes when I enter a command through the sender.

I would have preferred to write this in just C, but I'll be forwarding code to another project that will be using it and they've said they need the libraries in C++, so I'm using C++, even though the methods I'm using are not C++ specific.

I have a sender program in C++ that sends commands to the device and I have a listener in C++ that is not receiving the data properly. Ideally I want the listener to block while waiting for a new byte from the serial port because it'll be one of two threads and it'll spend most of its time blocked (overall, not much data comes back in), which makes thread management easier.

Last night I was finally able to get something from the serial port, but it was all 0x00, which is not what I know was being sent.

I've also found that the C++ sender was working fine without me setting any serial port parameters, but that's probably because they were still set from the Perl program. As I said, the Perl listener is working, but the C++ one is not. Here's the code to set up the serial port from Perl:

Code:
	$port = "/dev/ttyS0";
	$baud = "115200";
	$ob = Device::SerialPort->new ($port) || die "Can't Open $port: $!";
	$ob->baudrate($baud) || die "failed setting baudrate";
	$ob->parity("none") || die "failed setting parity";
	$ob->databits(8) || die "failed setting databits";
	$ob->handshake("none") || die "failed setting handshake";
	$ob->write_settings || die "no settings";
Here's the code I'm using to open and read from the serial port in C++:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <fcntl.h>
#include <termios.h>

void openinfile() {
	long BAUD = B115200;
	long DATABITS = CS8;
	long STOPBITS = 0;
	long PARITYON = 0;
	long PARITY = 0;
	struct termios options;
	
	cout << "Opening port for listening: " << serdev << endl;
//serdev is already set to /dev/ttyS0, serfd is an int
	serfd = open(serdev.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
	tcgetattr(serfd, &options);
	options.c_cflag |= B115200;
	options.c_cflag &= ~PARENB;
	options.c_cflag &= ~CSTOPB;
	options.c_cflag &= ~CSIZE;
	options.c_cflag |= CS8;
	options.c_cflag |= (CLOCAL | CREAD);
// 	options.c_iflag &= ~(IXON | IXOFF | IXANY);
	int rc = tcsetattr(serfd,TCSANOW ,&options);
	cout << "Attr return code: " << rc << endl;
//Return code is always 0

// 	options.c_cflag |= CRTSCTS;
	if (serfd == -1 ) {
		perror("open_port: Unable to open port");
		exit(1);
	} else {
// 		fcntl(serfd, F_SETFL, 0);
		printf("Port 1 has been sucessfully opened and %d is the file description\n", serfd);
	}
	return;
}

void readinfile() {
	char cIn, *buff;
	int i, rd;
//This next line should make us block until a byte comes in, but
// with it in place, no data comes in.  With it commented out,
// all 0x00 bytes are coming in now and not as many as should be for
// the data coming back from the device.
// 	fcntl(serfd, F_SETFL, 0);
	while (true) {
		rd = read(serfd, buff, 10);
//DEBUG: Remove when I know it's blocking and not looping.
// 		cout << "Waiting: " << rd << endl;
		if (rd <= 0)
			continue;
//DEBUG: Remove this line once I know it's actually reading in characters!
// 		cout << "Got a character!\n";
		cIn = buff[0];
//chout just prints the character as a 2 digit hex number, the decimal format, then the
// character itself it it's in printable range.
		chout(cIn);
	}
	return;
}
I can post the whole program if needed. It's between 80 - 100 lines long.

My guess is that I'm doing something or not doing something incredibly obvious that a new C++ programmer might not see but someone with experience will laugh at. If so, it's likely something on the low level end or Linux/serial port specific.

Why is it I either get no characters at all or only 0x00 from the C++ program?

I've been using a serial port article at http://www.easysw.com/~mike/serial/serial.html for one source and I've been doing a lot of Googling. It looks to me like it should work, but it doesn't.

Thanks for any help and links!