Newer
Older
ZXSSCJ / CounterAcq / CounterDevice.cpp
TAN YUE on 30 Sep 2021 3 KB 20210930 初始提交
#include "CounterDevice.h"

#include <iostream>
#include <QDateTime>

CounterDevice::CounterDevice(QObject *parent) : QObject(parent)
{
    connect(&this->serialUtil, &QSerialPortUtil::dataRecieved,
                this, &CounterDevice::dataReceivedHandler);
}

CounterDevice::~CounterDevice()
{
    disconnect(&this->serialUtil, &QSerialPortUtil::dataRecieved,
                this, &CounterDevice::dataReceivedHandler);
}

void CounterDevice::setComName(QString comName)
{
    this->comName = comName;
}
void CounterDevice::setBaudRate(int baudRate)
{
    this->baudRate = baudRate;
}
QString CounterDevice::getDevCode()
{
    return this->devCode;
}
void CounterDevice::setDevCode(QString devCode)
{
    this->devCode = devCode;
}

bool CounterDevice::isSerialOpen()
{
    return this->serialUtil.isOpen();
}

void CounterDevice::initSerialPort()
{
    this->serialUtil.openSerialPort(this->comName, this->baudRate);
}


void CounterDevice::dataReceivedHandler(QByteArray data)
{
    this->dataBuff.append(data);

    CounterDataDto * counterData = new CounterDataDto(this);
    if (CounterProtocolBM::checkFrame(this->dataBuff) == true)
    {
        counterData->rawFrame = this->dataBuff;
        std::cout << counterData->rawFrame.toStdString() << std::endl;

        // ★解析成数据对象
        bool parse = CounterProtocolBM::parseMessureData(this->dataBuff, counterData);

        // 解析成功
        if (parse == true)
        {
            QDateTime now = QDateTime::currentDateTime();
            counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz");
            counterData->milisecond = now.toMSecsSinceEpoch();
            this->afterFramePhase(counterData);
        }
    }

    // 在此处释放内存,不影响后续显示
    // 不在此处释放内存则会导致内存持续增加
    // 具体原因不明
    delete counterData;
}

void CounterDevice::afterFramePhase(CounterDataDto * counterData)
{
    std::cout << counterData->frameId.toStdString() << ", " << counterData->timestamp.toStdString() << ", " << counterData->milisecond << std::endl;
//    for (int i = 1; i <= PHASE_MESSURE_CHANNEL; i++)
//    {
//        if (phaseData->channelActive.at(i-1).toInt() != 0)
//        {
//            std::cout << "ch: " << i << ", " << phaseData->channelData.at(i-1) << std::endl;
//        }
//    }

    // 1. 清空dataBuff,等待下一帧的数据
    this->dataBuff.clear();

    // 2. 输出到日志文件中
    QString filePostfix = counterData->timestamp.mid(0, 13).replace(" ", "_");
    QString filePrefix = QApplication::applicationDirPath() + "/logs/";

    // 2.1 原始字节数组数据
    QString filename = filePrefix + "raw_" + filePostfix + ".log";
    QString content = counterData->timestamp + " " + counterData->rawFrame.left(counterData->rawFrame.size() - COUNTER_FRAME_TAIL.size());
    QLogUtil::writeRawDataLog(filename, content);

    // 2.2 各个通道的相差数据
//    for (int i = 1; i <= phaseData->channelActive.size(); i++)
//    {
//        if (phaseData->channelActive.at(i-1).toUInt() == 1)
//        {
//            QString chFilename("%1CH_%2_%3.log");
//            chFilename = chFilename.arg(filePrefix);
//            if (i < 10)
//            {
//                chFilename = chFilename.arg(QString("0%1").arg(i));
//            } else
//            {
//                chFilename = chFilename.arg(i);
//            }
//            chFilename = chFilename.arg(filePostfix);
//            QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId);

//            QLogUtil::writeChannelDataLog(chFilename, channelDataStr);
//        }
//    }

    // 3. 输出到中间件,执行后续处理过程

    // 4. 在界面上简单显示相差数据结果
//    emit this->sendDataToDraw(counterData);
}