在信号触发之前获取数据的计时器

时间:2019-06-17 01:35:40

标签: c++ timer

以前,我可以在收到信号(按下按钮)时获取传感器数据,并将这些数据存储到CSV文件中。但是在按下按钮之前,只有一个传感器总是在1〜2s左右有下降沿信号。所以我需要创建一个计时器来获取数据,然后再按下按钮。但是据我所知,C ++具有带有duration_cast的chrono只能创建一个确定时间的计时器,作为延迟计时器。

我的问题是:如何创建计时器以使程序在发出信号之前开始? 我试图在do-while循环中创建CSV文件,并在按下按钮if (uData == 0)之后插入新数据。但是,当我按下按钮时,它仅关闭文件并将新数据重新写入相同的文件名。我的期望是关闭旧文件,然后打开一个新文件。

do
        {
            clock_t timeElapsed = clock() - start;
            unsigned secElapsed = timeElapsed / CLOCKS_PER_SEC;
            unsigned msElapsed = timeElapsed / CLOCKS_PER_MS;           

            //---- Read AI signal 1s before getting DI -----------
            ret = wfAiCtrl->GetData(getDataCount, userDataBuffer, -1, &returnedCount);
            CHK_RESULT(ret);        

            sprintf(filename, "%04d%02d%02d_%02dh%02dm%02ds.csv", timeinfo->tm_year, timeinfo->tm_mon, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
            printf("Filename: %s \n\n", filename);

            rgbData.open(filename, std::ios::out | std::ios::trunc);
            rgbData << "Date" << "," << "Time" << "," << "Current" << "," << "Voltage" << "," << "AirPressure" << "," << "Channel 3" << "," << "Channel 4"
                << "," << "Channel 5" << "," << "Channel 6" << "," << "Channel 7" << "," << "," << endl;

            while (msElapsed >= 1000)
                msElapsed -= 1000;
            while ((timeinfo->tm_sec + secElapsed) > 59)
            {
                timeinfo->tm_sec -= 60;
                //      secElapsed = 0;
                timeinfo->tm_min++;
            }
            while (timeinfo->tm_min > 59)
            {
                timeinfo->tm_min -= 60;
                timeinfo->tm_hour++;

            }
            while (timeinfo->tm_hour > 23)
            {
                timeinfo->tm_hour -= 24;
                timeinfo->tm_mday++;
            }

            double us = 1.0 / returnedCount;

            for (int32 i = 0; i < returnedCount; i = i + channelCount)
            {
                rgbData << timeinfo->tm_year << "-" << timeinfo->tm_mon << "-" << timeinfo->tm_mday << "," << timeinfo->tm_hour << "h" << timeinfo->tm_min << "m" << timeinfo->tm_sec + secElapsed + us * i << "s" << ",";
                //rgbData << us * i << "ms" << ",";
                rgbData << userDataBuffer[i] << ",";
                rgbData << userDataBuffer[i + 1] << ",";
                rgbData << userDataBuffer[i + 2] << ",";
                rgbData << endl;
            }           
            ret = instantDiCtrl->ReadBit(0, 0, &uData);                         
            if (uData == 0)
            {               
                    ret = wfAiCtrl->GetData(getDataCount, userDataBuffer, -1, &returnedCount);
                    iGetDataTimes++;
                    printf("GetAI data times: %d\n", iGetDataTimes);
                    for (int32 i = 0; i < returnedCount; i = i + channelCount)
                    {
                        rgbData << timeinfo->tm_year << "-" << timeinfo->tm_mon << "-" << timeinfo->tm_mday << "," << timeinfo->tm_hour << "h" << timeinfo->tm_min << "m" << timeinfo->tm_sec + secElapsed + 1 + us * i << "s" << ",";
                        //rgbData << us * i << "ms" << ",";
                        rgbData << userDataBuffer[i] << ",";
                        rgbData << userDataBuffer[i + 1] << ",";
                        rgbData << userDataBuffer[i + 2] << ",";
                        rgbData << endl;
                    }                   
                rgbData.close();
                //CHK_RESULT(ret);

            }   
            SLEEP(0.01);
        } while (!kbhit());

总而言之,我有两个上述问题: 1)计时器在按钮信号发出前2秒钟获取数据 2)关闭旧文件,然后打开新文件。当我仅在按下按钮后才获取数据时,此任务太容易了。但是有了新的要求,它使我的程序运行异常。

0 个答案:

没有答案