加入命令生成逻辑,测试通过;
This commit is contained in:
parent
87af325c53
commit
89d1827e71
@ -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 \
|
||||
|
||||
@ -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()<<tmp.toHex();
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::on_pushButton_10_clicked()
|
||||
{
|
||||
QByteArray tmp = fHandler->genREGRead(0x01);
|
||||
qDebug()<<tmp.toHex();
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
#include "src/headers/drv_uart.h"
|
||||
#include "src/headers/drv_spi.h"
|
||||
#include "QDateTime"
|
||||
|
||||
#include "src/headers/framehandler.h"
|
||||
|
||||
|
||||
|
||||
@ -49,11 +49,17 @@ private slots:
|
||||
|
||||
void on_pushButton_3_clicked();
|
||||
|
||||
void on_pushButton_9_clicked();
|
||||
|
||||
void on_pushButton_10_clicked();
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
DialogCalibrate *dc;
|
||||
DRV_Uart *drvUart;
|
||||
DRV_Spi *drvSPI;
|
||||
FrameHandler *fHandler;
|
||||
|
||||
|
||||
};
|
||||
#endif // MAINWINDOW_H
|
||||
|
||||
@ -69,8 +69,7 @@ signals:
|
||||
private:
|
||||
// 私有成员变量
|
||||
// double m_currentValues[]; // 当前各通道输出值(假设有多个通道)
|
||||
|
||||
// 私有方法
|
||||
// 私有方法
|
||||
|
||||
};
|
||||
|
||||
|
||||
25
src/headers/framehandler.h
Normal file
25
src/headers/framehandler.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef FRAMEHANDLER_H
|
||||
#define FRAMEHANDLER_H
|
||||
|
||||
#include <QObject>
|
||||
#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
|
||||
@ -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<quint16>(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<quint8>(b)) & 0xFF];
|
||||
|
||||
QByteArray result;
|
||||
// 经过和python程序对比,说明CRC16的两个byte位置需调换
|
||||
// result.append(static_cast<char>((crc >> 8) & 0xFF));
|
||||
// result.append(static_cast<char>(crc & 0xFF));
|
||||
result.append(static_cast<char>(crc & 0xFF));
|
||||
result.append(static_cast<char>((crc >> 8) & 0xFF));
|
||||
return result;
|
||||
}
|
||||
|
||||
93
src/sources/framehandler.cpp
Normal file
93
src/sources/framehandler.cpp
Normal file
@ -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<quint16>(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<quint8>(b)) & 0xFF];
|
||||
|
||||
QByteArray result;
|
||||
// 经过和python程序对比,说明CRC16的两个byte位置需调换
|
||||
// result.append(static_cast<char>((crc >> 8) & 0xFF));
|
||||
// result.append(static_cast<char>(crc & 0xFF));
|
||||
result.append(static_cast<char>(crc & 0xFF));
|
||||
result.append(static_cast<char>((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<quint8>(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<char>(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<char>(sumResult.at(0));
|
||||
|
||||
|
||||
nFrameCount++;
|
||||
return cmd1;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user