Qt編寫氣體安全管理系統8-曲線監控

1、前言

曲線監控模塊用的不多,主要就是用來觀察某個設備的實時採集的數據和歷史採集的數據,能夠回放數據,在右側能夠選擇對應的通訊端口和控制器,而後選擇指定的探測器進行觀察,從選擇的時候開始計時,每一個數據都對應一個數據點,至於採集間隔,這個在端口管理中設定的,通常來講都是1秒鐘採集一次。node

顯示曲線圖表控件,我的強烈推薦開源的qcustomplot或者qwt,Qt5.7之後集成了qchart模塊也有曲線控件,我的以爲用法仍是不夠友好,並且不少人反映大數據量基本上歇菜,只能作一些小數據量的展現,我看過qchart模塊的完整源碼,寫的不是很好,也許後期改進了很多,仍是Qt4時代若基亞品質保證,代碼質量都是槓槓的,惋惜若基亞把Qt賣了。mysql

皮膚開源:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo 文件名稱:styledemolinux

體驗地址:https://gitee.com/feiyangqingyun/QWidgetExe https://github.com/feiyangqingyun/QWidgetExe 文件名稱:bin_sams.zipc++

2、功能特色

  1. 採集數據端口,支持串口端口+網絡端口,串口支持自由設置串口號+波特率,網絡支持自由設置IP地址+通信端口,每一個端口支持採集週期,默認1秒鐘一個地址,支持設置通信超時次數,默認3次,支持最大重連時間,用於從新讀取離線的設備。
  2. 控制器信息,可以添加控制器名稱,選擇控制器地址+控制器型號,設置該控制器下面的探測器數量。
  3. 探測器信息,可以添加位號,可自由選擇探測器型號,氣體種類,氣體符號,高報值,低報值,緩衝值,清零值,是否啓用,報警聲音,背景地圖,存儲週期,數值換算小數點位數,報警延時時間,報警的類型(HH,LL,HL)等。
  4. 控制器型號+探測器型號+氣體種類+氣體符號,都可自由配置。
  5. 地圖支持導入和刪除,全部的探測器對應地圖位置可自由拖動保存。
  6. 端口信息+控制器信息+探測器信息,支持導入導出+導出到excel+打印。
  7. 運行記錄+報警記錄+用戶記錄,支持多條件組合查詢,好比時間段+控制器+探測器等,全部記錄支持導出到excel+打印。
  8. 導出到excel的記錄支持全部excel+wps等表格文件版本,不依賴excel等軟件。
  9. 可刪除指定時間範圍內的數據,支持自動清理早期數據,設置最大保存記錄數。
  10. 支持報警短信轉發,支持多個接收手機號碼,可設定發送間隔,好比即時發送或者6個小時發送一次全部的報警信息,短信內容過長,自動拆分多條短信。
  11. 支持報警郵件轉發,支持多個接收郵箱,可設定發送間隔,好比即時發送或者6個小時發送一次全部的報警信息,支持附件發送。
  12. 高報顏色+低報顏色+正常顏色+0值顏色+曲線背景+曲線顏色等,均可以自由選擇。
  13. 軟件的中文標題+英文標題+logo路徑+版權全部均可以自由設置。
  14. 提供開關設置開機運行+報警聲音+自動登陸+記住密碼等。
  15. 報警聲音可設置播放次數,界面提供17種皮膚文件選擇。
  16. 支持雲端數據同步,可設置雲端數據庫的信息,好比數據庫名稱,用戶名+密碼等。
  17. 支持網絡轉發和網絡接收,網絡接收開啓後,軟件從udp接收數據進行解析。網絡轉發支持多個目標IP,這樣就實現了本地採集的軟件,自由將數據轉到客戶端,隨時查看探測器數據。
  18. 自動記住用戶最後停留的界面+其餘信息,重啓後自動應用。
  19. 報警自動切換到對應的地圖,探測器按鈕閃爍。
  20. 雙擊探測器圖標,能夠進行回控。
  21. 支持用戶權限管理,管理員+操做員兩大類,用戶登陸+用戶退出,能夠記住密碼和自動登陸,超過三次報錯提示並關閉程序。
  22. 支持四種監控模式,設備面板監控+地圖監控+表格數據監控+曲線數據監控,可自由切換,四種同步應用。
  23. 支持報警繼電器聯動,一個位號能夠跨串口聯動多個模塊和繼電器號,支持多對多。
  24. 本地數據存儲支持sqlite+mysql,支持遠程數據同步到雲端數據庫。自動重連。
  25. 本地設備採集到的數據實時上傳到雲端,以便手機APP或者web等其餘方式提取。
  26. 支持兩種數據源,一種是串口和網絡經過協議採集設備數據,一種是數據庫採集。數據庫採集模式能夠做爲通用的系統使用。
  27. 自帶設備模擬工具,支持16個設備數據模擬,同時還帶數據庫數據模擬,以便在沒有設備的時候測試數據。
  28. 默認通訊協議採用modbus協議,後期增長mqtt等物聯網協議的支持,作成通用系統。
  29. 支持全部windows操做系統+linux操做系統和其餘操做系統。

3、效果圖

4、核心代碼

void frmViewPlot::initPlot()
{
    ui->customPlot->addGraph();
    ui->customPlot->graph(0)->setName("濃度值");
    ui->customPlot->graph(0)->setPen(QPen(QColor(App::ColorPlotLine), LineWidth));
    ui->customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle,
                                                              Qt::NoPen, QBrush(QColor(App::ColorPlotLine)), DotWidth));

    QFont font;
    font.setPixelSize(11);
    ui->customPlot->legend->setFont(font);
    ui->customPlot->xAxis->setLabelFont(font);
    ui->customPlot->yAxis->setLabelFont(font);
    ui->customPlot->xAxis->setTickLabelFont(font);
    ui->customPlot->yAxis->setTickLabelFont(font);

    ui->customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    ui->customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
    ui->customPlot->xAxis->setAutoTickStep(false);
    ui->customPlot->xAxis->setTickStep(TickStep);
    ui->customPlot->xAxis->setRange(0, XMax, Qt::AlignRight);
    ui->customPlot->yAxis->setRange(0, 100);
    ui->customPlot->legend->setVisible(true);

    QColor textColor = QColor(App::ColorPlotText);
    ui->customPlot->yAxis->setLabelColor(textColor);
    ui->customPlot->xAxis->setTickLabelColor(textColor);
    ui->customPlot->yAxis->setTickLabelColor(textColor);
    ui->customPlot->xAxis->setBasePen(QPen(textColor));
    ui->customPlot->yAxis->setBasePen(QPen(textColor));
    ui->customPlot->xAxis->setTickPen(QPen(textColor));
    ui->customPlot->yAxis->setTickPen(QPen(textColor));
    ui->customPlot->xAxis->setSubTickPen(QPen(textColor));
    ui->customPlot->yAxis->setSubTickPen(QPen(textColor));
    ui->customPlot->xAxis->grid()->setPen(QPen(textColor));
    ui->customPlot->yAxis->grid()->setPen(QPen(textColor));
    ui->customPlot->yAxis->grid()->setZeroLinePen(QPen(textColor));

    ui->customPlot->setBackground(QColor(App::ColorPlotBg));
    //ui->customPlot->xAxis->setTickLabelRotation(90);
    ui->customPlot->yAxis->setAutoTickCount(30);
    ui->customPlot->replot();
    //ui->customPlot->installEventFilter(this);
}

void frmViewPlot::loadPlot()
{
    //移除第一個數據,增長一個數據,保證每次集合數據一致
    labs.remove(0, 1);
    labs.append(TIME);
    values.remove(0, 1);
    values.append(currentValue);

    ui->customPlot->graph(0)->setData(keys, values);
    ui->customPlot->xAxis->setTickVector(keys);
    ui->customPlot->xAxis->setTickVectorLabels(labs);
    ui->customPlot->replot();
}

void frmViewPlot::initData()
{
    whereSql = "where 1=1";
    columnNames << "編號" << "位號" << "控制器名稱" << "探測器名稱" << "濃度值" << "氣體符號" << "保存時間";
    columnWidths << 100 << 120 << 145 << 145 << 70 << 70 << 150;

    //設置須要顯示數據的表格和翻頁的按鈕,最後一列自動填充,奇偶行不一樣顏色顯示
    dbPage = new DbPage(this);
    dbPage->setAllCenter(true);
    dbPage->setColumnNames(columnNames);
    dbPage->setColumnWidths(columnWidths);
    dbPage->setResultCurrent(XMax2);
    dbPage->setTableName("NodeLog");
    dbPage->setOrderSql(QString("LogID %1").arg(App::AlarmLogOrder));
    dbPage->setControl(ui->tableView, ui->labPageCount, ui->labPageCurrent, ui->labResultCount, ui->labResultCurrent, ui->labResult, 0,
                       ui->btnFirst, ui->btnPre, ui->btnNext, ui->btnLast, "LogID");

    dbPage->setWhereSql(whereSql);
    dbPage->select();

    //綁定按鈕切換載入數據
    connect(ui->btnFirst, SIGNAL(clicked()), this, SLOT(loadData()));
    connect(ui->btnPre, SIGNAL(clicked()), this, SLOT(loadData()));
    connect(ui->btnNext, SIGNAL(clicked()), this, SLOT(loadData()));
    connect(ui->btnLast, SIGNAL(clicked()), this, SLOT(loadData()));
}

void frmViewPlot::loadData()
{
    keys.clear();
    labs.clear();
    values.clear();

    //加載當前表格中的數據
    QAbstractItemModel *model = ui->tableView->model();
    int count = model->rowCount();
    for (int i = 0; i < count; i++) {
        QModelIndex modelIndex = model->index(i, 4);
        QModelIndex labIndex = model->index(i, 6);
        double value = model->data(modelIndex).toDouble();
        QString strDate = model->data(labIndex).toString().right(8);

        keys << i;
        labs << strDate;
        values << value;
    }

    ui->customPlot->graph(0)->setData(keys, values);
    ui->customPlot->xAxis->setTickVector(keys);
    ui->customPlot->xAxis->setTickVectorLabels(labs);
    ui->customPlot->replot();
}

void frmViewPlot::receiveValue(const QString &positionID, float value)
{
    if (ui->ckPause->isChecked()) {
        return;
    }

    //必須是實時曲線才須要顯示
    if (ui->cboxType->currentText() != "實時曲線") {
        return;
    }

    //找到當前位號對應的索引,取出對應索引位置的值
    QString id = ui->cboxNodeName->itemData(ui->cboxNodeName->currentIndex()).toString();
    if (id != positionID) {
        return;
    }

    currentValue = value;

    //如下增長部分爲將接收數據曲線顯示
    QVector<double> keys;
    QVector<double> datas;
    double key = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000.0;
    keys.append(key);
    datas.append(currentValue);

    ui->customPlot->graph(0)->addData(keys, datas);
    ui->customPlot->graph(0)->removeDataBefore(key - XMax);
    ui->customPlot->xAxis->setRange(key, XMax, Qt::AlignRight);
    ui->customPlot->replot();
}

void frmViewPlot::receiveValue(const QString &portName, quint8 addr, const QList<float> &values)
{
    if (ui->ckPause->isChecked()) {
        return;
    }

    //必須是實時曲線才須要顯示
    if (ui->cboxType->currentText() != "實時曲線") {
        return;
    }

    //過濾端口
    if (ui->cboxPortName->currentText() != portName) {
        return;
    }

    //過濾設備
    if (ui->cboxDeviceName->itemData(ui->cboxDeviceName->currentIndex()).toInt() != addr) {
        return;
    }

    //找到當前位號對應的索引,取出對應索引位置的值
    QString positionID = ui->cboxNodeName->itemData(ui->cboxNodeName->currentIndex()).toString();
    //找到當前索引位置的設備地址對應探測器的最小寄存器地址
    //若是讀取的起始寄存器地址是5則回來的數據位第一個是寄存器地址5的數據,後面連續
    quint16 nodeMinAddr = DBHelper::getNodeMinAddr(portName, addr);
    int index = DBData::NodeInfo_PositionID.indexOf(positionID);
    int startIndex = DBData::NodeInfo_NodeAddr.at(index) - nodeMinAddr - 1;
    //有時候可能出現總共添加了8個探測器可是真實讀到4個探測器的狀況
    if (startIndex >= values.count()) {
        return;
    }

    currentValue = values.at(startIndex);

    //如下增長部分爲將接收數據曲線顯示
    QVector<double> keys;
    QVector<double> datas;
    double key = QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000.0;
    keys.append(key);
    datas.append(currentValue);

    ui->customPlot->graph(0)->addData(keys, datas);
    ui->customPlot->graph(0)->removeDataBefore(key - XMax);
    ui->customPlot->xAxis->setRange(key, XMax, Qt::AlignRight);
    ui->customPlot->replot();
}
相關文章
相關標籤/搜索