Pārlūkot izejas kodu

收发报文

master
鹏鹏 李 pirms 2 nedēļām
vecāks
revīzija
05bc65f4a9
7 mainītis faili ar 238 papildinājumiem un 9 dzēšanām
  1. +24
    -0
      crc.cpp
  2. +8
    -0
      crc.h
  3. +4
    -2
      modbus.pro
  4. +1
    -1
      modbus.pro.user
  5. +27
    -0
      widget.cpp
  6. +2
    -0
      widget.h
  7. +172
    -6
      widget.ui

+ 24
- 0
crc.cpp Parādīt failu

@@ -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<quint8>(data.at(i));

for (int bit = 0; bit < 8; ++bit)
{
if (crc & 0x0001)
{ // 检查最低位
crc = (crc >> 1) ^ polynomial; // 右移并异或
} else
{
crc >>= 1; // 仅右移
}
}
}
return crc;
}

+ 8
- 0
crc.h Parādīt failu

@@ -0,0 +1,8 @@
#ifndef CRC_H
#define CRC_H

#endif // CRC_H
#include <QByteArray>
#include <QtGlobal>

quint16 calculateCrc(const QByteArray &data);

+ 4
- 2
modbus.pro Parādīt failu

@@ -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

+ 1
- 1
modbus.pro.user Parādīt failu

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.0.3, 2025-07-23T18:39:04. -->
<!-- Written by QtCreator 4.0.3, 2025-07-24T09:24:34. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>


+ 27
- 0
widget.cpp Parādīt failu

@@ -4,6 +4,7 @@
#include <QDebug>
#include <QSerialPort>
#include <QByteArray>
#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);
}

+ 2
- 0
widget.h Parādīt failu

@@ -23,6 +23,8 @@ private slots:

void on_SerialData_ReadyToRead();

void on_btn_read_clicked();

private:
Ui::Widget *ui;
QSerialPort *serialPort;


+ 172
- 6
widget.ui Parādīt failu

@@ -224,8 +224,8 @@
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>20</x>
<y>150</y>
<x>420</x>
<y>70</y>
<width>231</width>
<height>21</height>
</rect>
@@ -234,8 +234,8 @@
<widget class="QPushButton" name="pushWrite">
<property name="geometry">
<rect>
<x>270</x>
<y>150</y>
<x>660</x>
<y>60</y>
<width>93</width>
<height>28</height>
</rect>
@@ -247,13 +247,179 @@
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>20</x>
<y>180</y>
<x>420</x>
<y>100</y>
<width>251</width>
<height>151</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_10">
<property name="geometry">
<rect>
<x>70</x>
<y>150</y>
<width>21</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>读</string>
</property>
</widget>
<widget class="QComboBox" name="comboBox_stationAddress">
<property name="geometry">
<rect>
<x>92</x>
<y>120</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<item>
<property name="text">
<string>1</string>
</property>
</item>
<item>
<property name="text">
<string>2</string>
</property>
</item>
<item>
<property name="text">
<string>3</string>
</property>
</item>
<item>
<property name="text">
<string>4</string>
</property>
</item>
<item>
<property name="text">
<string>5</string>
</property>
</item>
<item>
<property name="text">
<string>6</string>
</property>
</item>
<item>
<property name="text">
<string>7</string>
</property>
</item>
<item>
<property name="text">
<string>8</string>
</property>
</item>
<item>
<property name="text">
<string>9</string>
</property>
</item>
</widget>
<widget class="QLabel" name="label_9">
<property name="geometry">
<rect>
<x>40</x>
<y>120</y>
<width>45</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>站地址</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_length">
<property name="geometry">
<rect>
<x>90</x>
<y>210</y>
<width>51</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>00</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_stratAddress">
<property name="geometry">
<rect>
<x>100</x>
<y>180</y>
<width>51</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>00</string>
</property>
</widget>
<widget class="QLabel" name="label_12">
<property name="geometry">
<rect>
<x>40</x>
<y>210</y>
<width>41</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>长度</string>
</property>
</widget>
<widget class="QLabel" name="label_11">
<property name="geometry">
<rect>
<x>30</x>
<y>180</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>起始地址</string>
</property>
</widget>
<widget class="QComboBox" name="comboBox_gongnengma">
<property name="geometry">
<rect>
<x>90</x>
<y>150</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<item>
<property name="text">
<string>线圈</string>
</property>
</item>
<item>
<property name="text">
<string>寄存器</string>
</property>
</item>
</widget>
<widget class="QPushButton" name="btn_read">
<property name="geometry">
<rect>
<x>40</x>
<y>260</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>读</string>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>


Notiek ielāde…
Atcelt
Saglabāt