增加串口发送文件功能
This commit is contained in:
parent
36c2d4c9c1
commit
65ee0774dc
@ -24,6 +24,7 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
||||
qt_add_executable(Off-line-Tools
|
||||
MANUAL_FINALIZATION
|
||||
${PROJECT_SOURCES}
|
||||
FileSenderThread.h
|
||||
)
|
||||
# Define target properties for Android with Qt 6 as:
|
||||
# set_property(TARGET Off-line-Tools APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
|
||||
|
||||
55
FileSenderThread.h
Normal file
55
FileSenderThread.h
Normal file
@ -0,0 +1,55 @@
|
||||
#ifndef FILESENDERTHREAD_H
|
||||
#define FILESENDERTHREAD_H
|
||||
|
||||
#include <QThread>
|
||||
#include <QSerialPort>
|
||||
#include <QByteArray>
|
||||
#include <QFileDialog>
|
||||
#include <QDebug>
|
||||
|
||||
class FileSenderThread : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit FileSenderThread(QSerialPort *serial, const QString &filePath, QObject *parent = nullptr)
|
||||
: QThread(parent), m_serial(serial), m_filePath(filePath) {}
|
||||
|
||||
void run() override {
|
||||
// 打开文件
|
||||
QFile file(m_filePath);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
emit errorOccurred("无法打开文件");
|
||||
return;
|
||||
}
|
||||
|
||||
// 读取文件并发送
|
||||
QByteArray data = file.readAll();
|
||||
file.close();
|
||||
|
||||
// 将数据发送出去
|
||||
// int offset = 0;
|
||||
// const int chunkSize = 256; // 每次发送的最大数据量
|
||||
// while (offset < data.size()) {
|
||||
// QByteArray chunk = data.mid(offset, chunkSize);
|
||||
// m_serial->write(chunk);
|
||||
// offset += chunkSize;
|
||||
|
||||
// // 等待数据发送完成
|
||||
// m_serial->waitForBytesWritten(100);
|
||||
// QThread::msleep(10); // 可适当延时,以防止数据发送过快
|
||||
// }
|
||||
qDebug() << "文件已发送";
|
||||
emit finished();
|
||||
}
|
||||
|
||||
signals:
|
||||
void errorOccurred(const QString &errorMessage);
|
||||
void finished();
|
||||
|
||||
private:
|
||||
QSerialPort *m_serial;
|
||||
QString m_filePath;
|
||||
};
|
||||
|
||||
#endif // FILESENDERTHREAD_H
|
||||
120
mainwindow.cpp
120
mainwindow.cpp
@ -1,12 +1,12 @@
|
||||
#include "mainwindow.h"
|
||||
#include "./ui_mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
#include <QSerialPort>
|
||||
#include <QSerialPortInfo>
|
||||
#include <QMessageBox>
|
||||
#include <QDebug>
|
||||
#include <QStandardItemModel>
|
||||
#include <QStandardItem>
|
||||
|
||||
#include "FileSenderThread.h"
|
||||
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
@ -37,12 +37,19 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
qDebug() << "Serial Number:" << info.serialNumber();
|
||||
qDebug() << "System Location:" << info.systemLocation();
|
||||
qDebug() << "-----------------------------";
|
||||
if (info.manufacturer().compare("GenesysLogic") == 0 ||info.manufacturer().compare("Espressif") == 0) {
|
||||
if (info.manufacturer().compare("GenesysLogic") == 0 ||info.manufacturer().compare("Espressif") == 0 ||info.manufacturer().compare("Microsoft") == 0) {
|
||||
// 完全匹配
|
||||
#ifdef Q_OS_WIN
|
||||
if(info.systemLocation().startsWith("\\\\.\\COM"))
|
||||
{
|
||||
portList << info.portName();
|
||||
}
|
||||
#elif defined(Q_OS_MAC)
|
||||
if(info.systemLocation().startsWith("/dev/tty."))
|
||||
{
|
||||
portList << info.systemLocation();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
@ -135,7 +142,7 @@ void MainWindow::on_pushButton_clicked()
|
||||
|
||||
// 设置串口信息
|
||||
serial->setPortName(selectedPort);
|
||||
serial->setBaudRate(QSerialPort::Baud9600); // 设置波特率
|
||||
serial->setBaudRate(QSerialPort::Baud115200); // 设置波特率
|
||||
serial->setDataBits(QSerialPort::Data8); // 数据位
|
||||
serial->setParity(QSerialPort::NoParity); // 校验位
|
||||
serial->setStopBits(QSerialPort::OneStop); // 停止位
|
||||
@ -167,3 +174,108 @@ void MainWindow::onReadyRead()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_4_clicked()
|
||||
{
|
||||
QString fileName = QFileDialog::getOpenFileName(
|
||||
this,
|
||||
"选择文件",
|
||||
QDir::homePath(),
|
||||
"All Files (*.*)"
|
||||
);
|
||||
|
||||
if (fileName.isEmpty()) {
|
||||
qDebug() << "未选择文件";
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建一个新的发送线程
|
||||
FileSenderThread *fileSenderThread = new FileSenderThread(serial, fileName, this);
|
||||
|
||||
// 连接信号和槽
|
||||
connect(fileSenderThread, &FileSenderThread::finished, this, [this, fileSenderThread]() {
|
||||
// 文件发送完成
|
||||
qDebug() << "文件发送完成";
|
||||
fileSenderThread->deleteLater();
|
||||
});
|
||||
connect(fileSenderThread, &FileSenderThread::errorOccurred, this, [](const QString &errorMessage) {
|
||||
// 文件发送出错
|
||||
qDebug() << "错误:" << errorMessage;
|
||||
});
|
||||
|
||||
// 启动线程
|
||||
fileSenderThread->start();
|
||||
#if 0
|
||||
// 提取文件名(不包含路径)
|
||||
QFileInfo fileInfo(fileName);
|
||||
QString baseFileName = fileInfo.fileName();
|
||||
qDebug() << "文件:" << baseFileName;
|
||||
|
||||
// 将文件名转换为字节数组
|
||||
QByteArray fileNameBytes = baseFileName.toUtf8();
|
||||
|
||||
// 只取前 18 个字节,确保不会超出数组的大小限制
|
||||
uint8_t Protocol_header[6] = {0xAA, 0x55, 0x02, 0x01, 0x00, 0x00}; // 固定部分
|
||||
|
||||
// 构造数据包
|
||||
QByteArray data;
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 6); // 添加固定数据
|
||||
|
||||
// 将文件名字节拷贝到指定位置
|
||||
int fileNameLength = qMin(fileNameBytes.size(), 48); // 文件名最大长度为18
|
||||
data.append(fileNameBytes.constData(), fileNameLength); //添加文件名
|
||||
|
||||
// 计算数据包的长度(去掉开头的0xAA, 0x55, 0x02, 0x01等)
|
||||
uint16_t dataLength = data.size() - 4; // 减去开头的4个字节(0xAA, 0x55, 0x02, 0x01)
|
||||
data[5] = dataLength & 0xFF; // 设置低字节
|
||||
data[4] = (dataLength >> 8) & 0xFF; // 设置高字节
|
||||
qDebug() << "dataLength:" << dataLength;
|
||||
// 计算CRC16 Modbus值
|
||||
uint16_t crcValue = crc16_modbus(reinterpret_cast<const uint8_t*>(fileNameBytes.constData()), fileNameLength);
|
||||
qDebug() << "fileNameLength:" << fileNameLength << " crcValue:" << crcValue;
|
||||
// 将 CRC 添加到数据包
|
||||
data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue)); // 添加 CRC16
|
||||
|
||||
// 发送数据
|
||||
if (serial->isOpen()) {
|
||||
serial->write(data);
|
||||
qDebug() << "数据发送成功";
|
||||
} else {
|
||||
qDebug() << "串口未打开";
|
||||
}
|
||||
|
||||
data.clear();
|
||||
Protocol_header[3] = 0x02;
|
||||
data.append(reinterpret_cast<char*>(Protocol_header), 6); // 添加固定数据
|
||||
uint8_t buffer[128] = {0x01,0x7f,0xff};
|
||||
data.append(reinterpret_cast<char*>(buffer),128);
|
||||
dataLength = data.size() - 4;
|
||||
data[5] = dataLength & 0xFF; // 设置低字节
|
||||
data[4] = (dataLength >> 8) & 0xFF; // 设置高字节
|
||||
crcValue = crc16_modbus(reinterpret_cast<const uint8_t*>(buffer), 128);
|
||||
qDebug() << "fileNameLength:" << fileNameLength << " crcValue:" << crcValue;
|
||||
data.append(reinterpret_cast<char*>(&crcValue), sizeof(crcValue)); // 添加 CRC16
|
||||
// 发送数据
|
||||
if (serial->isOpen()) {
|
||||
serial->write(data);
|
||||
qDebug() << "数据发送成功";
|
||||
} else {
|
||||
qDebug() << "串口未打开";
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// CRC16 (Modbus) 计算函数
|
||||
uint16_t MainWindow::crc16_modbus(const uint8_t *data, uint16_t len) {
|
||||
uint16_t crc = 0xFFFF; // 初始化 CRC 值
|
||||
for (int i = 0; i < len; i++) {
|
||||
crc ^= data[i]; // 与数据进行异或
|
||||
for (int j = 0; j < 8; j++) { // 遍历每一位
|
||||
if (crc & 0x0001) // 如果最低位为 1
|
||||
crc = (crc >> 1) ^ 0xA001; // 右移一位并异或
|
||||
else
|
||||
crc = crc >> 1; // 否则仅右移
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QSerialPort>
|
||||
#include <QFileDialog>
|
||||
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
@ -18,11 +19,14 @@ class MainWindow : public QMainWindow
|
||||
public:
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
uint16_t crc16_modbus(const uint8_t *data, uint16_t len);
|
||||
|
||||
private slots:
|
||||
void on_pushButton_clicked();
|
||||
void onReadyRead(); // 串口数据接收的槽
|
||||
void onCellClicked(const QModelIndex &index);
|
||||
void on_pushButton_4_clicked();
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
QSerialPort *serial; // 将 serial 定义为成员对象
|
||||
|
||||
Loading…
Reference in New Issue
Block a user