From 9aea2abcd83a3746bcbc83d441f8aab4a0fcf4e8 Mon Sep 17 00:00:00 2001 From: 95384 <664090429@qq.com> Date: Sat, 11 Jan 2025 09:47:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=A0=E5=AF=86=E9=83=A8?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- micro_climate_config.pro.user | 6 +- widget.cpp | 231 +++++++++++++++++++++++++++++++++- widget.h | 11 ++ widget.ui | 111 ++++++++++++++++ 4 files changed, 354 insertions(+), 5 deletions(-) diff --git a/micro_climate_config.pro.user b/micro_climate_config.pro.user index 6a5714a..22d6096 100644 --- a/micro_climate_config.pro.user +++ b/micro_climate_config.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -77,7 +77,7 @@ Desktop Qt 5.12.9 MinGW 32-bit Desktop Qt 5.12.9 MinGW 32-bit qt.qt5.5129.win32_mingw73_kit - 0 + 1 0 0 @@ -320,7 +320,7 @@ false true - E:/Y/QT/build-micro_climate_config-Desktop_Qt_5_12_9_MinGW_32_bit-Debug + E:/Y/QT/build-micro_climate_config-Desktop_Qt_5_12_9_MinGW_32_bit-Release 1 diff --git a/widget.cpp b/widget.cpp index 63048a5..1c9fc08 100644 --- a/widget.cpp +++ b/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(0x30)); + sent_comand.append(static_cast(0x03)); + sent_comand.append(static_cast(0x00)); + sent_comand.append(static_cast(0x37)); + sent_comand.append(static_cast(0x00)); + sent_comand.append(static_cast(0x04)); + sent_comand.append(static_cast(0xF1)); + sent_comand.append(static_cast(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(0x30) || readed_data[1] != static_cast(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(0x30)); + sent_comand.append(static_cast(0x10)); + sent_comand.append(static_cast(0x00)); + sent_comand.append(static_cast(0x3B)); + sent_comand.append(static_cast(0x00)); + sent_comand.append(static_cast(0x04)); + sent_comand.append(static_cast(0x08)); + sent_comand.append(static_cast(PassWord[0] >> 24)); + sent_comand.append(static_cast(PassWord[0] >> 16)); + sent_comand.append(static_cast(PassWord[0] >> 8)); + sent_comand.append(static_cast(PassWord[0] >> 0)); + sent_comand.append(static_cast(PassWord[1] >> 24)); + sent_comand.append(static_cast(PassWord[1] >> 16)); + sent_comand.append(static_cast(PassWord[1] >> 8)); + sent_comand.append(static_cast(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(0x30)); + sent_comand.append(static_cast(0x10)); + sent_comand.append(static_cast(0x00)); + sent_comand.append(static_cast(0x1A)); + sent_comand.append(static_cast(0x00)); + sent_comand.append(static_cast(0x01)); + sent_comand.append(static_cast(0x02)); + sent_comand.append(static_cast(0x00)); + sent_comand.append(static_cast(0x01)); + sent_comand.append(static_cast(0x3C)); + sent_comand.append(static_cast(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(0x30) || readed_data[1] != static_cast(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); } diff --git a/widget.h b/widget.h index 1ae637f..755f08a 100644 --- a/widget.h +++ b/widget.h @@ -10,6 +10,7 @@ #include #include #include +#include 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; diff --git a/widget.ui b/widget.ui index ab78150..25c2744 100644 --- a/widget.ui +++ b/widget.ui @@ -1559,6 +1559,117 @@ border-radius: 10px; /* 圆角半径 */ + + + 加密 + + + + + + + + 获取ID并计算 + + + + + + + 唯一ID + + + + + + + + 160 + 0 + + + + true + + + + + + + + 0 + 0 + + + + 下发密文 + + + + + + + 密文 + + + + + + + + 160 + 0 + + + + true + + + + + + + + + 复位 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + 下发密文后需复位 + + + + +