From 7407c74f764c937efa66121125c5918e525c88ff Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Tue, 3 Oct 2023 00:24:09 +0800 Subject: [PATCH] no message --- global.h | 12 ++++++++++++ subform.cpp | 40 +++++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/global.h b/global.h index f7329f1..6c0c679 100644 --- a/global.h +++ b/global.h @@ -5,6 +5,7 @@ #include #include #include +#include extern "C"{ #include "libmodbus/modbus.h" @@ -14,6 +15,7 @@ typedef struct { int addr; float val1; QString time; + bool succ; }CapData; @@ -86,17 +88,27 @@ public: QThread::msleep(1000); int ret = modbus_read_registers(mMod,itr->req[s].addr,itr->req[s].len,dat); + mMux.lock(); + bool succ = true; + if(mListData.contains(itr->addr)){ + if(ret < 0){ + succ = false; + } mListData[itr->addr]->push_front(new CapData{ itr->req[s].addr, float(float(dat[0])), + QDateTime::currentDateTime().toString("yyyy-mm-DD hh:MM::ss"), + true }); }else{ mListData[itr->addr] = new QList; mListData[itr->addr]->push_front(new CapData{ itr->req[s].addr, float(float(dat[0])), + QDateTime::currentDateTime().toString("yyyy-mm-DD hh:MM::ss"), + succ }); } diff --git a/subform.cpp b/subform.cpp index 6e227f2..0429c92 100644 --- a/subform.cpp +++ b/subform.cpp @@ -33,7 +33,7 @@ SubForm::SubForm(QString addr,int baurate,int device_addr) mConfig->com = *com; mConfig->baurate = baurate; - connect(pTimer,&QTimer::timeout,[=](){ + connect(pTimer,&QTimer::timeout,[=]() { static float sd = 0; if(gAsyncData == nullptr) return; @@ -41,8 +41,8 @@ SubForm::SubForm(QString addr,int baurate,int device_addr) int ret = gAsyncData->TakeLast(device_addr,&z); if(ret == 0){ + bool succ = z->succ; mSeries1->append(sd,z->val1); - pTime.append(QDateTime::currentDateTime().toString("yyyyMMdd-hh:mm:ss")); QStandardItem *found = nullptr; @@ -52,39 +52,45 @@ SubForm::SubForm(QString addr,int baurate,int device_addr) int j = 0; for(;i < mModel->rowCount();i++){ for(;j < mModel->columnCount();j++){ - qDebug()<item((i,j))->data(Qt::DisplayRole); - if(mModel->item((i,j))->data(Qt::DisplayRole).toInt() == z->addr){ - found = mModel->item((i,j)); - break; + if(mModel->item((i,j)) != nullptr){ + qDebug()<item((i,j))->data(Qt::DisplayRole); + if(mModel->item((i,j))->data(Qt::DisplayRole).toInt() == z->addr){ + found = mModel->item((i,j)); + break; + } } } } -// if(list.size() != 0){ -// found = list[0]; -// } - if(found == 0){ - QStandardItem *item = new QStandardItem(QString("%1").arg(z->val1)); - mModel->setItem(i, 1, item); + QStandardItem *item; + if(succ) + item = new QStandardItem(QString("%1").arg(z->val1)); + else + item = new QStandardItem(QString("超时")); + mModel->setItem(i, 1, item); QStandardItem *item1 = new QStandardItem(QString("%1").arg(z->addr)); mModel->setItem(i, 0, item1); + mModel->setItem(i, 2, new QStandardItem(QString("%1").arg(z->time))); + }else{ mModel->item(found->row(),0)->setData(QString("%1").arg(z->addr),Qt::DisplayRole); - mModel->item(found->row(),1)->setData(QString("%1").arg(z->val1),Qt::DisplayRole); + if(succ) + mModel->item(found->row(),1)->setData(QString("%1").arg(z->val1),Qt::DisplayRole); + else + mModel->item(found->row(),1)->setData(QString("超时"),Qt::DisplayRole); + mModel->item(found->row(),2)->setData(QString("%1").arg(z->time),Qt::DisplayRole); + } -// mModel->clear(); -// mModel->setHorizontalHeaderLabels(QStringList() << "寄存器地址" << "返回值"<<"读取时间"); - - sd ++; if(sd > mMaxX){ mMaxX += 1024; mAxisX->setMax(mMaxX); } + delete z; } }); pTimer->start(100);