串行通信崩溃
Serial communication crash
我使用的是一个无线电模块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, ' ');
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, ' ');
}
}
}
我的问题是,当它推出时,它运行得很完美。但大约20分钟后,它就不再工作了。CPU使用率接近100%,因此读取调用似乎不再阻塞。这就像文件描述符不再链接到设备。。。
由于我完全不知道原因和错误,而且崩溃前需要随机的时间,所以我不能只是解除它的守护程序并在我的终端中查看输出。。。
所以我想问:
- 是不是有一个我没有在代码中发现的大错误
- 是什么原因导致它崩溃?我想到了另一个尝试使用相同设备的软件,但事实似乎并非如此。我也不认为这是波特率的问题
我添加了一个检查的情况下,收音机是拔下的,所以程序不能退出自己的。我确信这不是问题所在,因为崩溃发生后模块一直处于/dev/ttyUSB0
之下。
我使用Debian 3.2.57-3 i686。
当我将其他软件与我的无线电模块一起使用时,我对它们没有任何问题。
我似乎在另一台类似的计算机上使用此代码没有问题。。。
谢谢你的阅读,很抱歉英语不太好。
编辑:更确切地说,我想从这篇文章和这个节目中得到什么:在某个时刻,程序无法阻止读取,每次调用读取都不会阻止也不会读取任何内容,因此程序不会执行创建目的:记录来自无线电模块的数据。我只是想避免它,因为如果没有它,它工作得很好,可能会对硬件造成伤害。
快速查看您的代码——如果您在读取时得到EOF或错误(即read()返回零或-1),您将永远循环。我可以看到你处于原始模式,但我在FTDI驱动程序和固件中看到了各种错误,这些错误可能会导致这种情况发生,这不是你正在处理的情况。
所以我遵循了@janm提示。我删除了这行:
tty.c_cflag &= ~CRTSCTS;
并做了这样的测试:
rd = read(fd, &mes, sizeof(mes));
if (rd > 0)
{
doSomeStuff();
}
else
{
close(fd);
serialConfig();
listenPort();
}
也许当读取失败时,我会丢失一些数据,但至少程序不能关闭,也不必手动重新启动。
- 当回溯以零开始时,如何调试崩溃
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 程序崩溃并显示"std::out_of_range"错误
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 是否可以使用winusb同时与多个相同的usb设备进行通信
- CoInitialize()在单独的线程上崩溃而不返回
- 使用调试/崩溃报告将应用程序部署到客户端
- 为什么所有C++编译器都会崩溃或挂起此代码
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 为什么我的多线程作业队列崩溃
- ExtractIconEx:可以工作,但偶尔会崩溃
- 混淆了如何使用IDL与Ethovision进行通信
- 为什么引用传递会导致此崩溃(C++)
- 试图创建流或fopen时程序崩溃
- 类对象数组的问题会导致崩溃
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 为什么要增加导致崩溃的指针
- UCRTbase.dll涉及TCP通信的C ++ Windows Embedded Standard崩溃
- 串行通信崩溃