723 lines
24 KiB
C++
723 lines
24 KiB
C++
#include "mainwindow.h"
|
||
#include "ui_mainwindow.h"
|
||
|
||
#include <QtCore/QCoreApplication>
|
||
#include <QVector>
|
||
#include <QThread>
|
||
#include <QDebug>
|
||
#include <QGridLayout>
|
||
#include <QToolBar>
|
||
#include <QLabel>
|
||
#include <QGraphicsDropShadowEffect>
|
||
#include <QMessageBox>
|
||
#include <QSizePolicy>
|
||
#include <QFontDatabase>
|
||
#include <QDateTime>
|
||
|
||
#include "libmodbus/modbus.h"
|
||
|
||
#include "customdisplaypanel.h"
|
||
|
||
#include "formserialportsettingdialog.h"
|
||
|
||
#define _DEBUG_VSPD_
|
||
|
||
#define TH08D_TEMPERATURE_EQUIPMENT_81_00_09 40000
|
||
#define TH08D_TEMPERATURE_EQUIPMENT_81_09_06 40009
|
||
|
||
// 处理数组的槽函数
|
||
void DecodeWorker::processArray(const QVector<uint16_t>& array,int slave_id,int start_addr,int quantity,DeviceData* pData)
|
||
{
|
||
switch (start_addr)
|
||
{
|
||
case TH08D_TEMPERATURE_EQUIPMENT_81_00_09:
|
||
{
|
||
assert(pData->m_device_type == 81);
|
||
|
||
TemperatureData* pTemperature = (TemperatureData*)pData;
|
||
|
||
uint16_t value;
|
||
int index = 0;
|
||
pTemperature->m_device_online_state = array[index++]; //设备通讯状态
|
||
pTemperature->TempValue = (float)array[index++] * 1.0f / 10.f; //温度
|
||
pTemperature->HumidityValue = (float)array[index++] * 1.0f / 10.f; //湿度
|
||
pTemperature->DewPointValue = (float)array[index++] * 1.0f; //露点
|
||
value = array[index++]; //跳过温度偏移量
|
||
value = array[index++]; //跳过湿度偏移量
|
||
pTemperature->DO= array[index++]; //DO
|
||
pTemperature->DI1 = array[index++]; //DI1
|
||
pTemperature->DI2 = array[index++]; //DI2
|
||
pTemperature->bDecodeAlarm = false;
|
||
pTemperature->bDecodeTemp = true;
|
||
}
|
||
break;
|
||
|
||
case TH08D_TEMPERATURE_EQUIPMENT_81_09_06:
|
||
{
|
||
assert(pData->m_device_type == 81);
|
||
|
||
TemperatureData* pTemperature = (TemperatureData*)pData;
|
||
pTemperature->bDecodeAlarm = true;
|
||
pTemperature->bDecodeTemp = false;
|
||
|
||
uint16_t value;
|
||
int index = 0;
|
||
float temp_threshold = (float)array[index++] * 1.0f / 10.f; //温度阈值
|
||
float temp_offset = (float)array[index++] * 1.0f / 10.f; //温控偏移量
|
||
value = array[index++]; //跳过设备状态
|
||
pTemperature->TempHighAlarm = array[index++]; //高温告警
|
||
pTemperature->HumidityHighAlarm = array[index++]; //高湿度告警
|
||
pTemperature->TempLowAlarm = array[index++]; //低温告警
|
||
}
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
|
||
//下面是模拟发送进度
|
||
//for (int i = 0; i < array.size(); ++i)
|
||
//{
|
||
// qDebug() << "Worker处理索引:" << i << "值:" << array[i]
|
||
// << "线程:" << QThread::currentThread();
|
||
// //QThread::msleep(100); // 模拟耗时处理
|
||
// emit progress(i);
|
||
//}
|
||
|
||
qDebug() << "Worker thread finished";
|
||
emit finished();
|
||
}
|
||
|
||
MainWindow::MainWindow(QWidget *parent)
|
||
: QMainWindow(parent)
|
||
, ui(new Ui::MainWindow)
|
||
,m_pModbus(nullptr)
|
||
,m_pTimer(nullptr)
|
||
,m_pSettings(nullptr)
|
||
,m_bInitializeModbus(false)
|
||
|
||
{
|
||
ui->setupUi(this);
|
||
|
||
m_version= "1.0.0";
|
||
m_modbus_type = 1;
|
||
m_modbus_ip = "127.0.0.1";
|
||
m_modbus_port = 502;
|
||
m_modbus_com = "COM1";
|
||
m_modbus_baund = 9600;
|
||
m_modbus_data = 1;
|
||
m_modbus_parity = 0;
|
||
m_modbus_stop = 1;
|
||
m_modbus_slave_id = 1;
|
||
|
||
InitializeUI();
|
||
}
|
||
|
||
MainWindow::~MainWindow()
|
||
{
|
||
if (m_pModbus)
|
||
{
|
||
modbus_close(m_pModbus);
|
||
modbus_free(m_pModbus);
|
||
m_pModbus = nullptr;
|
||
}
|
||
|
||
for (SlaveItem* ptr : m_SlaveData)
|
||
{
|
||
delete ptr;
|
||
ptr = nullptr;
|
||
}
|
||
m_SlaveData.clear();
|
||
|
||
if (m_pSettings!=nullptr)
|
||
{
|
||
delete m_pSettings;
|
||
m_pSettings = nullptr;
|
||
}
|
||
delete ui;
|
||
}
|
||
|
||
void MainWindow::getConfiguration(QString iniFilePath)
|
||
{
|
||
m_pSettings = new QSettings(iniFilePath, QSettings::IniFormat);
|
||
|
||
m_version = m_pSettings->value("version/ver").toString();
|
||
m_modbus_type = m_pSettings->value("modbus/type").toInt();
|
||
|
||
m_modbus_ip = m_pSettings->value("modbus/ip").toString();
|
||
m_modbus_port = m_pSettings->value("modbus/port").toInt();
|
||
|
||
m_modbus_com = m_pSettings->value("modbus/com").toString();
|
||
m_modbus_baund = m_pSettings->value("modbus/baund").toInt();
|
||
m_modbus_data = m_pSettings->value("modbus/data").toInt();
|
||
m_modbus_parity = m_pSettings->value("modbus/parity").toInt();
|
||
m_modbus_stop = m_pSettings->value("modbus/stop").toInt();
|
||
|
||
m_modbus_slave_id = m_pSettings->value("slaves/slave_id").toInt();
|
||
|
||
QString app = QString("slave_%1/function_code_counts").arg(m_modbus_slave_id);
|
||
|
||
int functions_count = m_pSettings->value(app).toInt();
|
||
for(int i=0; i<functions_count; i++)
|
||
{
|
||
QString app2 = QString("slave_%1_function_%2").arg(m_modbus_slave_id).arg(i);
|
||
|
||
#ifndef _DEBUG_VSPD_
|
||
unsigned short equipmentCode = m_pSettings->value(app2 + "/equipment_code").toInt();
|
||
equipmentCode = equipmentCode << 9;
|
||
equipmentCode &= 0xFE00; // 二进制掩码: 1111 1110 0000 0000(保留高7位)
|
||
|
||
QStringList sn_list = m_pSettings->value(app2 + "/serial_number").toString().split(":", Qt::SkipEmptyParts);
|
||
QStringList quantity_list = m_pSettings->value(app2 + "/read_quantity").toString().split(":", Qt::SkipEmptyParts);
|
||
|
||
assert(sn_list.size() == quantity_list.size());
|
||
|
||
int j = 0;
|
||
foreach(const QString sn, sn_list)
|
||
{
|
||
SlaveItem* pSlaveData = new SlaveItem();
|
||
pSlaveData->id = m_modbus_slave_id;
|
||
pSlaveData->function_code = m_pSettings->value(app2 + "/function_code").toInt();
|
||
pSlaveData->interval = m_pSettings->value(app2 + "/interval").toInt();
|
||
|
||
unsigned short serialNumber = sn.toInt();
|
||
|
||
serialNumber &= 0x01FF; // 二进制掩码: 0000 0001 1111 1111(保留低9位)
|
||
|
||
// 组合操作:高7位左移9位,低9位直接填充
|
||
unsigned short address = pSlaveData->start_address = (equipmentCode ) | serialNumber; // 关键位操作
|
||
|
||
pSlaveData->start_address = address;
|
||
pSlaveData->quantity = quantity_list.at(j).toInt();
|
||
m_SlaveData.push_back(pSlaveData);
|
||
j++;
|
||
}
|
||
#else
|
||
SlaveItem* pSlaveData = new SlaveItem();
|
||
pSlaveData->id = m_modbus_slave_id;
|
||
pSlaveData->function_code = m_pSettings->value(app2 + "/function_code").toInt();
|
||
pSlaveData->interval = m_pSettings->value(app2 + "/interval").toInt();
|
||
pSlaveData->quantity = m_pSettings->value(app2 + "/quantity").toInt();
|
||
pSlaveData->start_address = m_pSettings->value(app2 + "/start_addr").toInt();
|
||
m_SlaveData.push_back(pSlaveData);
|
||
#endif
|
||
}
|
||
}
|
||
|
||
bool MainWindow::InitializeUI()
|
||
{
|
||
#ifndef NDEBUG
|
||
//this->showMaximized();
|
||
#endif
|
||
|
||
ui->statusbar->showMessage(tr("Ready"), 0);
|
||
|
||
//初始化窗口边框
|
||
QWidget *centralWidget = this->centralWidget(); // new QWidget(this);
|
||
QGridLayout *mainLayout = new QGridLayout(centralWidget);
|
||
|
||
this->setStyleSheet("background-color: white;");
|
||
//this->setProperty("canMove",true);
|
||
|
||
//设置窗体透明
|
||
this->setAttribute(Qt::WA_TranslucentBackground, true);
|
||
//设置无边框
|
||
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
|
||
//实例阴影shadow
|
||
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
|
||
//设置阴影距离
|
||
shadow->setOffset(0, 0);
|
||
//设置阴影颜色
|
||
shadow->setColor(QColor(44,44,44));
|
||
//设置阴影圆角
|
||
shadow->setBlurRadius(16);
|
||
//给嵌套QDialog设置阴影
|
||
ui->centralwidget->setGraphicsEffect(shadow);
|
||
|
||
//给垂直布局器设置边距(此步很重要, 设置宽度为阴影的宽度)
|
||
//->setMargin(12);
|
||
|
||
//温度
|
||
m_pTemperaturePanel = new CustomDisplayPanel(this);
|
||
m_pTemperaturePanel->setImage(":/icons/main_temp.png");
|
||
QStringList l1{tr("Online"),tr("Temperature"),tr("Humidity")};
|
||
m_pTemperaturePanel->setLableCount(l1.count());
|
||
m_pTemperaturePanel->setTableRowsCount(10);
|
||
m_pTemperaturePanel->setLables(l1);
|
||
m_pTemperaturePanel->setRowItems(QStringList{tr("露点"),tr("DO"),tr("DI1"),tr("DI2"),tr("高温"),tr("低温"),tr("高湿")});
|
||
m_pTemperaturePanel->setMainLabel(tr("Temp&RH"));
|
||
m_pTemperaturePanel->Build();
|
||
mainLayout->addWidget(m_pTemperaturePanel, 0, 0);
|
||
|
||
//电源
|
||
m_pPowerPanel = new CustomDisplayPanel(this);
|
||
m_pPowerPanel->setImage(":/icons/main_power.png");
|
||
QStringList l2{QStringList{tr("Online"),tr("Input Voltage"),tr("Output Voltage"),tr("Output Current"),tr("Module Temp.")}};
|
||
m_pPowerPanel->setLableCount(l2.count());
|
||
m_pPowerPanel->setTableRowsCount(10);
|
||
m_pPowerPanel->setLables(l2);
|
||
m_pPowerPanel->setRowItems(QStringList{tr("露点"),tr("DO"),tr("DI1"),tr("DI2"),tr("高温"),tr("低温"),tr("高湿")});
|
||
m_pPowerPanel->setMainLabel(tr("Power"));
|
||
m_pPowerPanel->Build();
|
||
mainLayout->addWidget(m_pPowerPanel, 0, 1);
|
||
|
||
//电池
|
||
m_pBatteryPanel = new CustomDisplayPanel(this);
|
||
m_pBatteryPanel->setImage(":/icons/main_battery.png");
|
||
QStringList l3{QStringList{tr("Online"),tr("SOC"),tr("SOH"),tr("Group Voltage"),tr("Cell V.Avg"),tr("Cell V.Max"),tr("Cell V.Min")}};
|
||
m_pBatteryPanel->setLableCount(l3.count());
|
||
m_pBatteryPanel->setTableRowsCount(10);
|
||
m_pBatteryPanel->setLables(l3);
|
||
m_pBatteryPanel->setRowItems(QStringList{tr("露点"),tr("DO"),tr("DI1"),tr("DI2"),tr("高温"),tr("低温"),tr("高湿")});
|
||
m_pBatteryPanel->setMainLabel(tr("Battery Pack"));
|
||
m_pBatteryPanel->Build();
|
||
mainLayout->addWidget(m_pBatteryPanel, 0, 2);
|
||
|
||
//空调
|
||
m_pACPanel = new CustomDisplayPanel(this);
|
||
m_pACPanel->setImage(":/icons/main_ac.png");
|
||
QStringList l4{QStringList{tr("Online"),tr("Machine"),tr("IFM"),tr("Compressor"),tr("OFM"),tr("Discharge Temp."),tr("Room Temp.")}};
|
||
m_pACPanel->setLableCount(l4.count());
|
||
m_pACPanel->setTableRowsCount(10);
|
||
m_pACPanel->setLables(l4);
|
||
m_pACPanel->setRowItems(QStringList{tr("露点"),tr("DO"),tr("DI1"),tr("DI2"),tr("高温"),tr("低温"),tr("高湿")});
|
||
m_pACPanel->setMainLabel(tr("HVACR"));
|
||
m_pACPanel->Build();
|
||
mainLayout->addWidget(m_pACPanel, 0, 3);
|
||
|
||
//交流配电
|
||
m_pInverterPanel = new CustomDisplayPanel(this);
|
||
m_pInverterPanel->setImage(":/icons/main_invertor.png");
|
||
QStringList l5{QStringList{tr("Online"),tr("Input Voltage"),tr("Input Current"),tr("Module Temp.")}};
|
||
m_pInverterPanel->setLableCount(l5.count());
|
||
m_pInverterPanel->setTableRowsCount(10);
|
||
m_pInverterPanel->setLables(l5);
|
||
m_pInverterPanel->setRowItems(QStringList{tr("露点"),tr("DO"),tr("DI1"),tr("DI2"),tr("高温"),tr("低温"),tr("高湿")});
|
||
m_pInverterPanel->setMainLabel(tr("AC Power"));
|
||
m_pInverterPanel->Build();
|
||
mainLayout->addWidget(m_pInverterPanel, 1, 0);
|
||
|
||
//PV太阳能
|
||
m_pPVPanel = new CustomDisplayPanel(this);
|
||
m_pPVPanel->setImage(":/icons/main_pv.png");
|
||
QStringList l6{QStringList{tr("Online"),tr("Output Voltage"),tr("Output Current"),tr("Module Temp.")}};
|
||
m_pPVPanel->setLableCount(l6.count());
|
||
m_pPVPanel->setTableRowsCount(10);
|
||
m_pPVPanel->setLables(l6);
|
||
m_pPVPanel->setRowItems(QStringList{tr("露点"),tr("DO"),tr("DI1"),tr("DI2"),tr("高温"),tr("低温"),tr("高湿")});
|
||
m_pPVPanel->setMainLabel(tr("PV Module"));
|
||
m_pPVPanel->Build();
|
||
mainLayout->addWidget(m_pPVPanel, 1, 1);
|
||
|
||
//门禁
|
||
m_pHomePanel = new CustomDisplayPanel(this);
|
||
m_pHomePanel->setImage(":/icons/main_cab.png");
|
||
QStringList l7{QStringList{tr("Online"),tr("Output Voltage"),tr("Output Current"),tr("Module Temp.")}};
|
||
m_pHomePanel->setLableCount(l7.count());
|
||
m_pHomePanel->setTableRowsCount(10);
|
||
m_pHomePanel->setLables(l7);
|
||
m_pHomePanel->setRowItems(QStringList{tr("露点"),tr("DO"),tr("DI1"),tr("DI2"),tr("高温"),tr("低温"),tr("高湿")});
|
||
m_pHomePanel->setMainLabel(tr("Sensors"));
|
||
m_pHomePanel->Build();
|
||
mainLayout->addWidget(m_pHomePanel, 1, 2);
|
||
|
||
//告警
|
||
m_pAlarmPanel = new CustomWarningPanel(this);
|
||
m_pAlarmPanel->setImage(":/icons/main_alarm.png");
|
||
QStringList l8{QStringList{tr("Warning Board")}};
|
||
m_pAlarmPanel->setLableCount(l8.count());
|
||
m_pAlarmPanel->setTableRowsCount(10);
|
||
m_pAlarmPanel->setTableRolCount(3);
|
||
m_pAlarmPanel->setLables(l8);
|
||
m_pAlarmPanel->setRowItems(QStringList{tr("")});
|
||
m_pAlarmPanel->setMainLabel(tr("Warning"));
|
||
m_pAlarmPanel->Build();
|
||
mainLayout->addWidget(m_pAlarmPanel, 1, 3);
|
||
|
||
// 设置布局的间距和边距
|
||
mainLayout->setSpacing(5);
|
||
//mainLayout->setContentsMargins(10, 40, 10, 10); // 顶部留出工具栏空间
|
||
//setCentralWidget(centralWidget);
|
||
|
||
QToolBar *toolBar = new QToolBar(this);
|
||
toolBar->setMovable(false);
|
||
toolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
|
||
toolBar->setIconSize(QSize(64,64));
|
||
// 设置按钮悬停效果(通过样式表)
|
||
// toolBar->setStyleSheet(
|
||
// "QToolButton { background: transparent; border: none; }"
|
||
// "QToolButton:hover { background: #e0e0e0; border-radius: 4px; }"
|
||
// );
|
||
|
||
// 添加工具栏按钮
|
||
QAction *actionRead = new QAction(QIcon(":/icons/modular.png"), tr("Read"), this);
|
||
actionRead->setToolTip(tr("Read Data"));
|
||
|
||
QAction *actionSetting = new QAction(QIcon(":/icons/setting.png"), tr("Setting"), this);
|
||
actionSetting->setToolTip(tr("Setting Modbud Port"));
|
||
|
||
QAction *actionClose = new QAction(QIcon(":/icons/close.png"), tr("Close"), this);
|
||
actionClose->setToolTip(tr("Close Application"));
|
||
|
||
//添加logo
|
||
QLabel *logoLabel = new QLabel(this);
|
||
QPixmap pixmap(":/icons/logo-en.png"); // 替换为实际图片路径
|
||
logoLabel->setPixmap(pixmap.scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
||
toolBar->addWidget(logoLabel);
|
||
toolBar->addSeparator();
|
||
|
||
//添加按钮
|
||
toolBar->addAction(actionRead);
|
||
toolBar->addSeparator();
|
||
|
||
// 添加间隔控件
|
||
QWidget *spacer = new QWidget();
|
||
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
||
toolBar->addWidget(spacer);
|
||
|
||
// 添加中央标签
|
||
QLabel *label = new QLabel(tr("Integrated Power Cabinet Monitoring"), this);
|
||
|
||
// // 加载字体
|
||
// int fontId = QFontDatabase::addApplicationFont(":/fonts/Alimama_DongFangDaKai_Regular.ttf");
|
||
// if (fontId == -1)
|
||
// {
|
||
// ui->statusbar->showMessage(tr("Failed to load font"));
|
||
// }
|
||
//QFont customFont("Alimama DongFangDaKai", 48);
|
||
//customFont.setBold(true);
|
||
//label->setFont(customFont);
|
||
//label->setStyleSheet("color: #2E86C1;");
|
||
label->setStyleSheet(
|
||
"QLabel {"
|
||
//" font-family: 'Alimama DongFangDaKai';" // 需确保字体已加载
|
||
" font-family: 'Arial';" // 需确保字体已加载
|
||
" font-size: 52px;"
|
||
" color: #2E86C1;"
|
||
" font-weight: bold;"
|
||
" font-style: black;"
|
||
"}"
|
||
);
|
||
toolBar->addWidget(label);
|
||
|
||
QWidget *spacerWidget = new QWidget();
|
||
spacerWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
||
toolBar->addWidget(spacerWidget);
|
||
|
||
toolBar->addAction(actionSetting);
|
||
toolBar->addSeparator();
|
||
|
||
toolBar->addAction(actionClose);
|
||
|
||
// 将工具栏定位到窗口顶部·
|
||
addToolBar(Qt::TopToolBarArea, toolBar);
|
||
|
||
connect(actionClose, &QAction::triggered, this, &QMainWindow::close);
|
||
connect(actionSetting, &QAction::triggered, this, &MainWindow::SettingSerialPort);
|
||
connect(actionRead, &QAction::triggered, this, &MainWindow::ReadSerialPortData);
|
||
|
||
return true;
|
||
}
|
||
|
||
bool MainWindow::InitializeModbus()
|
||
{
|
||
if (m_bInitializeModbus)
|
||
return true;
|
||
|
||
QString appDir = QCoreApplication::applicationDirPath();
|
||
QString iniFilePath = appDir + QString::fromStdString("/emsshower.ini");
|
||
|
||
getConfiguration(iniFilePath);
|
||
|
||
m_bInitializeModbus = true;
|
||
switch (m_modbus_type)
|
||
{
|
||
case 0:
|
||
return InitializeTcp();
|
||
break;
|
||
|
||
default:
|
||
case 1:
|
||
return InitializeRtu();
|
||
break;
|
||
}
|
||
}
|
||
|
||
bool MainWindow::InitializeRtu()
|
||
{
|
||
if(m_pModbus)
|
||
{
|
||
modbus_close(m_pModbus);
|
||
m_pModbus = nullptr;
|
||
}
|
||
|
||
std::string com = std::string("\\\\.\\") + m_modbus_com.toStdString();
|
||
|
||
m_pModbus = modbus_new_rtu(com.c_str(), m_modbus_baund, 'N', m_modbus_data, m_modbus_stop);
|
||
|
||
int slave_id = m_modbus_slave_id;
|
||
//modbus_set_debug(m_pModbus, true);
|
||
modbus_set_slave(m_pModbus, slave_id); //设置modbus从机地址
|
||
modbus_rtu_set_serial_mode(m_pModbus, MODBUS_RTU_RS485);
|
||
|
||
int rc = modbus_connect(m_pModbus);
|
||
if (rc == -1)
|
||
return false;
|
||
|
||
struct timeval t;
|
||
t.tv_sec=0;
|
||
t.tv_usec=1000000; //设置modbus超时时间为1000毫秒
|
||
modbus_set_response_timeout(m_pModbus, t.tv_usec,t.tv_usec);
|
||
|
||
return true;
|
||
}
|
||
|
||
bool MainWindow::InitializeTcp()
|
||
{
|
||
if(m_pModbus)
|
||
{
|
||
modbus_close(m_pModbus);
|
||
m_pModbus = nullptr;
|
||
}
|
||
|
||
std::string ip = m_modbus_ip.toStdString();
|
||
int port = m_modbus_port;
|
||
m_pModbus = modbus_new_tcp(ip.c_str(), port); //由于是tcp client连接,在同一个程序中相同的端口可以连接多次。
|
||
if (m_pModbus == nullptr)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
int slave_id = m_modbus_slave_id;
|
||
modbus_set_slave(m_pModbus, slave_id); //从机地址
|
||
|
||
int rc = modbus_connect(m_pModbus);
|
||
if (rc == -1)
|
||
return false;
|
||
|
||
struct timeval t;
|
||
t.tv_sec=0;
|
||
t.tv_usec=1000000; //设置modbus超时时间为1000毫秒
|
||
modbus_set_response_timeout(m_pModbus, t.tv_usec,t.tv_usec);
|
||
|
||
return true;
|
||
}
|
||
|
||
bool MainWindow::readRegister(int addr,int nb,uint16_t* dest)
|
||
{
|
||
if(!InitializeModbus()) //这里有问题,如果是虚拟串口,连接的通常会返回成功
|
||
{
|
||
ui->statusbar->showMessage(tr("Failed to open Modbus device,Check modbus connection please!")); //打开MODBUS设备失败,请检查设备连接情况!"));
|
||
return false;
|
||
}
|
||
|
||
for (auto it = m_SlaveData.begin(); it != m_SlaveData.end(); ++it)
|
||
{
|
||
SlaveItem* pItem = (SlaveItem*)(*it);
|
||
|
||
uint16_t* tab_reg = new uint16_t[pItem->quantity];
|
||
int regs = modbus_read_registers(m_pModbus, pItem->start_address, pItem->quantity, tab_reg);
|
||
|
||
QVector<uint16_t> registers;
|
||
for (int i = 0; i < regs; ++i)
|
||
{
|
||
registers.push_back(tab_reg[i]);
|
||
}
|
||
|
||
delete []tab_reg;
|
||
|
||
DeviceData* pDevice = nullptr;
|
||
|
||
if (pItem->start_address == TH08D_TEMPERATURE_EQUIPMENT_81_00_09
|
||
|| pItem->start_address == TH08D_TEMPERATURE_EQUIPMENT_81_09_06)
|
||
pDevice = new TemperatureData();
|
||
|
||
startAsyncProcess(registers,pItem->id,pItem->start_address, pItem->quantity, pDevice);
|
||
|
||
if (pItem->start_address == TH08D_TEMPERATURE_EQUIPMENT_81_00_09
|
||
|| pItem->start_address == TH08D_TEMPERATURE_EQUIPMENT_81_09_06)
|
||
{
|
||
TemperatureData* pData = (TemperatureData*)pDevice;
|
||
assert(pData);
|
||
OpenJson json;
|
||
if(CreateJson(pData,json))
|
||
{
|
||
if(pData->bDecodeTemp)
|
||
m_pTemperaturePanel->UpdateData(json);
|
||
if(pData->bDecodeAlarm)
|
||
m_pAlarmPanel->UpdateAlarm(json);
|
||
}
|
||
else
|
||
{
|
||
ui->statusbar->showMessage(tr("Failed to decode temperaure data")); //解析温度数据失败!"));
|
||
}
|
||
}
|
||
|
||
qDebug() << "同步读取地址" << pItem->start_address;
|
||
delete pDevice;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void MainWindow::startAsyncProcess(const QVector<uint16_t>& array,int slave_id,int start_addr,int quantity, DeviceData* pDevice)
|
||
{
|
||
QThread* thread = new QThread;
|
||
DecodeWorker* worker = new DecodeWorker;
|
||
worker->moveToThread(thread);
|
||
|
||
worker->setParameters(array, slave_id, start_addr, quantity, pDevice);
|
||
|
||
// 连接线程启动信号和槽函数
|
||
connect(this, &MainWindow::startProcessing, worker, &DecodeWorker::processArray);
|
||
|
||
// 工作完成后退出线程
|
||
connect(worker, &DecodeWorker::finished, thread, &QThread::quit);
|
||
connect(worker, &DecodeWorker::finished, worker, &DecodeWorker::deleteLater);
|
||
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
|
||
|
||
//// 可选:连接进度信号更新UI
|
||
//connect(worker, &DecodeWorker::progress, this, [](int index)
|
||
//{
|
||
// qDebug() << "进度更新,当前处理到索引:" << index;
|
||
//});
|
||
|
||
thread->start();
|
||
|
||
QEventLoop loop;
|
||
QObject::connect(worker, &DecodeWorker::finished, &loop, &QEventLoop::quit);
|
||
|
||
// 发射信号传递参数
|
||
emit startProcessing(array, slave_id, start_addr, quantity, pDevice);
|
||
|
||
// 阻塞,等待线程结束
|
||
loop.exec();
|
||
|
||
qDebug() << "同步读取完成";
|
||
}
|
||
|
||
|
||
void MainWindow::ReadSerialPortData()
|
||
{
|
||
if (!m_pTimer)
|
||
m_pTimer = new QTimer(this);
|
||
|
||
if (m_pTimer->isActive())
|
||
{
|
||
m_pTimer->stop();
|
||
ui->statusbar->showMessage(tr("Stop Reading"));
|
||
}
|
||
else
|
||
{
|
||
m_pTimer->setInterval(10000); // 10秒
|
||
connect(m_pTimer, &QTimer::timeout, this, &MainWindow::onTimeout);
|
||
ui->statusbar->showMessage(tr("Begin Readding"));
|
||
m_pTimer->start();
|
||
onTimeout();
|
||
}
|
||
}
|
||
|
||
void MainWindow::onTimeout()
|
||
{
|
||
readRegister(0,0,0);
|
||
}
|
||
|
||
void MainWindow::SettingSerialPort()
|
||
{
|
||
FormSerialPortSettingDialog* dlg = new FormSerialPortSettingDialog(this);
|
||
dlg->setWindowFlags(dlg->windowFlags()&~(Qt::WindowMinMaxButtonsHint|Qt::WindowContextHelpButtonHint));
|
||
dlg->exec();
|
||
|
||
// if(dlg->exec() == QDialog::Accepted)
|
||
// QMessageBox::information(this, "OK Clicked", "Button 1 was clicked!");
|
||
// else
|
||
// QMessageBox::information(this, "Cancel Clicked", "Cancel was clicked!");
|
||
}
|
||
|
||
bool MainWindow::CreateJson(DeviceData* pData,OpenJson& json)
|
||
{
|
||
int data_type = pData->m_device_type;
|
||
if (data_type == 81) //根据协议定义的设备id进行分类,参考DeviceData定义
|
||
{
|
||
TemperatureData* pTempData = (TemperatureData*)pData;
|
||
if (pTempData->bDecodeTemp)
|
||
{
|
||
json["panel_type"] = CustomDisplayPanel::PANEL_TEMPERATURE;
|
||
|
||
auto& nodeLabel = json["text_panel"];
|
||
nodeLabel[0]["value"] = pTempData->m_device_online_state;
|
||
nodeLabel[0]["title"] = "Online";
|
||
|
||
nodeLabel[1]["value"] = pTempData->TempValue;
|
||
nodeLabel[1]["title"] = "Temp(℃)";
|
||
|
||
nodeLabel[2]["value"] = pTempData->HumidityValue;
|
||
nodeLabel[2]["title"] = "RH (%) ";
|
||
|
||
auto& nodeTable = json["table"];
|
||
int i = 0;
|
||
|
||
nodeTable[i]["value"] = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toStdString();
|
||
nodeTable[i]["signal"] = "Time";
|
||
|
||
i++;
|
||
nodeTable[i]["value"] = pTempData->DewPointValue;
|
||
nodeTable[i]["signal"] = "Dew Point";
|
||
|
||
i++;
|
||
nodeTable[i]["value"] = pTempData->DO;
|
||
nodeTable[i]["signal"] = "DO";
|
||
|
||
i++;
|
||
nodeTable[i]["value"] = pTempData->DI1;
|
||
nodeTable[i]["signal"] = "DI1";
|
||
|
||
i++;
|
||
nodeTable[i]["value"] = pTempData->DI2;
|
||
nodeTable[i]["signal"] = "DI2";
|
||
|
||
std::string a = json.encode();
|
||
|
||
qDebug() << QString::fromStdString(a);
|
||
}
|
||
|
||
if (pTempData->bDecodeAlarm)
|
||
{
|
||
json["panel_type"] = CustomDisplayPanel::PANEL_ALARM;
|
||
auto& nodeTable = json["alarm"];
|
||
|
||
int i = -1;
|
||
if(pTempData->TempHighAlarm != 0)
|
||
{
|
||
i++;
|
||
nodeTable[i]["time"] = QDateTime::currentDateTime().toString("MM-dd HH:mm:ss").toStdString();
|
||
nodeTable[i]["signal"] = "High Temperature";
|
||
nodeTable[i]["value"] = pTempData->TempHighAlarm;
|
||
}
|
||
|
||
if(pTempData->TempLowAlarm != 0)
|
||
{
|
||
i++;
|
||
nodeTable[i]["time"] = QDateTime::currentDateTime().toString("MM-dd HH:mm:ss").toStdString();
|
||
nodeTable[i]["signal"] = "High Temperature";
|
||
nodeTable[i]["value"] = pTempData->TempLowAlarm;
|
||
}
|
||
|
||
if(pTempData->HumidityHighAlarm != 0)
|
||
{
|
||
i++;
|
||
nodeTable[i]["time"] = QDateTime::currentDateTime().toString("MM-dd HH:mm:ss").toStdString();
|
||
nodeTable[i]["signal"] = "High Humidity";
|
||
nodeTable[i]["value"] = pTempData->HumidityHighAlarm;
|
||
}
|
||
|
||
std::string a = json.encode();
|
||
|
||
qDebug() << QString::fromStdString(a);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|