I'm using a radio module XBee pro and I log some radio data.
I use a FTDI serial to USB converter so the module appear under /dev/ttyUSB0
.
I've written this code :
void TsToCoord::serialConfig()
{
// Open Serial Port
cout << "Opening serial port..." << endl;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
if (fd < 0 )
{
cout << "Error " << errno << " opening /dev/ttyUSB0: " << strerror(errno) << endl;
}
else
{
//Configure Serial Port
cout << "Configuring serial port..." << endl;
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
cout << "Error " << errno << " from tcgetattr: " << strerror (errno) << endl;
}
cfsetispeed(&tty, B57600);
cfsetospeed(&tty, B57600);
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS;
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 50;
tty.c_cflag |= CREAD | CLOCAL;
cfmakeraw(&tty);
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &tty) != 0)
{
cout << "Error " << errno << " from tcsetattr" << endl;
}
}
}
void TsToCoord::listenPort()
{
// Creation of a buffer to store data from radio module
fill_n(buff, 2048, '\0');
this-> ind = 0;
while(true)
{
char mes[1024];
fill_n(mes, 1024, '0');
//cout << "Blocking read" << endl;
int rd = read(fd, &mes, sizeof(mes));
if (rd > 0)
{
//cout << "Storing in buffer" << endl;
storeInBuff(mes, rd);
fill_n(mes, 1024, '0');
struct pollfd fds;
fds.fd = fd;
fds.events = POLLIN | POLLPRI;
int slct = 1;
/*
int slct = 1;
fd_set rdfds;
FD_ZERO(&rdfds);
FD_SET(fd, &rdfds);
struct timeval to;
to.tv_sec = 0;
to.tv_usec = 100000;
*/
//fd_set rdfdsCopy = rdfds;
//cout << "Entering second while loop" << endl;
while (slct > 0)
{
//cout << "Call to select" << endl;
//slct = select((fd+1), &rdfdsCopy, NULL, NULL, &to);
slct = poll(&fds, 1, 100);
if (slct > 0)
{
//cout << "Next call to read, would not block" << endl;
rd = read(fd, &mes, sizeof(mes));
storeInBuff(mes, rd);
//rdfdsCopy = rdfds;
}
}
findFrame(0);
ind = 0;
fill_n(buff, 2048, '\0');
}
}
}
My problem is that when it's launched it works perfectly. But after like 20 min, it does not do its work anymore. The CPU usage go to like 100% so the read call doesn't seem to block anymore. It's like the file descriptor isn't linked to the device anymore...
Since I absolutely doesn't know the reason and the bug and since it takes a random amount of time before crashing, I can't just un-daemonize it and watch the output in my terminal...
So I want to ask :
- Is there a big error I didn't found in the code.
- What can be the reason that makes it crash? I thought about another software that tries to use the same device but it doesn't seem to be the case. I don't think it's a baud rate problem either.
I added a check for the case where the radio is unplugged, so the program can't exit from its own. I'm pretty sure it's not the problem since the module stayed under /dev/ttyUSB0
after the crash occurs.
I use Debian 3.2.57-3 i686.
I don't have any problems with other software when using them with my radio module.
I seem to not have problems using this code on another similar computer...
Thanks for reading and sorry for a not so good English.
EDIT : to be more precise on what I want from this post and from this program : At some point the program fail to block on read and every toher calls to read don't block and don't read anything so the program doesn't do what it was created for : log data coming from radio module. I simply want to avoid it since without this it works perfectly fine and it could cause harm to hardware.