Serial port 串行端口:卡在读取()中 我有一个C++程序,它读取IMU设备并在处理后将数据写入文件。 速率很高,每秒500行。该程序似乎工作正常,但它会随机停止读取串行端口

Serial port 串行端口:卡在读取()中 我有一个C++程序,它读取IMU设备并在处理后将数据写入文件。 速率很高,每秒500行。该程序似乎工作正常,但它会随机停止读取串行端口,serial-port,port,communication,Serial Port,Port,Communication,经过一些研究,它似乎被卡在了read命令中: while((res += read(IMU, header, 3)) != 3). 这是如何发生的?我如何防止这种情况发生 如果您需要任何其他信息,请告诉我 void* imuLogger(void* arg) { IMU = initSerial("/dev/ttyUSB0", B115200); openLog("logIMU", IMUfile); int res; char tmp; char header[3]; while(true

经过一些研究,它似乎被卡在了read命令中:

while((res += read(IMU, header, 3)) != 3).
这是如何发生的?我如何防止这种情况发生

如果您需要任何其他信息,请告诉我

void* imuLogger(void* arg) {
IMU = initSerial("/dev/ttyUSB0", B115200);
openLog("logIMU", IMUfile);
int res;
char tmp;
char header[3];

while(true)
{
    gettimeofday(&ts, NULL);
    timeLog = (ts.tv_sec * 1000000 + ts.tv_usec) - timeStart;

    /**** READ IMU ****/
    res = 0;
    cout << indexLog << "-"<< IMU<< endl;
    while((res += read(IMU, header, 3)) != 3);
    cout << indexLog << "entered loop"<< endl;
    string head(header, 3);


    if(head.compare("snp") == 0 || head.substr(1, 2).compare("np") == 0 || head.substr(0, 2).compare("sn") == 0 || (header[0] == 's' && header[2] == 'p'))
    {

        buf[0] = 's';
        buf[1] = 'n';
        buf[2] = 'p';

        while((res = read(IMU, &tmp, 1)) != 1);
        buf[3] = tmp;

        res = 4;
        while(res < sizeof(buf))
            res += read(IMU, buf + res, sizeof(buf) - res);

        parseData();
    }
    else if(head.substr(0, 2).compare("np") == 0)
    {

        buf[0] = 's';
        buf[1] = 'n';

        buf[3] = buf[2];
        buf[2] = 'p';

        res = 4;
        while(res < sizeof(buf))
            res += read(IMU, buf + res, sizeof(buf) - res);

        parseData();
    }
    else if(head.substr(1,2).compare("sn") == 0)
    {

        buf[0] = 's';
        buf[1] = 'n';
        buf[2] = 'p';

        while((res = read(IMU, &tmp, 1)) != 1);
        while((res = read(IMU, &tmp, 1)) != 1);
        buf[3] = tmp;

        res = 4;
        while(res < sizeof(buf))
            res += read(IMU, buf + res, sizeof(buf) - res);

        parseData();
    }
    else
    {
        //tcflush(IMU, TCIOFLUSH);
        //while((res = read(IMU, header, 1)) != 1)
        //  cout << 
        cout << "Error parser\n";
    }
//parseData();
        //gettimeofday(&ts, NULL);
        //timeEnd = (ts.tv_sec * 1000000 + ts.tv_usec) - time - timeStart;
        //cout << "Length:" << res << " Time:" << timeEnd << " Address:" << (int)packet.Address << endl;
        //usleep(10000 - timeEnd);

//cout <<"entered"<< endl;
    }
return 0;
}

int main() {

    signal(SIGINT, exit_handler);

    gettimeofday(&ts, NULL);
    timeStart = ts.tv_sec * 1000000 + ts.tv_usec;

    pthread_create(&imuThread, NULL, imuLogger, NULL);
    pthread_create(&rpmThread, NULL, rpmLogger, NULL);
    //pthread_create(&baroThread, NULL, baroLogger, NULL);

    while(true)
    { 
        gettimeofday(&ts4, NULL);
        timeLog_main = (ts4.tv_sec * 1000000 + ts4.tv_usec) - timeStart;
        cout << timeLog_main/1e6 << endl;
        sleep(1);
    }

    return 0;
}
void*imuLogger(void*arg){
IMU=initSerial(“/dev/ttyUSB0”,B115200);
openLog(“logIMU”,IMUfile);
国际关系;
char-tmp;
字符头[3];
while(true)
{
gettimeofday(&ts,NULL);
timeLog=(ts.tv_sec*1000000+ts.tv_usec)-timeStart;
/****阅读IMU****/
res=0;

cout如果
read()
第一次返回1个字节,第二次返回3个字节,那么
res
永远不会是3。

可能需要更详细地描述硬件,因为它可能是一个特定设备的怪癖。“IMU”是什么它是否唯一地描述了一种设备?粗的IMU是CH-UM6,它是广播模式,意思是它总是发送数据。我已经用java编写了一个程序,它确实是我想要的,但是它是慢的在树莓PI上运行。为此,我把它改写成C++,现在碰到了这种奇怪的不稳定性。树莓的原因是它必须在伐木时飞行。