一、前言
曲線監控模塊用的很少,主要就是用來觀察某個設備的實時采集的數據和歷史采集的數據,可以回放數據,在右側可以選擇對應的通信端口和控制器,然后選擇指定的探測器進行觀察,從選擇的時候開始計時,每個數據都對應一個數據點,至於采集間隔,這個在端口管理中設定的,一般來說都是1秒鍾采集一次。
顯示曲線圖表控件,個人強烈推薦開源的qcustomplot或者qwt,Qt5.7以后集成了qchart模塊也有曲線控件,個人覺得用法還是不夠友好,而且很多人反映大數據量基本上歇菜,只能做一些小數據量的展示,我看過qchart模塊的完整源碼,寫的不是很好,也許后期改進了不少,還是Qt4時代若基亞品質保證,代碼質量都是杠杠的,可惜若基亞把Qt賣了。
皮膚開源:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo
文件名稱:styledemo
體驗地址:https://gitee.com/feiyangqingyun/QWidgetExe https://github.com/feiyangqingyun/QWidgetExe
文件名稱:bin_sams.zip
二、功能特點
- 采集數據端口,支持串口端口+網絡端口,串口支持自由設置串口號+波特率,網絡支持自由設置IP地址+通訊端口,每個端口支持采集周期,默認1秒鍾一個地址,支持設置通訊超時次數,默認3次,支持最大重連時間,用於重新讀取離線的設備。
- 控制器信息,能夠添加控制器名稱,選擇控制器地址+控制器型號,設置該控制器下面的探測器數量。
- 探測器信息,能夠添加位號,可自由選擇探測器型號,氣體種類,氣體符號,高報值,低報值,緩沖值,清零值,是否啟用,報警聲音,背景地圖,存儲周期,數值換算小數點位數,報警延時時間,報警的類型(HH,LL,HL)等。
- 控制器型號+探測器型號+氣體種類+氣體符號,均可自由配置。
- 地圖支持導入和刪除,所有的探測器對應地圖位置可自由拖動保存。
- 端口信息+控制器信息+探測器信息,支持導入導出+導出到excel+打印。
- 運行記錄+報警記錄+用戶記錄,支持多條件組合查詢,比如時間段+控制器+探測器等,所有記錄支持導出到excel+打印。
- 導出到excel的記錄支持所有excel+wps等表格文件版本,不依賴excel等軟件。
- 可刪除指定時間范圍內的數據,支持自動清理早期數據,設置最大保存記錄數。
- 支持報警短信轉發,支持多個接收手機號碼,可設定發送間隔,比如即時發送或者6個小時發送一次所有的報警信息,短信內容過長,自動拆分多條短信。
- 支持報警郵件轉發,支持多個接收郵箱,可設定發送間隔,比如即時發送或者6個小時發送一次所有的報警信息,支持附件發送。
- 高報顏色+低報顏色+正常顏色+0值顏色+曲線背景+曲線顏色等,都可以自由選擇。
- 軟件的中文標題+英文標題+logo路徑+版權所有都可以自由設置。
- 提供開關設置開機運行+報警聲音+自動登錄+記住密碼等。
- 報警聲音可設置播放次數,界面提供17種皮膚文件選擇。
- 支持雲端數據同步,可設置雲端數據庫的信息,比如數據庫名稱,用戶名+密碼等。
- 支持網絡轉發和網絡接收,網絡接收開啟后,軟件從udp接收數據進行解析。網絡轉發支持多個目標IP,這樣就實現了本地采集的軟件,自由將數據轉到客戶端,隨時查看探測器數據。
- 自動記住用戶最后停留的界面+其他信息,重啟后自動應用。
- 報警自動切換到對應的地圖,探測器按鈕閃爍。
- 雙擊探測器圖標,可以進行回控。
- 支持用戶權限管理,管理員+操作員兩大類,用戶登錄+用戶退出,可以記住密碼和自動登錄,超過三次報錯提示並關閉程序。
- 支持四種監控模式,設備面板監控+地圖監控+表格數據監控+曲線數據監控,可自由切換,四種同步應用。
- 支持報警繼電器聯動,一個位號可以跨串口聯動多個模塊和繼電器號,支持多對多。
- 本地數據存儲支持sqlite+mysql,支持遠程數據同步到雲端數據庫。自動重連。
- 本地設備采集到的數據實時上傳到雲端,以便手機APP或者web等其他方式提取。
- 支持兩種數據源,一種是串口和網絡通過協議采集設備數據,一種是數據庫采集。數據庫采集模式可以作為通用的系統使用。
- 自帶設備模擬工具,支持16個設備數據模擬,同時還帶數據庫數據模擬,以便在沒有設備的時候測試數據。
- 默認通信協議采用modbus協議,后期增加mqtt等物聯網協議的支持,做成通用系統。
- 支持所有windows操作系統+linux操作系統和其他操作系統。
三、效果圖


四、核心代碼
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();
}
