串行通信崩溃

Serial communication crash

本文关键字:崩溃 通信      更新时间:2023-10-16

我使用的是一个无线电模块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();
 }

也许当读取失败时,我会丢失一些数据,但至少程序不能关闭,也不必手动重新启动。