From 89d1827e71ed0f4263b63555d94bb6c1ef9a3624 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 1 Aug 2025 19:28:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=91=BD=E4=BB=A4=E7=94=9F?= =?UTF-8?q?=E6=88=90=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B5=8B=E8=AF=95=E9=80=9A?= =?UTF-8?q?=E8=BF=87=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SP713_Upper.pro | 2 + mainwindow.cpp | 15 ++++++ mainwindow.h | 8 +++- src/headers/drv_spi.h | 3 +- src/headers/framehandler.h | 25 ++++++++++ src/sources/crc.cpp | 35 -------------- src/sources/framehandler.cpp | 93 ++++++++++++++++++++++++++++++++++++ 7 files changed, 143 insertions(+), 38 deletions(-) create mode 100644 src/headers/framehandler.h create mode 100644 src/sources/framehandler.cpp diff --git a/SP713_Upper.pro b/SP713_Upper.pro index e858d18..80ccced 100644 --- a/SP713_Upper.pro +++ b/SP713_Upper.pro @@ -10,6 +10,7 @@ CONFIG += c++17 SOURCES += \ dialogcalibrate.cpp \ + src/sources/framehandler.cpp \ main.cpp \ mainwindow.cpp \ src/sources/crc.cpp \ @@ -18,6 +19,7 @@ SOURCES += \ HEADERS += \ dialogcalibrate.h \ + src/headers/framehandler.h \ mainwindow.h \ src/headers/CH347DLL.H \ src/headers/crc.h \ diff --git a/mainwindow.cpp b/mainwindow.cpp index 9319eab..556078e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -8,6 +8,7 @@ MainWindow::MainWindow(QWidget *parent) ui->setupUi(this); drvSPI = new DRV_Spi(this); drvUart = new DRV_Uart(this); + fHandler = new FrameHandler(this); } MainWindow::~MainWindow() @@ -172,3 +173,17 @@ void MainWindow::on_pushButton_3_clicked() } } + +void MainWindow::on_pushButton_9_clicked() +{ + QByteArray tmp = fHandler->genREGWrite(0x01,0x01,0x09,0x81); + qDebug()<genREGRead(0x01); + qDebug()< +#include "QByteArray" +#include "qdebug.h" + +class FrameHandler : public QObject +{ + Q_OBJECT +public: + explicit FrameHandler(QObject *parent = nullptr); + QByteArray genREGWrite(unsigned char ucDAC,unsigned char ucChannel,unsigned char ucFun1,unsigned char ucFun2); + QByteArray genREGRead(unsigned char ucDAC); + QByteArray genCrc16(const QByteArray& data); + QByteArray genSum(const QByteArray& data); + static void genCrc16table(quint16 table[256], quint16 poly); + +signals: + +private: + unsigned char nFrameCount; +}; + +#endif // FRAMEHANDLER_H diff --git a/src/sources/crc.cpp b/src/sources/crc.cpp index 4a595e9..e8ba122 100644 --- a/src/sources/crc.cpp +++ b/src/sources/crc.cpp @@ -4,39 +4,4 @@ // void generate_crc16_reverse_table(quint16 *table); // QByteArray crc16Reverse(const QByteArray& data); -static void generate_crc16_reverse_table(quint16 table[256], quint16 poly = 0x8408) -{ - for (int i = 0; i < 256; ++i) { - quint16 crc = static_cast(i); - for (int j = 0; j < 8; ++j) { - if (crc & 0x0001) - crc = (crc >> 1) ^ poly; - else - crc >>= 1; - } - table[i] = crc; - } -} -// 计算反射型CRC16-CCITT -QByteArray crc16Reverse(const QByteArray& data) -{ - static quint16 table[256]; - static bool tableInited = false; - if (!tableInited) { - generate_crc16_reverse_table(table); - tableInited = true; - } - - quint16 crc = 0x6363; - for (auto b : data) - crc = (crc >> 8) ^ table[(crc ^ static_cast(b)) & 0xFF]; - - QByteArray result; - // 经过和python程序对比,说明CRC16的两个byte位置需调换 - // result.append(static_cast((crc >> 8) & 0xFF)); - // result.append(static_cast(crc & 0xFF)); - result.append(static_cast(crc & 0xFF)); - result.append(static_cast((crc >> 8) & 0xFF)); - return result; -} diff --git a/src/sources/framehandler.cpp b/src/sources/framehandler.cpp new file mode 100644 index 0000000..c3b164d --- /dev/null +++ b/src/sources/framehandler.cpp @@ -0,0 +1,93 @@ +#include "framehandler.h" + +FrameHandler::FrameHandler(QObject *parent) + : QObject{parent} +{ + nFrameCount=0; +} + + +void FrameHandler::genCrc16table(quint16 table[256], quint16 poly = 0x8408) +{ + for (int i = 0; i < 256; ++i) { + quint16 crc = static_cast(i); + for (int j = 0; j < 8; ++j) { + if (crc & 0x0001) + crc = (crc >> 1) ^ poly; + else + crc >>= 1; + } + table[i] = crc; + } +} + +// 计算反射型CRC16-CCITT +QByteArray FrameHandler::genCrc16(const QByteArray& data) +{ + static quint16 table[256]; + static bool tableInited = false; + if (!tableInited) { + FrameHandler::genCrc16table(table); + tableInited = true; + } + + quint16 crc = 0x6363; + for (auto b : data) + crc = (crc >> 8) ^ table[(crc ^ static_cast(b)) & 0xFF]; + + QByteArray result; + // 经过和python程序对比,说明CRC16的两个byte位置需调换 + // result.append(static_cast((crc >> 8) & 0xFF)); + // result.append(static_cast(crc & 0xFF)); + result.append(static_cast(crc & 0xFF)); + result.append(static_cast((crc >> 8) & 0xFF)); + return result; +} + +QByteArray FrameHandler::genSum(const QByteArray &data) +{ + quint8 sum = 0; + for (int i = 0; i < data.length(); ++i) { + sum += static_cast(data.at(i)); // 自动处理溢出 + } + + QByteArray tmp; + tmp.append(sum); + return tmp; +} + + +QByteArray FrameHandler::genREGWrite(unsigned char ucDAC, unsigned char ucChannel, unsigned char ucFun1, unsigned char ucFun2) +{ + QByteArray cmd1(16,0); + cmd1[0]=0x9F; + cmd1[1]=0xE4; + cmd1[2]=0x04; + cmd1[3]=ucDAC; + cmd1[4]=ucChannel; + cmd1[5]=ucFun1; + cmd1[6]=ucFun2; + cmd1[14]=nFrameCount; + QByteArray sumResult = genSum(cmd1); + cmd1[15] = static_cast(sumResult.at(0)); + + nFrameCount++; + + return cmd1; +} + +QByteArray FrameHandler::genREGRead(unsigned char ucDAC) +{ + QByteArray cmd1(16,0); + cmd1[0]=0x9F; + cmd1[1]=0xE4; + cmd1[2]=0x05; + cmd1[3]=ucDAC; + cmd1[14]=nFrameCount; + QByteArray sumResult = genSum(cmd1); + cmd1[15] = static_cast(sumResult.at(0)); + + + nFrameCount++; + return cmd1; +}