[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 <QTranslator>
#include <QThread>
#include <QMetaType>
DialogLogin::DialogLogin(QWidget *parent)
: QDialog(parent)
, ui(new Ui::DialogLogin)
{
ui->setupUi(this);
qRegisterMetaType<QList<int>>("QList<int>");
init();
serialInit();
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_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_MainWindow, &MainWindow::closeThread, this, [=]{
connect(m_MainWindow, &MainWindow::quitThread, this, [=]{
qDebug() << "关闭线程";
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; }");
//主窗口接受语言选择number的信号与槽的连接
@ -206,6 +212,11 @@ void DialogLogin::slot_comChangeStatus(QString name, bool flag)
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();
QString language = ui->comboBoxLanguage->currentText();
@ -234,11 +245,7 @@ void DialogLogin::slot_btnConnect_clicked()
emit sendBCUNumberSignals(BCUNumbers);
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(ini->value("System/Language") == MainWindow::LANGUAGE_ENG) {

View File

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

View File

@ -1,6 +1,7 @@
#include "dialoglogin.h"
#include <QApplication>
#include <QTranslator>
#include <QPersistentModelIndex>
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'; }");
DialogLogin loginWidget;
loginWidget.setStyleSheet("background-color: #FFFFFF;");
qRegisterMetaType<QList<QPersistentModelIndex>>("QList<QPersistentModelIndex>");
qRegisterMetaType<QAbstractItemModel::LayoutChangeHint>("QAbstractItemModel::LayoutChangeHint");
qRegisterMetaType<QVector<int>>("QVector<int>");
loginWidget.show();
return a.exec();
}

View File

@ -3667,6 +3667,8 @@ void MainWindow::on_pageParaSetRead_clicked()
pairs_addr.append(qMakePair(startAddress, 1));
}
emit closeThread();
QThread::msleep(700);
int rowStart = 0;
QList<QPair<int, int>>::iterator 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];
int tempIndex = checkRow_read[rowStart];
emit read_regs_signals(it->first, it->second, result, tempIndex);
QThread::msleep(80);
rowStart += it->second;
}
emit openThread();
rowStart = 0;
qDebug() << "read of the row" << checkRow_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);
QList<int> result_int;
// qDebug("#####slot_testData data[0]:[%x] [1]:[%x] [2]:[%x] [3]:[%x] [4]:[%x] [5]:[%x] [6]:[%x] [7]:[%x] [8]:[%x] [9]:[%x] ",
// data[0], data[1], data[2], data[3], data[4],
// data[5], data[6], data[7], data[8], data[9]);
}
bool ok;
for (int i = 3; i < byteArray.size(); i += 2) {
quint8 firstByte = (i < byteArray.size())? byteArray.at(i) : 0;
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;
void MainWindow::slot_pageParaSetShow(int row, int nb, uint16_t *data)
{
// qDebug() << "mainwindow:" << data[0] << data[1] << data[2] << data[3] << data[4];
int i = 0;
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());
if (model)
{
qDebug() << "1111111111111111111";
QModelIndex index = model->index(row, 2);
model->setData(index, result_int[i]);
qDebug() << "row " << row << " result: " << result_int[i];
model->setData(index, data[i]);
qDebug() << "" << i << "个元素:"<< "row " << row << " result: " << data[i];
}
}
}
}
i = 0;
}
void MainWindow::trubleListInit()
@ -3853,7 +3850,7 @@ void MainWindow::slot_pageHomeFault(QByteArray byteArray, int dataType, uint16_t
void MainWindow::closeEvent(QCloseEvent *e)
{
emit closeThread();
emit quitThread();
QWidget::closeEvent(e);
}
@ -4226,11 +4223,13 @@ void MainWindow::on_pageParaSetWrite_clicked()
}
else
{
emit closeThread();
QThread::msleep(700);
for (int i=0; i<checkRow_write.size();i++)
{
emit write_regs_signals(checkRow_write[i], originVector[i]);
QThread::msleep(100);
}
emit openThread();
}
// 序列化消息对象
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_temp(int addr, int nb, uint16_t *data);
void row_of_bmus_index(int index);
void closeThread();
void openThread();
void quitThread();
public:
void slot_getBcuNumberFromDialoglogin(int number);
@ -162,7 +163,8 @@ public:
void slot_pageHomeBaseInfo57_78(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_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);
protected:

View File

@ -4,6 +4,7 @@
#include <QDebug>
#include "modbus-private.h"
#include <QMessageBox>
#include <QEventLoop>
ModbusRTU::ModbusRTU(QObject *parent) : QThread(parent)
{
init();
@ -25,179 +26,152 @@ void ModbusRTU::init()
void ModbusRTU::run()
{
qDebug() << "进入线程:" << QThread::currentThread();
// QMetaObject::invokeMethod(QThread::currentThread(), "read_regs_slot", Qt::QueuedConnection);
// QMetaObject::invokeMethod(QThread::currentThread(), "write_regs_slot", Qt::QueuedConnection);
while(true)
{
// qDebug() << "11111111111111111111111111111111";
QThread::msleep(500);
uint16_t base1_45[45];
uint16_t base57_78[22];
uint16_t base83_106[24];
uint16_t base47[1];
uint16_t base49[1];
uint16_t base51[1];
uint16_t base53[1];
uint16_t base55[1];
QList<int> base_list_47_49_51_53_55;
uint16_t sgl_vol[16]; // Buffer to store the registers
uint16_t sgl_tempt[16];
uint16_t fault[8];
// uint16_t alarm[];
if (closeThread)
{
QElapsedTimer time;
time.start();
QThread::msleep(300);
uint16_t base1_45[45];
uint16_t base57_78[22];
uint16_t base83_106[24];
uint16_t base47[1];
uint16_t base49[1];
uint16_t base51[1];
uint16_t base53[1];
uint16_t base55[1];
QList<int> base_list_47_49_51_53_55;
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);
if(MODBUS_ERR == ret_base1_45)
{
// qDebug() << "modbus no message";
goto label;
}
else
{
// qDebug() << "2222222222222222222222222222222";
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_base_info1_45(sendArray, TypeSendData, base1_45);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
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;
label: int ret_base1_45 = modbus_read_input_registers(m_ModbusRTUDevice, 1, 45, base1_45);
if(MODBUS_ERR == ret_base1_45)
{
goto label;
}
else
{
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_base_info1_45(sendArray, TypeSendData, base1_45);
free(m_ModbusRTUDevice->sendMsg);
QByteArray recvArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->recvMsg), m_ModbusRTUDevice->recvMsgLen);
emit sig_showModbusData_base_info1_45(recvArray, TypeRecvData, base1_45);
free(m_ModbusRTUDevice->recvMsg);
}
emit sig_showModbusData_base_info_list_47_49_51_53_55(base_list_47_49_51_53_55, TypeRecvData);
// emit sig_showModbusData_base_info47_49_51_53_55(sendArray, TypeSendData);
free(m_ModbusRTUDevice->recvMsg);
}
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;
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);
if(MODBUS_ERR == ret_base57_78)
{
// qDebug() << "modbus no message";
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);
}
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);
}
int ret_fault = modbus_read_input_registers(m_ModbusRTUDevice, 66, 8, fault);
if(MODBUS_ERR == ret_fault)
{
// 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);
}
emit sig_showModbusData_base_info_list_47_49_51_53_55(base_list_47_49_51_53_55, TypeRecvData);
free(m_ModbusRTUDevice->recvMsg);
}
int ret_base83_106 = modbus_read_input_registers(m_ModbusRTUDevice, 83, 24, base83_106);
// qDebug() << "#######" << base83_106[0] << base83_106[1] << base83_106[2] << base83_106[3];
// 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];
if(MODBUS_ERR == ret_base83_106)
{
// qDebug() << "modbus no message";
goto label;
}
else
{
// qDebug() << "5555555555555555555555555555";
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 ret_base57_78 = modbus_read_input_registers(m_ModbusRTUDevice, 57, 22, base57_78);
if(MODBUS_ERR == ret_base57_78)
{
goto label;
}
else
{
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);
}
// qDebug() << "the current of the BMU is :" << BMUIndex;
int volAddress = (1001 + BMUIndex * 16);
int tempAddress = (1601 + BMUIndex * 16);
// qDebug() << "volAddress: " << volAddress;
// qDebug() << "tempAddress: " << tempAddress;
// QElapsedTimer time;
// 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]
// << sgl_vol[8] << sgl_vol[9] << sgl_vol[10] << sgl_vol[11] << sgl_vol[12] << sgl_vol[13] << sgl_vol[14] << sgl_vol[15];
// int milsec = time.elapsed();
// qDebug() <<"0.12311231" << milsec;
int ret_fault = modbus_read_input_registers(m_ModbusRTUDevice, 66, 8, fault);
if(MODBUS_ERR == ret_fault)
{
goto label;
}
else
{
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_vol = modbus_read_input_registers(m_ModbusRTUDevice, volAddress, 16, sgl_vol);
if(MODBUS_ERR == ret_vol)
{
// qDebug() << "modbus no message";
goto label;
int ret_base83_106 = modbus_read_input_registers(m_ModbusRTUDevice, 83, 24, base83_106);
if(MODBUS_ERR == ret_base83_106)
{
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);
}
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);
}
// 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);
}
int ret_vol = modbus_read_input_registers(m_ModbusRTUDevice, volAddress, 16, sgl_vol);
if(MODBUS_ERR == ret_vol)
{
goto label;
}
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();
int rc = write_single_register(addr, data);
this->blockSignals(true);
if(MODBUS_ERR == rc) {
qDebug() << "write######modbus no message";
qDebug() << "write######modbus no message" << modbus_strerror(errno);
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)
{
// 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);
if(MODBUS_ERR == rc) {
qDebug() << "read modbus no message";
qDebug() << "read modbus no message" << modbus_strerror(errno);
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);
emit sig_showReadHoldingRegs(recvArray, TypeRecvData, row, nb);
emit sig_showReadHoldingRegs(row, nb, data);
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)
{
BMUIndex = index;

View File

@ -4,6 +4,7 @@
#include <QObject>
#include "modbus.h"
#include <QThread>
#include <QEventLoop>
class QTimer;
@ -30,9 +31,11 @@ public:
int dataBit;
int stopBit;
int BMUIndex = 0;
bool closeThread = true;
public:
void run() override;
QEventLoop eventLoop;
private:
@ -74,12 +77,15 @@ signals:
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_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 paused();
void resumed();
public slots:
void slot_pauseThread();
void slot_resumeThread();
};
#endif // MODBUSRTU_H