[Add] move modbusrtu to thread very smoothgit status!

This commit is contained in:
Carl 2024-11-01 11:24:09 +08:00
parent 54d0c9e831
commit f474423549
6 changed files with 148 additions and 145 deletions

View File

@ -49,7 +49,7 @@ void AppSerial::setHWND(HWND hwnd)
this->m_hwnd = hwnd;
}
bool AppSerial::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
bool AppSerial::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result)
{
Q_UNUSED(eventType);
Q_UNUSED(result);

View File

@ -33,7 +33,7 @@ public:
void setHWND(HWND hwnd);
protected:
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result);
bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result);
signals:
/**

View File

@ -159,6 +159,7 @@ void DialogLogin::slot_comChangeStatus(QString name, bool flag)
void DialogLogin::slot_btnConnect_clicked()
{
BCUNumbers = ui->comboBoxBCUQuantity->currentText().toInt();
emit sendBCUNumberSignals(BCUNumbers);
connect(this, &DialogLogin::sendBCUNumberSignals, m_MainWindow, &MainWindow::slot_getBcuNumberFromDialoglogin);
ini->setValue("Serial/BaudRate", ui->comboBoxBaudRate->currentIndex());
@ -246,7 +247,7 @@ void DialogLogin::slot_btnConnect_clicked()
m_MainWindow->statusBarConnectedIcon->setPixmap(QPixmap(QIcon(":/Image/connected2.png").pixmap(QSize(24, 24))));
m_MainWindow->show();
// m_MainWindow->showMaximized();
m_ModbusRTU->start();
}
else {
if(ini->value("System/Language") == MainWindow::LANGUAGE_ENG) {

View File

@ -1266,6 +1266,7 @@ void MainWindow::slot_pageHomeBaseInfo1_45(QByteArray byteArray, int dataType)
{
if(dataType == TypeRecvData)
{
// qDebug() << "33333333333333333333333333333333";
QList<int> result_int;
bool ok;

View File

@ -3,8 +3,8 @@
#include <QEvent>
#include <QDebug>
#include "modbus-private.h"
ModbusRTU::ModbusRTU(QObject *parent) : QObject(parent)
#include <QMessageBox>
ModbusRTU::ModbusRTU(QObject *parent) : QThread(parent)
{
init();
}
@ -17,31 +17,16 @@ ModbusRTU::~ModbusRTU()
void ModbusRTU::init()
{
timer50msId = startTimer(50);
timer100msId = startTimer(100);
timer200msId = startTimer(200);
timer1000msId = startTimer(1000);
}
void ModbusRTU::timerEvent(QTimerEvent *event)
void ModbusRTU::run()
{
if(isConnect != true) {
return;
}
else {
if(event->timerId() == timer50msId) {
}
if(event->timerId() == timer100msId) {
}
if(event->timerId() == timer200msId) {
}
if(event->timerId() == timer1000msId) {
uint16_t sgl_vol[112]; // Buffer to store the registers
uint16_t sgl_tempt[112];
qDebug() << "进入线程:" << QThread::currentThread();
while(true)
{
// qDebug() << "11111111111111111111111111111111";
QThread::msleep(200);
uint16_t base1_45[45];
uint16_t base57_78[22];
uint16_t base83_106[24];
@ -50,15 +35,19 @@ void ModbusRTU::timerEvent(QTimerEvent *event)
uint16_t base51[1];
uint16_t base53[1];
uint16_t base55[1];
uint16_t base47_49_51_53_55[5];
uint16_t test2[2] = {10, 20};
QList<int> base_list_47_49_51_53_55;
int ret_base1_45 = modbus_read_input_registers(m_ModbusRTUDevice, 1, 45, base1_45);
if(MODBUS_ERR == ret_base1_45) {
// qDebug() << "modbus no message";
return;
uint16_t sgl_vol[112]; // Buffer to store the registers
uint16_t sgl_tempt[112];
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 {
else
{
// qDebug() << "2222222222222222222222222222222";
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_base_info1_45(sendArray, TypeSendData);
free(m_ModbusRTUDevice->sendMsg);
@ -104,11 +93,14 @@ void ModbusRTU::timerEvent(QTimerEvent *event)
}
int ret_base57_78 = modbus_read_input_registers(m_ModbusRTUDevice, 57, 22, base57_78);
if(MODBUS_ERR == ret_base57_78) {
if(MODBUS_ERR == ret_base57_78)
{
// qDebug() << "modbus no message";
return;
}
else {
else
{
// qDebug() << "4444444444444444444444444444";
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_base_info57_78(sendArray, TypeSendData);
free(m_ModbusRTUDevice->sendMsg);
@ -118,11 +110,14 @@ void ModbusRTU::timerEvent(QTimerEvent *event)
}
int ret_base83_106 = modbus_read_input_registers(m_ModbusRTUDevice, 83, 24, base83_106);
if(MODBUS_ERR == ret_base83_106) {
if(MODBUS_ERR == ret_base83_106)
{
// qDebug() << "modbus no message";
return;
goto label;
}
else {
else
{
// qDebug() << "5555555555555555555555555555";
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_base_info83_106(sendArray, TypeSendData);
free(m_ModbusRTUDevice->sendMsg);
@ -132,11 +127,14 @@ void ModbusRTU::timerEvent(QTimerEvent *event)
}
int ret_vol = modbus_read_input_registers(m_ModbusRTUDevice, 1001, 112, sgl_vol);
if(MODBUS_ERR == ret_vol) {
if(MODBUS_ERR == ret_vol)
{
// qDebug() << "modbus no message";
return;
goto label;
}
else {
else
{
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData(sendArray, TypeSendData);
free(m_ModbusRTUDevice->sendMsg);
@ -146,11 +144,13 @@ void ModbusRTU::timerEvent(QTimerEvent *event)
}
int ret_tempt = modbus_read_input_registers(m_ModbusRTUDevice, 1601, 112, sgl_tempt);
if(MODBUS_ERR == ret_tempt) {
if(MODBUS_ERR == ret_tempt)
{
// qDebug() << "modbus no message";
return;
goto label;
}
else {
else
{
QByteArray sendArray = QByteArray::fromRawData(reinterpret_cast<char *>(m_ModbusRTUDevice->sendMsg), m_ModbusRTUDevice->sendMsgLen);
emit sig_showModbusData_sgl_tempt(sendArray, TypeSendData);
free(m_ModbusRTUDevice->sendMsg);
@ -158,10 +158,12 @@ void ModbusRTU::timerEvent(QTimerEvent *event)
emit sig_showModbusData_sgl_tempt(recvArray, TypeRecvData);
free(m_ModbusRTUDevice->recvMsg);
}
}
}
}
void ModbusRTU::write_regs_slot(int addr,const uint16_t data)
{
int rc = write_single_register(addr, data);

View File

@ -3,16 +3,16 @@
#include <QObject>
#include "modbus.h"
#include <QThread>
class QTimer;
class ModbusRTU : public QObject
class ModbusRTU : public QThread
{
Q_OBJECT
public:
ModbusRTU(QObject *parent = nullptr);
~ModbusRTU();
void timerEvent(QTimerEvent *event);
void write_regs_slot(int addr, const uint16_t data);
void read_regs_slot(int addr, int nb, uint16_t *data, int row);
@ -25,13 +25,12 @@ public:
int dataBit;
int stopBit;
private:
int timer50msId;
int timer100msId;
int timer200msId;
int timer1000msId;
protected:
void run() override;
static constexpr int MODBUS_ERR = -1;
private:
static constexpr int MODBUS_ERR = -1;
enum DataType{
TypeRecvData,