我使用一个无线电模块XBee pro和我记录一些无线电数据。
我使用FTDI串行到USB转换器,所以模块出现在/dev/ttyUSB0下。
我写了这段代码:
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');
}
}
}我的问题是,当它发射时,它的工作是完美的。但是在大约20分钟后,它不再工作了。CPU使用率达到100%,因此读取调用似乎不再阻塞。就像文件描述符不再链接到设备上一样.
因为我完全不知道原因和错误,而且它在崩溃前需要随机的时间,我不能只是不去消除它和观察我的终端输出.
所以我想问:
我增加了一个检查的情况下,收音机是拔出,所以程序不能退出自己的。我很确定这不是问题,因为模块在崩溃发生后一直停留在/dev/ttyUSB0下。
我使用Debian3.2.57-3 i686.
当其他软件与我的无线电模块一起使用时,我没有任何问题。
我在另一台类似的计算机上使用这段代码似乎没有问题.
谢谢你的阅读,不太好的英语很抱歉。
编辑:更准确地说,我想从这篇文章和这个节目:在某个时候,程序没有阻止在读和每一个她的呼叫读不阻塞和不读取任何东西,所以该程序不做它所创建的:日志数据从无线电模块。我只是想避免它,因为没有它,它的工作非常好,它可能会对硬件造成伤害。
发布于 2016-07-20 11:15:48
快速查看您的代码--如果您在读取时得到EOF或错误(即。read()返回0或-1)您将永远循环。我可以看到您处于原始模式,但我在FTDI驱动程序和固件中看到了可能导致这种情况发生的各种错误,而这不是您正在处理的情况。
发布于 2016-07-21 07:54:46
所以我跟着@janm暗示。我删除了这一行:
tty.c_cflag &= ~CRTSCTS;做了这样的测试:
rd = read(fd, &mes, sizeof(mes));
if (rd > 0)
{
doSomeStuff();
}
else
{
close(fd);
serialConfig();
listenPort();
}也许当读取失败时,我丢失了一些数据,但至少程序不能关闭,也不必手动重新启动。
https://stackoverflow.com/questions/38479498
复制相似问题