添加加密部分
This commit is contained in:
parent
98549e640c
commit
9aea2abcd8
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE QtCreatorProject>
|
||||
<!-- Written by QtCreator 4.12.2, 2024-12-18T09:05:17. -->
|
||||
<!-- Written by QtCreator 4.12.2, 2025-02-06T11:28:13. -->
|
||||
<qtcreator>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
|
@ -77,7 +77,7 @@
|
|||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.12.9 MinGW 32-bit</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.12.9 MinGW 32-bit</value>
|
||||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5129.win32_mingw73_kit</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
|
||||
|
@ -320,7 +320,7 @@
|
|||
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
|
||||
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
|
||||
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
|
||||
<value type="QString" key="RunConfiguration.WorkingDirectory.default">E:/Y/QT/build-micro_climate_config-Desktop_Qt_5_12_9_MinGW_32_bit-Debug</value>
|
||||
<value type="QString" key="RunConfiguration.WorkingDirectory.default">E:/Y/QT/build-micro_climate_config-Desktop_Qt_5_12_9_MinGW_32_bit-Release</value>
|
||||
</valuemap>
|
||||
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
|
||||
</valuemap>
|
||||
|
|
231
widget.cpp
231
widget.cpp
|
@ -91,6 +91,13 @@ void Widget::disable__()
|
|||
ui->groupBox->setEnabled(false);
|
||||
ui->groupBox_2->setEnabled(false);
|
||||
ui->groupBox_3->setEnabled(false);
|
||||
// 未打开串口时不让开加密页面
|
||||
ui->main_tabWidget->setTabEnabled(1, false);
|
||||
ui->Password_lineEdit->setEnabled(false);
|
||||
ui->ID_lineEdit->setEnabled(false);
|
||||
ui->label_10->setEnabled(false);
|
||||
ui->label_11->setEnabled(false);
|
||||
ui->Trans_password_pushButton->setEnabled(false);
|
||||
}
|
||||
|
||||
void Widget::able__()
|
||||
|
@ -98,6 +105,8 @@ void Widget::able__()
|
|||
ui->groupBox->setEnabled(true);
|
||||
ui->groupBox_2->setEnabled(true);
|
||||
ui->groupBox_3->setEnabled(true);
|
||||
// 未打开串口时不让开加密页面
|
||||
ui->main_tabWidget->setTabEnabled(1, true);
|
||||
}
|
||||
|
||||
uint16_t Widget::modbusCrc16(const uint8_t *data, const int len)
|
||||
|
@ -382,8 +391,6 @@ void Widget::get_configdata()
|
|||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//30->1E
|
||||
void Widget::on_pushButton_clicked()
|
||||
{
|
||||
|
@ -417,6 +424,11 @@ void Widget::on_pushButton_clicked()
|
|||
sent_comand.append(crc_16_val);
|
||||
|
||||
Serial_port->write(sent_comand);
|
||||
|
||||
//连接信号与槽
|
||||
//断开所有其他的信号与槽连接
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(write_ack()), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
//31->1F
|
||||
|
@ -452,6 +464,11 @@ void Widget::on_pushButton_2_clicked()
|
|||
sent_comand.append(crc_16_val);
|
||||
|
||||
Serial_port->write(sent_comand);
|
||||
|
||||
//连接信号与槽
|
||||
//断开所有其他的信号与槽连接
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(write_ack()), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
//32->20
|
||||
|
@ -487,6 +504,11 @@ void Widget::on_pushButton_3_clicked()
|
|||
sent_comand.append(crc_16_val);
|
||||
|
||||
Serial_port->write(sent_comand);
|
||||
|
||||
//连接信号与槽
|
||||
//断开所有其他的信号与槽连接
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(write_ack()), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
//33->21
|
||||
|
@ -522,6 +544,11 @@ void Widget::on_pushButton_4_clicked()
|
|||
sent_comand.append(crc_16_val);
|
||||
|
||||
Serial_port->write(sent_comand);
|
||||
|
||||
//连接信号与槽
|
||||
//断开所有其他的信号与槽连接
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(write_ack()), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
//34->22
|
||||
|
@ -558,6 +585,11 @@ void Widget::on_pushButton_5_clicked()
|
|||
sent_comand.append(crc_16_val);
|
||||
|
||||
Serial_port->write(sent_comand);
|
||||
|
||||
//连接信号与槽
|
||||
//断开所有其他的信号与槽连接
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(write_ack()), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
//35->23
|
||||
|
@ -594,6 +626,11 @@ void Widget::on_pushButton_6_clicked()
|
|||
sent_comand.append(crc_16_val);
|
||||
|
||||
Serial_port->write(sent_comand);
|
||||
|
||||
//连接信号与槽
|
||||
//断开所有其他的信号与槽连接
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(write_ack()), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
//36->24
|
||||
|
@ -630,4 +667,194 @@ void Widget::on_pushButton_7_clicked()
|
|||
sent_comand.append(crc_16_val);
|
||||
|
||||
Serial_port->write(sent_comand);
|
||||
|
||||
//连接信号与槽
|
||||
//断开所有其他的信号与槽连接
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(write_ack()), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
void Widget::on_Get_ID_pushButton_clicked()
|
||||
{
|
||||
QByteArray sent_comand;
|
||||
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||
sent_comand.append(static_cast<uint8_t>(0x03));
|
||||
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||
sent_comand.append(static_cast<uint8_t>(0x37));
|
||||
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||
sent_comand.append(static_cast<uint8_t>(0x04));
|
||||
sent_comand.append(static_cast<uint8_t>(0xF1));
|
||||
sent_comand.append(static_cast<uint8_t>(0xE6));
|
||||
|
||||
Serial_port->write(sent_comand);
|
||||
|
||||
//连接信号与槽
|
||||
//断开所有其他的信号与槽连接
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(getIdData()), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
uint32_t PassWord[2] = {0};
|
||||
uint32_t key[] = {0x509770ff, 0x29c6b369, 0x4da2c125, 0x0};
|
||||
void Widget::getIdData()
|
||||
{
|
||||
uint32_t ID_1;
|
||||
uint32_t ID_2;
|
||||
// 接收数据,一会再接收,以便接收超长数据
|
||||
QByteArray readed_data;
|
||||
while(Serial_port->waitForReadyRead(UART_WAIT_MS)==true);
|
||||
readed_data.append(Serial_port -> readAll());
|
||||
|
||||
//防止干扰
|
||||
while(readed_data.length() > 1)
|
||||
{
|
||||
qDebug() << readed_data.length();
|
||||
qDebug() << readed_data;
|
||||
if(readed_data[0] != static_cast<char>(0x30) || readed_data[1] != static_cast<char>(0x03))
|
||||
{
|
||||
readed_data.remove(0, 1);
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t read_comand_u8[readed_data.length()];
|
||||
for(int i = 0; i < readed_data.length(); i++)
|
||||
{
|
||||
read_comand_u8[i] = readed_data[i];
|
||||
}
|
||||
if(readed_data.length() >= 13)
|
||||
if(modbusCrc16(read_comand_u8, 11) >> 8 == read_comand_u8[11] && (modbusCrc16(read_comand_u8, 11) & 0x00FF) == read_comand_u8[12])
|
||||
{
|
||||
ui->ID_lineEdit->clear();
|
||||
ID_1 = read_comand_u8[3] << 24 | read_comand_u8[4] << 16 | read_comand_u8[5] << 8 | read_comand_u8[6];
|
||||
QString ID_1_Str = QString::number(ID_1, 16).toUpper().rightJustified(2, '0').prepend("0x");
|
||||
ID_2 = read_comand_u8[7] << 24 | read_comand_u8[8] << 16 | read_comand_u8[9] << 8 | read_comand_u8[10];
|
||||
QString ID_2_Str = QString::number(ID_2, 16).toUpper().rightJustified(2, '0').prepend("0x");
|
||||
ui->ID_lineEdit->setText(ID_1_Str + " " + ID_2_Str);
|
||||
qDebug("%x %x",ID_1, ID_2);
|
||||
|
||||
key[3] = ID_1;
|
||||
PassWord[0] = ID_1;
|
||||
PassWord[1] = ID_2;
|
||||
|
||||
tea_encrypt(PassWord, key);
|
||||
|
||||
ui->Password_lineEdit->clear();
|
||||
QString PW_1_Str = QString::number(PassWord[0], 16).toUpper().rightJustified(2, '0').prepend("0x");
|
||||
QString PW_2_Str = QString::number(PassWord[1], 16).toUpper().rightJustified(2, '0').prepend("0x");
|
||||
qDebug("%x %x",PassWord[0], PassWord[1]);
|
||||
ui->Password_lineEdit->setText(PW_1_Str + " " + PW_2_Str);
|
||||
}
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
|
||||
// 点亮控件
|
||||
ui->Password_lineEdit->setEnabled(true);
|
||||
ui->ID_lineEdit->setEnabled(true);
|
||||
ui->label_10->setEnabled(true);
|
||||
ui->label_11->setEnabled(true);
|
||||
ui->Trans_password_pushButton->setEnabled(true);
|
||||
}
|
||||
|
||||
void Widget::tea_encrypt(uint32_t *v, uint32_t *k)
|
||||
{
|
||||
uint32_t v0 = v[0], v1 = v[1], sum = 0, i; // 初始化v0, v1, sum
|
||||
uint32_t delta = 0x9e3779b9; // 初始化delta
|
||||
uint32_t k1 = k[1], k3 = k[3], k0 = k[0], k2 = k[2]; // 初始化密钥k
|
||||
for (i = 0; i < 32; i++) { // 执行32轮加密
|
||||
sum += delta;
|
||||
v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
|
||||
v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
|
||||
}
|
||||
v[0] = v0; // 返回加密结果
|
||||
v[1] = v1;
|
||||
}
|
||||
|
||||
void Widget::on_Trans_password_pushButton_clicked()
|
||||
{
|
||||
QByteArray sent_comand;
|
||||
|
||||
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||
sent_comand.append(static_cast<uint8_t>(0x10));
|
||||
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||
sent_comand.append(static_cast<uint8_t>(0x3B));
|
||||
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||
sent_comand.append(static_cast<uint8_t>(0x04));
|
||||
sent_comand.append(static_cast<uint8_t>(0x08));
|
||||
sent_comand.append(static_cast<uint8_t>(PassWord[0] >> 24));
|
||||
sent_comand.append(static_cast<uint8_t>(PassWord[0] >> 16));
|
||||
sent_comand.append(static_cast<uint8_t>(PassWord[0] >> 8));
|
||||
sent_comand.append(static_cast<uint8_t>(PassWord[0] >> 0));
|
||||
sent_comand.append(static_cast<uint8_t>(PassWord[1] >> 24));
|
||||
sent_comand.append(static_cast<uint8_t>(PassWord[1] >> 16));
|
||||
sent_comand.append(static_cast<uint8_t>(PassWord[1] >> 8));
|
||||
sent_comand.append(static_cast<uint8_t>(PassWord[1] >> 0));
|
||||
|
||||
uint8_t sent_comand_u8[sent_comand.length()];
|
||||
for(int i = 0; i < sent_comand.length(); i++)
|
||||
{
|
||||
sent_comand_u8[i] = sent_comand[i];
|
||||
}
|
||||
|
||||
uint16_t crc_16_val = modbusCrc16(sent_comand_u8, sent_comand.length());
|
||||
|
||||
sent_comand.append(crc_16_val >> 8);
|
||||
sent_comand.append(crc_16_val);
|
||||
|
||||
Serial_port->write(sent_comand);
|
||||
|
||||
//连接信号与槽
|
||||
//断开所有其他的信号与槽连接
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(write_ack()), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
void Widget::on_pushButton_8_clicked()
|
||||
{
|
||||
QByteArray sent_comand;
|
||||
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||
sent_comand.append(static_cast<uint8_t>(0x10));
|
||||
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||
sent_comand.append(static_cast<uint8_t>(0x1A));
|
||||
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||
sent_comand.append(static_cast<uint8_t>(0x01));
|
||||
sent_comand.append(static_cast<uint8_t>(0x02));
|
||||
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||
sent_comand.append(static_cast<uint8_t>(0x01));
|
||||
sent_comand.append(static_cast<uint8_t>(0x3C));
|
||||
sent_comand.append(static_cast<uint8_t>(0x3B));
|
||||
Serial_port->write(sent_comand);
|
||||
}
|
||||
|
||||
void Widget::write_ack()
|
||||
{
|
||||
// 接收数据,一会再接收,以便接收超长数据
|
||||
QByteArray readed_data;
|
||||
while(Serial_port->waitForReadyRead(UART_WAIT_MS)==true);
|
||||
readed_data.append(Serial_port -> readAll());
|
||||
|
||||
//防止干扰
|
||||
while(readed_data.length() > 1)
|
||||
{
|
||||
qDebug() << readed_data.length();
|
||||
qDebug() << readed_data;
|
||||
if(readed_data[0] != static_cast<char>(0x30) || readed_data[1] != static_cast<char>(0x10))
|
||||
{
|
||||
readed_data.remove(0, 1);
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t read_comand_u8[readed_data.length()];
|
||||
for(int i = 0; i < readed_data.length(); i++)
|
||||
{
|
||||
read_comand_u8[i] = readed_data[i];
|
||||
}
|
||||
|
||||
if(modbusCrc16(read_comand_u8, readed_data.length()-2) >> 8 == read_comand_u8[readed_data.length()-2] && (modbusCrc16(read_comand_u8, readed_data.length()-2) & 0x00FF) == read_comand_u8[readed_data.length()-1])
|
||||
{
|
||||
QMessageBox::information(this, "提示", "数据下发成功");
|
||||
}
|
||||
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||
}
|
||||
|
|
11
widget.h
11
widget.h
|
@ -10,6 +10,7 @@
|
|||
#include <QRegExp>
|
||||
#include <QRegExpValidator>
|
||||
#include <QLabel>
|
||||
#include <QThread>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui { class Widget; }
|
||||
|
@ -32,6 +33,8 @@ private slots:
|
|||
|
||||
//槽函数
|
||||
void get_configdata(void);
|
||||
void getIdData(void);
|
||||
void write_ack(void);
|
||||
|
||||
void on_pushButton_clicked();
|
||||
|
||||
|
@ -47,6 +50,12 @@ private slots:
|
|||
|
||||
void on_pushButton_7_clicked();
|
||||
|
||||
void on_Get_ID_pushButton_clicked();
|
||||
|
||||
void on_Trans_password_pushButton_clicked();
|
||||
|
||||
void on_pushButton_8_clicked();
|
||||
|
||||
private:
|
||||
Ui::Widget *ui;
|
||||
//关闭窗口提示
|
||||
|
@ -57,6 +66,8 @@ private:
|
|||
void able__(void);
|
||||
//crc校验
|
||||
uint16_t modbusCrc16(const uint8_t *data, const int len);
|
||||
//加密
|
||||
void tea_encrypt(uint32_t *v, uint32_t *k);
|
||||
//串口
|
||||
QSerialPort * Serial_port;
|
||||
|
||||
|
|
111
widget.ui
111
widget.ui
|
@ -1559,6 +1559,117 @@ border-radius: 10px; /* 圆角半径 */
|
|||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="encrypt_tab">
|
||||
<attribute name="title">
|
||||
<string>加密</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_7">
|
||||
<item row="0" column="0">
|
||||
<layout class="QGridLayout" name="gridLayout_6" columnstretch="3,2,5">
|
||||
<item row="0" column="0">
|
||||
<widget class="QPushButton" name="Get_ID_pushButton">
|
||||
<property name="text">
|
||||
<string>获取ID并计算</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QLabel" name="label_10">
|
||||
<property name="text">
|
||||
<string>唯一ID</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="QLineEdit" name="ID_lineEdit">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>160</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QPushButton" name="Trans_password_pushButton">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>下发密文</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QLabel" name="label_11">
|
||||
<property name="text">
|
||||
<string>密文</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="2">
|
||||
<widget class="QLineEdit" name="Password_lineEdit">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>160</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QPushButton" name="pushButton_8">
|
||||
<property name="text">
|
||||
<string>复位</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label_12">
|
||||
<property name="text">
|
||||
<string>下发密文后需复位</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
|
|
Loading…
Reference in New Issue