diff --git a/Modbus/Modbus.vcxproj b/Modbus/Modbus.vcxproj index 5cd1f61..dacd89e 100644 --- a/Modbus/Modbus.vcxproj +++ b/Modbus/Modbus.vcxproj @@ -71,7 +71,7 @@ true - Windows + Console true @@ -95,9 +95,10 @@ + - + @@ -106,11 +107,12 @@ - + + diff --git a/Modbus/Modbus.vcxproj.filters b/Modbus/Modbus.vcxproj.filters index bc1fd80..62f3aea 100644 --- a/Modbus/Modbus.vcxproj.filters +++ b/Modbus/Modbus.vcxproj.filters @@ -52,7 +52,7 @@ Source Files - + Source Files @@ -61,6 +61,9 @@ Source Files + + Source Files + @@ -74,7 +77,7 @@ Header Files - + Header Files @@ -83,5 +86,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Modbus/include/modbus_log.h b/Modbus/include/modbus_log.h deleted file mode 100644 index 581bf76..0000000 --- a/Modbus/include/modbus_log.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include - - -/** - * . - */ -class Modbus_Log : public QObject -{ - Q_OBJECT - -public: - Modbus_Log(QObject *parent); - ~Modbus_Log(); -}; - diff --git a/Modbus/include/modbus_rtu_pkg.h b/Modbus/include/modbus_rtu_pkg.h deleted file mode 100644 index 6d62478..0000000 --- a/Modbus/include/modbus_rtu_pkg.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - - -/** - * Modbus RTU数据包格式合成和解析 - * 创建传输或发送数据包的统一格式 - * 计算以及解析CRC16校验码 - */ -class Modbus_RTU_Pkg : public QObject -{ - Q_OBJECT - -public: - explicit Modbus_RTU_Pkg(QObject *parent = nullptr); - ~Modbus_RTU_Pkg(); - - // 构建Modbus RTU数据包 - QVector build_modbus_pkg( - quint8 slave_addr, - quint8 func_code, - const QVector& work_data - ); - - // 解析Modbus RTU数据包 - bool parse_modbus_pkg( - const QVector &pkg, - quint8 &slave_addr, - quint8 &func_code, - QVector &work_data - ); - - - -private: - // 计算Modbus CRC16 - quint16 calc_crc(const QVector& pkg_data) const; -}; - diff --git a/Modbus/include/modbus_rtu_receiver.h b/Modbus/include/modbus_rtu_receiver.h deleted file mode 100644 index 3e0774f..0000000 --- a/Modbus/include/modbus_rtu_receiver.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include - -class Modbus_RTU_Receiver : public QObject -{ - Q_OBJECT - -public: - Modbus_RTU_Receiver(QObject *parent); - ~Modbus_RTU_Receiver(); -}; - diff --git a/Modbus/include/modbus_rtu_sender.h b/Modbus/include/modbus_rtu_sender.h deleted file mode 100644 index c9eba3d..0000000 --- a/Modbus/include/modbus_rtu_sender.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include -#include -#include - -/** - * Modbus RTU发送数据类 - * 主要实现了功能码 - */ -class Modbus_RTU_Sender : public QObject -{ -public: - explicit Modbus_RTU_Sender(QObject *parent); - ~Modbus_RTU_Sender(); - - - -}; - diff --git a/Modbus/src/modbus.cpp b/Modbus/src/modbus.cpp index 0e60d53..806d566 100644 --- a/Modbus/src/modbus.cpp +++ b/Modbus/src/modbus.cpp @@ -32,20 +32,21 @@ Modbus::~Modbus() } + + // 实现下拉框显示串口 void Modbus::init_serialport_client() { ui->serialBox->addItems(get_avail_serialport()); } -// 下拉框获取信息 QStringList Modbus::get_avail_serialport() { // 下拉框串口 QStringList listAvailSerialPort; // 遍历可用串口 - foreach(const QSerialPortInfo& info, QSerialPortInfo::availablePorts()) + foreach(const QSerialPortInfo & info, QSerialPortInfo::availablePorts()) { serial->setPort(info); // 使用ReadWrite模式打开串口成功,则加入列表 @@ -58,6 +59,7 @@ QStringList Modbus::get_avail_serialport() return listAvailSerialPort; } + // 使用按钮打开串口 void Modbus::serial_button_clicked() { diff --git a/Modbus/src/modbus_log.cpp b/Modbus/src/modbus_log.cpp deleted file mode 100644 index 3df7a97..0000000 --- a/Modbus/src/modbus_log.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "../include/modbus_log.h" - -Modbus_Log::Modbus_Log(QObject *parent) - : QObject(parent) -{ - -} - -Modbus_Log::~Modbus_Log() -{ - -} - diff --git a/Modbus/src/modbus_rtu_pkg.cpp b/Modbus/src/modbus_rtu_pkg.cpp deleted file mode 100644 index 1f24bb3..0000000 --- a/Modbus/src/modbus_rtu_pkg.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include "../include/modbus_rtu_pkg.h" - -Modbus_RTU_Pkg::Modbus_RTU_Pkg(QObject *parent) - : QObject(parent) -{ - -} - -Modbus_RTU_Pkg::~Modbus_RTU_Pkg() -{ - -} - -/** - * 构建Modbus RTU数据包 - * - * \param slave_addr - * \param func_code - * \param work_data - * \return - */ -QVector Modbus_RTU_Pkg::build_modbus_pkg( - quint8 slave_addr, - quint8 func_code, - const QVector& work_data -) -{ - QVector pkg; - - try { - // 添加设备地址 - pkg.append(slave_addr); - // 添加功能码 - pkg.append(func_code); - // 添加工作数据 - pkg.append(work_data); - - // 计算CRC - quint16 crc = calc_crc(pkg); - - // 添加CRC,小端序 - // 低字节 - pkg.append(static_cast(crc & 0xFF)); - // 高字节 - pkg.append(static_cast((crc >> 8) & 0xFF)); - } - catch (const std::exception& ex) { - // TODO:抛出异常 - - - } - - return pkg; -} - -/** - * 解析Modbus RTU数据包 - * - * \param pkg - * \param slave_addr - * \param func_code - * \param work_data - * \return - */ -bool Modbus_RTU_Pkg::parse_modbus_pkg( - const QVector& pkg, - quint8& slave_addr, - quint8& func_code, - QVector& work_data -) -{ - // 检查数据包长度 - if (pkg.size() < 4) - { - // TODO:抛出异常 - - return false; - } - - // 提取设备地址 - slave_addr = pkg[0]; - // 提取功能码 - func_code = pkg[1]; - // 提取工作数据 - work_data.clear(); - if (pkg.size() > 4) - { - work_data = pkg.mid(2, pkg.size() - 4); - } - // 提取数据包自带的CRC - quint16 crc_bring = static_cast(pkg[pkg.size() - 2]) | - (static_cast(pkg[pkg.size() - 1]) << 8); - // 计算接收数据CRC - QVector pkg_data = pkg.mid(0, pkg.size() - 2); - quint16 crc_after_calc = calc_crc(pkg_data); - // CRC校验 - if (crc_after_calc != crc_bring) - { - // TODO:抛出异常 - - return false; - } - - return true; -} - -/** - * 使用查表法计算CRC - * - * \param pkg_data - * \return - */ -quint16 Modbus_RTU_Pkg::calc_crc(const QVector& pkg_data) const -{ - static const quint16 crcTable[] = { - 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, - 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, - 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, - 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841 - }; - - quint16 crc = 0xFFFF; - for (int i = 0; i < pkg_data.size(); ++i) { - crc ^= static_cast(pkg_data[i]); - crc = (crc >> 8) ^ crcTable[crc & 0x0F]; - crc = (crc >> 8) ^ crcTable[crc & 0x0F]; - } - return crc; -} diff --git a/Modbus/src/modbus_rtu_receiver.cpp b/Modbus/src/modbus_rtu_receiver.cpp deleted file mode 100644 index d52f898..0000000 --- a/Modbus/src/modbus_rtu_receiver.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "../include/modbus_rtu_receiver.h" - -Modbus_RTU_Receiver::Modbus_RTU_Receiver(QObject *parent) - : QObject(parent) -{ - -} - -Modbus_RTU_Receiver::~Modbus_RTU_Receiver() -{ - -} - diff --git a/Modbus/src/modbus_rtu_sender.cpp b/Modbus/src/modbus_rtu_sender.cpp deleted file mode 100644 index fe9e089..0000000 --- a/Modbus/src/modbus_rtu_sender.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "../include/modbus_rtu_sender.h" - - -Modbus_RTU_Sender::Modbus_RTU_Sender(QObject* parent) - : QObject(parent) -{ -} - -Modbus_RTU_Sender::~Modbus_RTU_Sender() -{ - -} -