[Add] firefix some bugs

This commit is contained in:
Carl 2024-11-09 17:25:09 +08:00
parent 525d6adb0c
commit a13d1e4a98
7 changed files with 231 additions and 203 deletions

View File

@ -12,12 +12,14 @@
#include <windef.h> #include <windef.h>
#include <QTranslator> #include <QTranslator>
#include <QThread> #include <QThread>
#include <QMetaType>
DialogLogin::DialogLogin(QWidget *parent) DialogLogin::DialogLogin(QWidget *parent)
: QDialog(parent) : QDialog(parent)
, ui(new Ui::DialogLogin) , ui(new Ui::DialogLogin)
{ {
ui->setupUi(this); ui->setupUi(this);
qRegisterMetaType<QList<int>>("QList<int>");
init(); init();
serialInit(); serialInit();
mainwindowInit(); mainwindowInit();
@ -95,18 +97,22 @@ void DialogLogin::init()
connect(m_ModbusRTU, &ModbusRTU::sig_showModbusData_base_info_list_47_49_51_53_55, m_MainWindow, &MainWindow::slot_pageHomeBaseInfo47_49_51_53_55); connect(m_ModbusRTU, &ModbusRTU::sig_showModbusData_base_info_list_47_49_51_53_55, m_MainWindow, &MainWindow::slot_pageHomeBaseInfo47_49_51_53_55);
//写寄存器的信号与槽的连接 //写寄存器的信号与槽的连接
connect(m_MainWindow, &MainWindow::write_regs_signals, m_ModbusRTU, &ModbusRTU::write_regs_slot, Qt::QueuedConnection); connect(m_MainWindow, &MainWindow::write_regs_signals, m_ModbusRTU, &ModbusRTU::write_regs_slot);
//读寄存器的信号与槽的连接 //读寄存器的信号与槽的连接
connect(m_MainWindow, &MainWindow::read_regs_signals, m_ModbusRTU, &ModbusRTU::read_regs_slot, Qt::QueuedConnection); connect(m_MainWindow, &MainWindow::read_regs_signals, m_ModbusRTU, &ModbusRTU::read_regs_slot);
connect(m_ModbusRTU, &ModbusRTU::sig_showReadHoldingRegs, m_MainWindow, &MainWindow::slot_pageParaSetShow); connect(m_ModbusRTU, &ModbusRTU::sig_showReadHoldingRegs, m_MainWindow, &MainWindow::slot_pageParaSetShow);
//主窗口关闭时,关闭线程 //主窗口关闭时,关闭线程
connect(m_MainWindow, &MainWindow::closeThread, this, [=]{ connect(m_MainWindow, &MainWindow::quitThread, this, [=]{
qDebug() << "关闭线程"; qDebug() << "关闭线程";
m_ModbusRTU->quit(); m_ModbusRTU->quit();
}); });
connect(m_MainWindow, &MainWindow::closeThread, m_ModbusRTU, &ModbusRTU::slot_pauseThread);
connect(m_MainWindow, &MainWindow::openThread, m_ModbusRTU, &ModbusRTU::slot_resumeThread);
// ui->btnConnect->setStyleSheet("QPushButton { background-color: lightgray; color: black; border: none; }"); // ui->btnConnect->setStyleSheet("QPushButton { background-color: lightgray; color: black; border: none; }");
//主窗口接受语言选择number的信号与槽的连接 //主窗口接受语言选择number的信号与槽的连接
@ -206,6 +212,11 @@ void DialogLogin::slot_comChangeStatus(QString name, bool flag)
void DialogLogin::slot_btnConnect_clicked() void DialogLogin::slot_btnConnect_clicked()
{ {
ini->setValue("Serial/BaudRate", ui->comboBoxBaudRate->currentIndex());
ini->setValue("Serial/DataBits", ui->comboBoxDataBits->currentIndex());
ini->setValue("Serial/StopBits", ui->comboBoxStopBits->currentIndex());
ini->setValue("Serial/Parity", ui->comboBoxParity->currentIndex());
ini->sync();
// BCUNumbers = ui->comboBoxBCUQuantity->currentText().toInt(); // BCUNumbers = ui->comboBoxBCUQuantity->currentText().toInt();
QString language = ui->comboBoxLanguage->currentText(); QString language = ui->comboBoxLanguage->currentText();
@ -234,11 +245,7 @@ void DialogLogin::slot_btnConnect_clicked()
emit sendBCUNumberSignals(BCUNumbers); emit sendBCUNumberSignals(BCUNumbers);
connect(this, &DialogLogin::sendBCUNumberSignals, m_MainWindow, &MainWindow::slot_getBcuNumberFromDialoglogin); connect(this, &DialogLogin::sendBCUNumberSignals, m_MainWindow, &MainWindow::slot_getBcuNumberFromDialoglogin);
ini->setValue("Serial/BaudRate", ui->comboBoxBaudRate->currentIndex());
ini->setValue("Serial/DataBits", ui->comboBoxDataBits->currentIndex());
ini->setValue("Serial/StopBits", ui->comboBoxStopBits->currentIndex());
ini->setValue("Serial/Parity", ui->comboBoxParity->currentIndex());
ini->sync();
if(m_Serial->getAvailablePort().size() == 0) { if(m_Serial->getAvailablePort().size() == 0) {
if(ini->value("System/Language") == MainWindow::LANGUAGE_ENG) { if(ini->value("System/Language") == MainWindow::LANGUAGE_ENG) {

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>430</width> <width>362</width>
<height>539</height> <height>444</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -33,7 +33,7 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout" stretch="4,7,1,2,3"> <layout class="QVBoxLayout" name="verticalLayout" stretch="4,7,1,3,3">
<item> <item>
<widget class="QLabel" name="label_bluesunLogo"> <widget class="QLabel" name="label_bluesunLogo">
<property name="sizePolicy"> <property name="sizePolicy">
@ -346,10 +346,15 @@
</property> </property>
<property name="font"> <property name="font">
<font> <font>
<family>黑体</family> <family>Microsoft YaHei UI</family>
<pointsize>10</pointsize> <pointsize>12</pointsize>
<italic>false</italic>
<bold>true</bold>
</font> </font>
</property> </property>
<property name="styleSheet">
<string notr="true">font: 700 12pt &quot;Microsoft YaHei UI&quot;;</string>
</property>
<property name="text"> <property name="text">
<string>Connect</string> <string>Connect</string>
</property> </property>
@ -381,7 +386,7 @@
<property name="font"> <property name="font">
<font> <font>
<family>幼圆</family> <family>幼圆</family>
<pointsize>11</pointsize> <pointsize>12</pointsize>
</font> </font>
</property> </property>
<property name="text"> <property name="text">

View File

@ -1,6 +1,7 @@
#include "dialoglogin.h" #include "dialoglogin.h"
#include <QApplication> #include <QApplication>
#include <QTranslator> #include <QTranslator>
#include <QPersistentModelIndex>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -18,6 +19,9 @@ int main(int argc, char *argv[])
a.setStyleSheet("QToolTip { font-size: 18px; font-family: 'SimHei'; }"); a.setStyleSheet("QToolTip { font-size: 18px; font-family: 'SimHei'; }");
DialogLogin loginWidget; DialogLogin loginWidget;
loginWidget.setStyleSheet("background-color: #FFFFFF;"); loginWidget.setStyleSheet("background-color: #FFFFFF;");
qRegisterMetaType<QList<QPersistentModelIndex>>("QList<QPersistentModelIndex>");
qRegisterMetaType<QAbstractItemModel::LayoutChangeHint>("QAbstractItemModel::LayoutChangeHint");
qRegisterMetaType<QVector<int>>("QVector<int>");
loginWidget.show(); loginWidget.show();
return a.exec(); return a.exec();
} }

View File

@ -3667,6 +3667,8 @@ void MainWindow::on_pageParaSetRead_clicked()
pairs_addr.append(qMakePair(startAddress, 1)); pairs_addr.append(qMakePair(startAddress, 1));
} }
emit closeThread();
QThread::msleep(700);
int rowStart = 0; int rowStart = 0;
QList<QPair<int, int>>::iterator it; QList<QPair<int, int>>::iterator it;
for (it = pairs_addr.begin(); it!= pairs_addr.end(); ++it) { for (it = pairs_addr.begin(); it!= pairs_addr.end(); ++it) {
@ -3674,8 +3676,10 @@ void MainWindow::on_pageParaSetRead_clicked()
uint16_t result[it->second]; uint16_t result[it->second];
int tempIndex = checkRow_read[rowStart]; int tempIndex = checkRow_read[rowStart];
emit read_regs_signals(it->first, it->second, result, tempIndex); emit read_regs_signals(it->first, it->second, result, tempIndex);
QThread::msleep(80);
rowStart += it->second; rowStart += it->second;
} }
emit openThread();
rowStart = 0; rowStart = 0;
qDebug() << "read of the row" << checkRow_read; qDebug() << "read of the row" << checkRow_read;
qDebug() << "read of the address" << checkAddress_read; qDebug() << "read of the address" << checkAddress_read;
@ -3685,24 +3689,16 @@ void MainWindow::on_pageParaSetRead_clicked()
} }
} }
void MainWindow::slot_pageParaSetShow(QByteArray byteArray, int dataType, int row, int nb) void MainWindow::slot_testData(uint16_t *data)
{ {
Q_UNUSED(dataType); // qDebug("#####slot_testData data[0]:[%x] [1]:[%x] [2]:[%x] [3]:[%x] [4]:[%x] [5]:[%x] [6]:[%x] [7]:[%x] [8]:[%x] [9]:[%x] ",
QList<int> result_int; // data[0], data[1], data[2], data[3], data[4],
// data[5], data[6], data[7], data[8], data[9]);
}
bool ok; void MainWindow::slot_pageParaSetShow(int row, int nb, uint16_t *data)
for (int i = 3; i < byteArray.size(); i += 2) { {
quint8 firstByte = (i < byteArray.size())? byteArray.at(i) : 0; // qDebug() << "mainwindow:" << data[0] << data[1] << data[2] << data[3] << data[4];
quint8 secondByte = (i + 1 < byteArray.size())? byteArray.at(i + 1) : 0;
QString hexValue = QString("%1%2")
.arg(QString::number(firstByte, 16).rightJustified(2, '0'))
.arg(QString::number(secondByte, 16).rightJustified(2, '0'));
result_int += hexValue.toInt(&ok, 16);
}
// qDebug() << "test read holding regs" << result_int;
qDebug() << "the row of the start index:" << row << "the number of the setting: " << nb;
qDebug() << result_int;
int i = 0; int i = 0;
for(;i < nb; row++, i++) for(;i < nb; row++, i++)
{ {
@ -3715,14 +3711,15 @@ void MainWindow::slot_pageParaSetShow(QByteArray byteArray, int dataType, int ro
QStandardItemModel *model = qobject_cast<QStandardItemModel *>(tableView->model()); QStandardItemModel *model = qobject_cast<QStandardItemModel *>(tableView->model());
if (model) if (model)
{ {
qDebug() << "1111111111111111111";
QModelIndex index = model->index(row, 2); QModelIndex index = model->index(row, 2);
model->setData(index, result_int[i]); model->setData(index, data[i]);
qDebug() << "row " << row << " result: " << result_int[i]; qDebug() << "" << i << "个元素:"<< "row " << row << " result: " << data[i];
} }
} }
} }
} }
i = 0;
} }
void MainWindow::trubleListInit() void MainWindow::trubleListInit()
@ -3853,7 +3850,7 @@ void MainWindow::slot_pageHomeFault(QByteArray byteArray, int dataType, uint16_t
void MainWindow::closeEvent(QCloseEvent *e) void MainWindow::closeEvent(QCloseEvent *e)
{ {
emit closeThread(); emit quitThread();
QWidget::closeEvent(e); QWidget::closeEvent(e);
} }
@ -4226,11 +4223,13 @@ void MainWindow::on_pageParaSetWrite_clicked()
} }
else else
{ {
emit closeThread();
QThread::msleep(700);
for (int i=0; i<checkRow_write.size();i++) for (int i=0; i<checkRow_write.size();i++)
{ {
emit write_regs_signals(checkRow_write[i], originVector[i]); emit write_regs_signals(checkRow_write[i], originVector[i]);
QThread::msleep(100);
} }
emit openThread();
} }
// 序列化消息对象 // 序列化消息对象
std::string new_serialized_data; std::string new_serialized_data;

View File

@ -150,8 +150,9 @@ signals:
// void read_regs_input_signals_vol(int addr, int nb, uint16_t *data); // void read_regs_input_signals_vol(int addr, int nb, uint16_t *data);
// void read_regs_input_signals_temp(int addr, int nb, uint16_t *data); // void read_regs_input_signals_temp(int addr, int nb, uint16_t *data);
void row_of_bmus_index(int index); void row_of_bmus_index(int index);
void closeThread(); void closeThread();
void openThread();
void quitThread();
public: public:
void slot_getBcuNumberFromDialoglogin(int number); void slot_getBcuNumberFromDialoglogin(int number);
@ -162,7 +163,8 @@ public:
void slot_pageHomeBaseInfo57_78(QByteArray byteArray, int dataType, uint16_t *data); void slot_pageHomeBaseInfo57_78(QByteArray byteArray, int dataType, uint16_t *data);
void slot_pageHomeBaseInfo83_106(QByteArray byteArray, int dataType, uint16_t *data); void slot_pageHomeBaseInfo83_106(QByteArray byteArray, int dataType, uint16_t *data);
void slot_pageHomeBaseInfo47_49_51_53_55(QList<int> byteArray, int dataType); void slot_pageHomeBaseInfo47_49_51_53_55(QList<int> byteArray, int dataType);
void slot_pageParaSetShow(QByteArray byteArray, int dataType, int row, int nb); void slot_pageParaSetShow(int row, int nb, uint16_t *data);
void slot_testData(uint16_t *data);
void slot_pageHomeFault(QByteArray byteArray, int dataType, uint16_t *data); void slot_pageHomeFault(QByteArray byteArray, int dataType, uint16_t *data);
protected: protected:

View File

@ -4,6 +4,7 @@
#include <QDebug> #include <QDebug>
#include "modbus-private.h" #include "modbus-private.h"
#include <QMessageBox> #include <QMessageBox>
#include <QEventLoop>
ModbusRTU::ModbusRTU(QObject *parent) : QThread(parent) ModbusRTU::ModbusRTU(QObject *parent) : QThread(parent)
{ {
init(); init();
@ -25,179 +26,152 @@ void ModbusRTU::init()
void ModbusRTU::run() void ModbusRTU::run()
{ {
qDebug() << "进入线程:" << QThread::currentThread(); qDebug() << "进入线程:" << QThread::currentThread();
// QMetaObject::invokeMethod(QThread::currentThread(), "read_regs_slot", Qt::QueuedConnection);
// QMetaObject::invokeMethod(QThread::currentThread(), "write_regs_slot", Qt::QueuedConnection);
while(true) while(true)
{ {
// qDebug() << "11111111111111111111111111111111"; if (closeThread)
QThread::msleep(500); {
uint16_t base1_45[45]; QElapsedTimer time;
uint16_t base57_78[22]; time.start();
uint16_t base83_106[24]; QThread::msleep(300);
uint16_t base47[1]; uint16_t base1_45[45];
uint16_t base49[1]; uint16_t base57_78[22];
uint16_t base51[1]; uint16_t base83_106[24];
uint16_t base53[1]; uint16_t base47[1];
uint16_t base55[1]; uint16_t base49[1];
QList<int> base_list_47_49_51_53_55; uint16_t base51[1];
uint16_t sgl_vol[16]; // Buffer to store the registers uint16_t base53[1];
uint16_t sgl_tempt[16]; uint16_t base55[1];
uint16_t fault[8]; QList<int> base_list_47_49_51_53_55;
// uint16_t alarm[]; uint16_t sgl_vol[16]; // Buffer to store the registers
uint16_t sgl_tempt[16];
uint16_t fault[8];
label: int ret_base1_45 = modbus_read_input_registers(m_ModbusRTUDevice, 1, 45, base1_45); label: int ret_base1_45 = modbus_read_input_registers(m_ModbusRTUDevice, 1, 45, base1_45);
if(MODBUS_ERR == ret_base1_45) if(MODBUS_ERR == ret_base1_45)
{ {
// qDebug() << "modbus no message"; goto label;
goto label; }
} else
else {
{ QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
// qDebug() << "2222222222222222222222222222222"; emit sig_showModbusData_base_info1_45(sendArray, TypeSendData, base1_45);
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen); free(m_ModbusRTUDevice->sendMsg);
emit sig_showModbusData_base_info1_45(sendArray, TypeSendData, base1_45); QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
free(m_ModbusRTUDevice->sendMsg); emit sig_showModbusData_base_info1_45(recvArray, TypeRecvData, base1_45);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen); free(m_ModbusRTUDevice->recvMsg);
emit sig_showModbusData_base_info1_45(recvArray, TypeRecvData, base1_45);
free(m_ModbusRTUDevice->recvMsg);
// qDebug() << "in modbus:" << base1_45;
}
int ret_base47 = modbus_read_input_registers(m_ModbusRTUDevice, 47, 1, base47);
int ret_base49 = modbus_read_input_registers(m_ModbusRTUDevice, 49, 1, base49);
int ret_base51 = modbus_read_input_registers(m_ModbusRTUDevice, 51, 1, base51);
int ret_base53 = modbus_read_input_registers(m_ModbusRTUDevice, 53, 1, base53);
int ret_base55 = modbus_read_input_registers(m_ModbusRTUDevice, 55, 1, base55);
if (ret_base47 == -1 | ret_base49 == -1 | ret_base51 == -1 | ret_base53 == -1 | ret_base55 == -1)
{
return;
}
else
{
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_base_info47_49_51_53_55(sendArray, TypeSendData);
free(m_ModbusRTUDevice->sendMsg);
// QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
QByteArray recvArray;
recvArray.append(reinterpret_cast<char*>(&base47), sizeof(base47));
recvArray.append(reinterpret_cast<char*>(&base49), sizeof(base49));
recvArray.append(reinterpret_cast<char*>(&base51), sizeof(base51));
recvArray.append(reinterpret_cast<char*>(&base53), sizeof(base53));
recvArray.append(reinterpret_cast<char*>(&base55), sizeof(base55));
for (int i = 0; i < recvArray.size(); i += 2) {
uint16_t value;
memcpy(&value, recvArray.data() + i, 2);
base_list_47_49_51_53_55.append(value);
// base47_49_51_53_55[i] = value;
// qDebug() << "Value: " << value;
} }
emit sig_showModbusData_base_info_list_47_49_51_53_55(base_list_47_49_51_53_55, TypeRecvData); int ret_base47 = modbus_read_input_registers(m_ModbusRTUDevice, 47, 1, base47);
// emit sig_showModbusData_base_info47_49_51_53_55(sendArray, TypeSendData); int ret_base49 = modbus_read_input_registers(m_ModbusRTUDevice, 49, 1, base49);
free(m_ModbusRTUDevice->recvMsg); int ret_base51 = modbus_read_input_registers(m_ModbusRTUDevice, 51, 1, base51);
} int ret_base53 = modbus_read_input_registers(m_ModbusRTUDevice, 53, 1, base53);
int ret_base55 = modbus_read_input_registers(m_ModbusRTUDevice, 55, 1, base55);
if (ret_base47 == -1 | ret_base49 == -1 | ret_base51 == -1 | ret_base53 == -1 | ret_base55 == -1)
{
return;
}
else
{
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_base_info47_49_51_53_55(sendArray, TypeSendData);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray;
recvArray.append(reinterpret_cast<char*>(&base47), sizeof(base47));
recvArray.append(reinterpret_cast<char*>(&base49), sizeof(base49));
recvArray.append(reinterpret_cast<char*>(&base51), sizeof(base51));
recvArray.append(reinterpret_cast<char*>(&base53), sizeof(base53));
recvArray.append(reinterpret_cast<char*>(&base55), sizeof(base55));
int ret_base57_78 = modbus_read_input_registers(m_ModbusRTUDevice, 57, 22, base57_78); for (int i = 0; i < recvArray.size(); i += 2) {
if(MODBUS_ERR == ret_base57_78) uint16_t value;
{ memcpy(&value, recvArray.data() + i, 2);
// qDebug() << "modbus no message"; base_list_47_49_51_53_55.append(value);
goto label; }
}
else
{
// qDebug() << "4444444444444444444444444444";
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_base_info57_78(sendArray, TypeSendData, base57_78);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showModbusData_base_info57_78(recvArray, TypeRecvData, base57_78);
free(m_ModbusRTUDevice->recvMsg);
}
int ret_fault = modbus_read_input_registers(m_ModbusRTUDevice, 66, 8, fault); emit sig_showModbusData_base_info_list_47_49_51_53_55(base_list_47_49_51_53_55, TypeRecvData);
if(MODBUS_ERR == ret_fault) free(m_ModbusRTUDevice->recvMsg);
{ }
// qDebug() << "modbus no message";
goto label;
}
else
{
// qDebug() << "4444444444444444444444444444";
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showFault(sendArray, TypeSendData, fault);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showFault(recvArray, TypeRecvData,fault);
free(m_ModbusRTUDevice->recvMsg);
}
int ret_base83_106 = modbus_read_input_registers(m_ModbusRTUDevice, 83, 24, base83_106); int ret_base57_78 = modbus_read_input_registers(m_ModbusRTUDevice, 57, 22, base57_78);
// qDebug() << "#######" << base83_106[0] << base83_106[1] << base83_106[2] << base83_106[3]; if(MODBUS_ERR == ret_base57_78)
// qDebug() << "#######" << sgl_vol[0] << sgl_vol[1] << sgl_vol[2] << sgl_vol[3] << sgl_vol[4] << sgl_vol[5] << sgl_vol[6] << sgl_vol[7] {
// << sgl_vol[8] << sgl_vol[9] << sgl_vol[10] << sgl_vol[11] << sgl_vol[12] << sgl_vol[13] << sgl_vol[14] << sgl_vol[15]; goto label;
if(MODBUS_ERR == ret_base83_106) }
{ else
// qDebug() << "modbus no message"; {
goto label; QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
} emit sig_showModbusData_base_info57_78(sendArray, TypeSendData, base57_78);
else free(m_ModbusRTUDevice->sendMsg);
{ QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
// qDebug() << "5555555555555555555555555555"; emit sig_showModbusData_base_info57_78(recvArray, TypeRecvData, base57_78);
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen); free(m_ModbusRTUDevice->recvMsg);
emit sig_showModbusData_base_info83_106(sendArray, TypeSendData, base83_106); }
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showModbusData_base_info83_106(recvArray, TypeRecvData, base83_106);
free(m_ModbusRTUDevice->recvMsg);
}
// qDebug() << "the current of the BMU is :" << BMUIndex; int ret_fault = modbus_read_input_registers(m_ModbusRTUDevice, 66, 8, fault);
int volAddress = (1001 + BMUIndex * 16); if(MODBUS_ERR == ret_fault)
int tempAddress = (1601 + BMUIndex * 16); {
// qDebug() << "volAddress: " << volAddress; goto label;
// qDebug() << "tempAddress: " << tempAddress; }
// QElapsedTimer time; else
// time.start(); {
// qDebug() << "#######" << sgl_vol[0] << sgl_vol[1] << sgl_vol[2] << sgl_vol[3] << sgl_vol[4] << sgl_vol[5] << sgl_vol[6] << sgl_vol[7] QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
// << sgl_vol[8] << sgl_vol[9] << sgl_vol[10] << sgl_vol[11] << sgl_vol[12] << sgl_vol[13] << sgl_vol[14] << sgl_vol[15]; emit sig_showFault(sendArray, TypeSendData, fault);
// int milsec = time.elapsed(); free(m_ModbusRTUDevice->sendMsg);
// qDebug() <<"0.12311231" << milsec; QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showFault(recvArray, TypeRecvData,fault);
free(m_ModbusRTUDevice->recvMsg);
}
int ret_vol = modbus_read_input_registers(m_ModbusRTUDevice, volAddress, 16, sgl_vol); int ret_base83_106 = modbus_read_input_registers(m_ModbusRTUDevice, 83, 24, base83_106);
if(MODBUS_ERR == ret_vol) if(MODBUS_ERR == ret_base83_106)
{ {
// qDebug() << "modbus no message"; goto label;
goto label; }
else
{
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_base_info83_106(sendArray, TypeSendData, base83_106);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showModbusData_base_info83_106(recvArray, TypeRecvData, base83_106);
free(m_ModbusRTUDevice->recvMsg);
}
int volAddress = (1001 + BMUIndex * 16);
int tempAddress = (1601 + BMUIndex * 16);
} int ret_vol = modbus_read_input_registers(m_ModbusRTUDevice, volAddress, 16, sgl_vol);
else if(MODBUS_ERR == ret_vol)
{ {
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen); goto label;
emit sig_showModbusData(sendArray, TypeSendData, sgl_vol);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showModbusData(recvArray, TypeRecvData, sgl_vol);
free(m_ModbusRTUDevice->recvMsg);
}
// QThread::msleep(500);
int ret_tempt = modbus_read_input_registers(m_ModbusRTUDevice, tempAddress, 16, sgl_tempt);
if(MODBUS_ERR == ret_tempt)
{
// qDebug() << "modbus no message";
goto label;
}
else
{
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_sgl_tempt(sendArray, TypeSendData, sgl_tempt);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showModbusData_sgl_tempt(recvArray, TypeRecvData, sgl_tempt);
free(m_ModbusRTUDevice->recvMsg);
}
}
else
{
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData(sendArray, TypeSendData, sgl_vol);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showModbusData(recvArray, TypeRecvData, sgl_vol);
free(m_ModbusRTUDevice->recvMsg);
}
int ret_tempt = modbus_read_input_registers(m_ModbusRTUDevice, tempAddress, 16, sgl_tempt);
if(MODBUS_ERR == ret_tempt)
{
goto label;
}
else
{
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_sgl_tempt(sendArray, TypeSendData, sgl_tempt);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showModbusData_sgl_tempt(recvArray, TypeRecvData, sgl_tempt);
free(m_ModbusRTUDevice->recvMsg);
}
int milsec = time.elapsed();
qDebug() <<"the time: " << milsec;
}
} }
} }
@ -206,8 +180,9 @@ void ModbusRTU::write_regs_slot(int addr,const uint16_t data)
{ {
// qDebug() << "write:进入线程:" << QThread::currentThread(); // qDebug() << "write:进入线程:" << QThread::currentThread();
int rc = write_single_register(addr, data); int rc = write_single_register(addr, data);
this->blockSignals(true);
if(MODBUS_ERR == rc) { if(MODBUS_ERR == rc) {
qDebug() << "write######modbus no message"; qDebug() << "write######modbus no message" << modbus_strerror(errno);
return; return;
} }
} }
@ -215,18 +190,48 @@ void ModbusRTU::write_regs_slot(int addr,const uint16_t data)
void ModbusRTU::read_regs_slot(int addr, int nb, uint16_t *data, int row) void ModbusRTU::read_regs_slot(int addr, int nb, uint16_t *data, int row)
{ {
// qDebug() << "read:进入线程:" << QThread::currentThread(); // qDebug() << "read:进入线程:" << QThread::currentThread();
// uint16_t temp[10];
// uint16_t temp1[10] = {1, 2, 3, 4, 5};
// memset(temp1, 0, sizeof(temp));
qDebug() << "current add and nb" << addr << " " << nb;
int rc = read_holding_register(addr, nb, data); int rc = read_holding_register(addr, nb, data);
if(MODBUS_ERR == rc) { if(MODBUS_ERR == rc) {
qDebug() << "read modbus no message"; qDebug() << "read modbus no message" << modbus_strerror(errno);
return; return;
} }
else { else
{
// qDebug() << "read rc" << rc;
// QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
// emit sig_showReadHoldingRegs(row, nb, temp);
// free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen); QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showReadHoldingRegs(recvArray, TypeRecvData, row, nb); emit sig_showReadHoldingRegs(row, nb, data);
free(m_ModbusRTUDevice->recvMsg); free(m_ModbusRTUDevice->recvMsg);
// emit sig_testData(temp);
} }
} }
void ModbusRTU::slot_pauseThread()
{
qDebug() << "暂停线程";
closeThread = false;
// emit paused();
}
void ModbusRTU::slot_resumeThread()
{
qDebug() << "恢复线程";
// eventLoop.exit();
// eventLoop.quit();
// qDebug() << "Event loop state before quit: " << eventLoop.isRunning();
closeThread = true;
// emit resumed();
// qDebug() << "closeThread:" << closeThread;
}
void ModbusRTU::change_BMUIndex_slot(int index) void ModbusRTU::change_BMUIndex_slot(int index)
{ {
BMUIndex = index; BMUIndex = index;

View File

@ -4,6 +4,7 @@
#include <QObject> #include <QObject>
#include "modbus.h" #include "modbus.h"
#include <QThread> #include <QThread>
#include <QEventLoop>
class QTimer; class QTimer;
@ -30,9 +31,11 @@ public:
int dataBit; int dataBit;
int stopBit; int stopBit;
int BMUIndex = 0; int BMUIndex = 0;
bool closeThread = true;
public: public:
void run() override; void run() override;
QEventLoop eventLoop;
private: private:
@ -74,12 +77,15 @@ signals:
void sig_showModbusData_base_info83_106(QByteArray byteArray,int dataType, uint16_t *data); void sig_showModbusData_base_info83_106(QByteArray byteArray,int dataType, uint16_t *data);
void sig_showModbusData_base_info47_49_51_53_55(QByteArray byteArray,int dataType); void sig_showModbusData_base_info47_49_51_53_55(QByteArray byteArray,int dataType);
void sig_showModbusData_base_info_list_47_49_51_53_55(QList<int> byteArray,int dataType); void sig_showModbusData_base_info_list_47_49_51_53_55(QList<int> byteArray,int dataType);
void sig_showReadHoldingRegs(QByteArray byteArray,int dataType, int row, int nb); void sig_showReadHoldingRegs(int row, int nb, uint16_t *data);
void sig_showFault(QByteArray byteArray, int dataType, uint16_t *data); void sig_showFault(QByteArray byteArray, int dataType, uint16_t *data);
void paused();
void resumed();
public slots: public slots:
void slot_pauseThread();
void slot_resumeThread();
}; };
#endif // MODBUSRTU_H #endif // MODBUSRTU_H