I had this working, thanks to help in another thread, and now when I open the serial port for reading and writing, I can ONLY read from the serial port in the same function where I opened it.
This is not all the code in the file, but I start with my variables:
Code:
//Seperate namespace to keep them invisible to other files
namespace {
bool isOpen = false;
int infd = 0, outfd = 0, lastread = 0;
unsigned long naptime = 100;
long BAUD = B115200;
long DATABITS = CS8;
long STOPBITS = 0;
long PARITYON = 0;
long PARITY = 0;
// Default value: use the RS232 port
string serdev = "/dev/ttyS0";
}
Then, in order of use (but almost reverse order of how they're in the file), I have a function to open the serial port as a file:
Code:
void openport() {
if (isOpen) return;
isOpen = true;
cout << "Opening port: " << serdev << endl;
infd = open(serdev.c_str(), O_RDWR | O_NOCTTY);
fcntl(infd, F_SETFL, 0);
setportattr(infd);
//Open for output later (device doesn't work well using same FD for in and out!)
// outfd = open(serdev.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
// setportattr(outfd);
if (infd == -1 || outfd == -1) {
if (infd == -1)
perror("Unable to open port for reading");
if (outfd == -1)
perror("Unable to open port for writing");
exit(1);
} else {
cout << "Port " << serdev << " has been opened. Input: " << infd << ", Output " << outfd << endl;
}
//Test read the port from in this function -- always works and reads a byte
cout << "====>Attempting to read port: " << infd << endl;
char* buffx; int rdx;
rdx = read(infd, buffx, 1);
cout << "====>FD: " << infd << ", Read length: " << rdx << endl;
//Call other routine to test read it from outside function - always fails, even
//if the test above is removed, so it's not a matter of if there's no data
fakeread();
return;
}
I initialize the serial port parameters here:
Code:
void setportattr(int portfd) {
struct termios options;
tcgetattr(portfd, &options);
//Should never change, set at 115200
options.c_cflag |= BAUD;
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);
options.c_cflag &= ~CRTSCTS;
int rc = tcsetattr(portfd,TCSANOW ,&options);
cout << "FD: " << portfd << ", Attr return code: " << rc << endl;
return;
}
Setting attributes is always successful. I've also tried reading a byte from the file in this function. It's worked in some cases.
Then there's the simple function to do another test read:
Code:
void fakeread() {
cout << "---->Attempting to read port: " << infd << endl;
char* buffy; int rdy;
rdy = read(infd, buffy, 1);
cout << "---->FD: " << infd << ", Read length: " << rdy << endl;
return;
}
If I put read() statements anywhere in the openport() function, it works, but once I move it out and into another function, it ALWAYS fails (but it has worked if it was in the setportattr() function, at the very end of that function).
This was working perfectly this afternoon. I can't figure out what I did that might have changed it that much, but now I can only read from the device from within that function. The debugging output I've printed (statements are included in above samples) shows that it's using the right file descriptor.
Is this something so bleeding obvious that I'm not seeing it?
Thanks for any help on this!