From 05bc65f4a9f00ae16bb3f62029256ce2f3e7c513 Mon Sep 17 00:00:00 2001 From: lipengpeng Date: Thu, 24 Jul 2025 09:25:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E5=8F=91=E6=8A=A5=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crc.cpp | 24 +++++++ crc.h | 8 +++ modbus.pro | 6 +- modbus.pro.user | 2 +- widget.cpp | 27 ++++++++ widget.h | 2 + widget.ui | 178 ++++++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 238 insertions(+), 9 deletions(-) create mode 100644 crc.cpp create mode 100644 crc.h diff --git a/crc.cpp b/crc.cpp new file mode 100644 index 0000000..155bbe1 --- /dev/null +++ b/crc.cpp @@ -0,0 +1,24 @@ +#include "crc.h" + +quint16 calculateCrc(const QByteArray &data) +{ + quint16 crc = 0xFFFF; // MODBUS初始值 + const quint16 polynomial = 0xA001; // MODBUS多项式(0x8005的反转) + + for (int i = 0; i < data.size(); ++i) + { + crc ^= static_cast(data.at(i)); + + for (int bit = 0; bit < 8; ++bit) + { + if (crc & 0x0001) + { // 检查最低位 + crc = (crc >> 1) ^ polynomial; // 右移并异或 + } else + { + crc >>= 1; // 仅右移 + } + } + } + return crc; +} diff --git a/crc.h b/crc.h new file mode 100644 index 0000000..621871e --- /dev/null +++ b/crc.h @@ -0,0 +1,8 @@ +#ifndef CRC_H +#define CRC_H + +#endif // CRC_H +#include +#include + +quint16 calculateCrc(const QByteArray &data); diff --git a/modbus.pro b/modbus.pro index 1eccdc5..c6e6268 100644 --- a/modbus.pro +++ b/modbus.pro @@ -14,8 +14,10 @@ TEMPLATE = app SOURCES += main.cpp\ - widget.cpp + widget.cpp \ + crc.cpp -HEADERS += widget.h +HEADERS += widget.h \ + crc.h FORMS += widget.ui diff --git a/modbus.pro.user b/modbus.pro.user index d691afe..b047d9f 100644 --- a/modbus.pro.user +++ b/modbus.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/widget.cpp b/widget.cpp index 783d31e..1de5a7b 100644 --- a/widget.cpp +++ b/widget.cpp @@ -4,6 +4,7 @@ #include #include #include +#include "crc.h" Widget::Widget(QWidget *parent) : QWidget(parent), @@ -88,3 +89,29 @@ void Widget::on_SerialData_ReadyToRead() ui->textEdit->append(hexData); } + +void Widget::on_btn_read_clicked() +{ + QByteArray SendCommand; + SendCommand.append(ui->comboBox_stationAddress->currentText().toInt()%256); + if (ui->comboBox_gongnengma->currentIndex() == 0) + { + SendCommand.append(0x01); + } + else if(ui->comboBox_gongnengma->currentIndex() == 1) + { + SendCommand.append(0x03); + } + SendCommand.append(ui->lineEdit_stratAddress->text().toInt()/256); + SendCommand.append(ui->lineEdit_stratAddress->text().toInt()%256); + SendCommand.append(ui->lineEdit_length->text().toInt()/256); + SendCommand.append(ui->lineEdit_length->text().toInt()%256); + + quint16 temp = calculateCrc(SendCommand); + + SendCommand.append(temp%256); + SendCommand.append(temp/256); + + qDebug() << SendCommand.toHex(); + serialPort->write(SendCommand); +} diff --git a/widget.h b/widget.h index 7d769b1..22fc51a 100644 --- a/widget.h +++ b/widget.h @@ -23,6 +23,8 @@ private slots: void on_SerialData_ReadyToRead(); + void on_btn_read_clicked(); + private: Ui::Widget *ui; QSerialPort *serialPort; diff --git a/widget.ui b/widget.ui index 5a3130d..dc01c71 100644 --- a/widget.ui +++ b/widget.ui @@ -224,8 +224,8 @@ - 20 - 150 + 420 + 70 231 21 @@ -234,8 +234,8 @@ - 270 - 150 + 660 + 60 93 28 @@ -247,13 +247,179 @@ - 20 - 180 + 420 + 100 251 151 + + + + 70 + 150 + 21 + 16 + + + + + + + + + + 92 + 120 + 61 + 21 + + + + + 1 + + + + + 2 + + + + + 3 + + + + + 4 + + + + + 5 + + + + + 6 + + + + + 7 + + + + + 8 + + + + + 9 + + + + + + + 40 + 120 + 45 + 16 + + + + 站地址 + + + + + + 90 + 210 + 51 + 21 + + + + 00 + + + + + + 100 + 180 + 51 + 21 + + + + 00 + + + + + + 40 + 210 + 41 + 16 + + + + 长度 + + + + + + 30 + 180 + 71 + 16 + + + + 起始地址 + + + + + + 90 + 150 + 61 + 21 + + + + + 线圈 + + + + + 寄存器 + + + + + + + 40 + 260 + 93 + 28 + + + + + +