283 lines
9.7 KiB
C++
283 lines
9.7 KiB
C++
#include "mainwindow.h"
|
||
#include "ui_mainwindow.h"
|
||
#include <QSerialPort>
|
||
#include <QSerialPortInfo>
|
||
#include <QMessageBox>
|
||
#include <QDebug>
|
||
#include <QStandardItemModel>
|
||
#include <QStandardItem>
|
||
#include "FileSenderThread.h"
|
||
#include "FileEncryptor.h"
|
||
|
||
|
||
MainWindow::MainWindow(QWidget *parent)
|
||
: QMainWindow(parent)
|
||
, ui(new Ui::MainWindow)
|
||
, serial(new QSerialPort(this)) // 在构造函数中初始化 serial
|
||
{
|
||
ui->setupUi(this);
|
||
ui->groupBox->setMaximumWidth(250);
|
||
ui->groupBox_2->setMaximumWidth(250);
|
||
// 连接串口的 readyRead 信号到槽函数
|
||
connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
|
||
#ifdef Q_OS_WIN
|
||
qDebug() << "当前是 Windows 平台";
|
||
#elif defined(Q_OS_MAC)
|
||
qDebug() << "当前是 macOS 平台";
|
||
#elif defined(Q_OS_LINUX)
|
||
qDebug() << "当前是 Linux 平台";
|
||
#else
|
||
qDebug() << "未知平台";
|
||
#endif
|
||
QStringList portList;
|
||
const auto serialPortList = QSerialPortInfo::availablePorts();
|
||
for (const QSerialPortInfo &info : serialPortList) {
|
||
qDebug() << "Port Name:" << info.portName();
|
||
qDebug() << "Description:" << info.description();
|
||
qDebug() << "Manufacturer:" << info.manufacturer();
|
||
qDebug() << "Serial Number:" << info.serialNumber();
|
||
qDebug() << "System Location:" << info.systemLocation();
|
||
qDebug() << "-----------------------------";
|
||
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
|
||
}
|
||
|
||
}
|
||
|
||
ui->comboBox->addItems(portList);
|
||
|
||
// 创建 QStandardItemModel(表格数据模型)
|
||
QStandardItemModel *model = new QStandardItemModel(3, 4); // 3行4列(包括序号列)
|
||
|
||
// 设置表头标签
|
||
model->setHorizontalHeaderLabels({"序号", "文件名", "烧录地址", "文件大小"});
|
||
|
||
// 填充表格数据(假设我们填充一些假数据)
|
||
for (int row = 0; row < 3; ++row) {
|
||
// 创建序号列(根据行号动态填充)
|
||
QStandardItem *serialNumberItem = new QStandardItem(QString::number(row + 1));
|
||
model->setItem(row, 0, serialNumberItem);
|
||
|
||
// 创建其他数据列
|
||
model->setItem(row, 1, new QStandardItem("example.txt"));
|
||
model->setItem(row, 2, new QStandardItem("0x1000"));
|
||
model->setItem(row, 3, new QStandardItem("1024"));
|
||
}
|
||
|
||
// 插入数据
|
||
model->setItem(0, 1, new QStandardItem("/Users/yu/Qt/Work_Space/Off-line-Tools"));
|
||
model->setItem(0, 2, new QStandardItem("0xffffffff"));
|
||
model->setItem(0, 3, new QStandardItem("0xffffffff"));
|
||
|
||
|
||
// 将模型与视图关联
|
||
ui->tableView->setModel(model);
|
||
|
||
// 隐藏默认的行号列
|
||
ui->tableView->verticalHeader()->setVisible(false);
|
||
// 设置默认行号列宽度
|
||
// ui->tableView->verticalHeader()->setFixedWidth(30); // 设定行号列的宽度为 像素
|
||
//设置列宽
|
||
ui->tableView->setColumnWidth(0,60);
|
||
ui->tableView->setColumnWidth(1,280);
|
||
ui->tableView->setColumnWidth(2,100);
|
||
ui->tableView->setColumnWidth(3,100);
|
||
|
||
// 连接 QTableView 的 clicked 信号到槽函数
|
||
connect(ui->tableView, &QTableView::clicked, this, &MainWindow::onCellClicked);
|
||
|
||
}
|
||
|
||
MainWindow::~MainWindow()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void MainWindow::onCellClicked(const QModelIndex &index) {
|
||
// 检查是否是第一列
|
||
if (index.column() == 0) {
|
||
// 获取选中的行
|
||
int row = index.row();
|
||
|
||
// 获取选择模型
|
||
QItemSelectionModel *selectionModel = ui->tableView->selectionModel();
|
||
|
||
// 选中整行
|
||
QItemSelection selection(ui->tableView->model()->index(row, 0), ui->tableView->model()->index(row, ui->tableView->model()->columnCount() - 1));
|
||
selectionModel->select(selection, QItemSelectionModel::Select);
|
||
}
|
||
qDebug() << "onCellClicked:" << index.column();
|
||
|
||
}
|
||
|
||
void MainWindow::on_pushButton_clicked()
|
||
{
|
||
// 获取用户在 comboBox 中选择的串口设备
|
||
QString selectedPort = ui->comboBox->currentText();
|
||
|
||
if (selectedPort.isEmpty()) {
|
||
QMessageBox::warning(this, "警告", "请选择一个串口设备");
|
||
return;
|
||
}
|
||
|
||
// 创建串口对象,如果已创建则先关闭
|
||
if (!serial) {
|
||
serial = new QSerialPort(this);
|
||
} else {
|
||
// 确保串口已打开才调用 close
|
||
if (serial->isOpen()) {
|
||
serial->close();
|
||
}
|
||
}
|
||
|
||
// 设置串口信息
|
||
serial->setPortName(selectedPort);
|
||
serial->setBaudRate(QSerialPort::Baud115200); // 设置波特率
|
||
serial->setDataBits(QSerialPort::Data8); // 数据位
|
||
serial->setParity(QSerialPort::NoParity); // 校验位
|
||
serial->setStopBits(QSerialPort::OneStop); // 停止位
|
||
serial->setFlowControl(QSerialPort::NoFlowControl); // 无流控
|
||
|
||
// 打开串口
|
||
if (serial->open(QIODevice::ReadWrite)) {
|
||
QMessageBox::information(this, "成功", "串口已成功打开");
|
||
|
||
// 这里可以执行更多的操作,比如发送数据、读取数据等
|
||
// 连接 readyRead 信号到槽函数
|
||
// connect(serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
|
||
// 示例:发送简单数据
|
||
QByteArray dataToSend = "Hello, Serial Port!\r\n";
|
||
serial->write(dataToSend);
|
||
} else {
|
||
QMessageBox::critical(this, "错误", "无法打开串口 " + selectedPort);
|
||
}
|
||
|
||
}
|
||
|
||
void MainWindow::onReadyRead()
|
||
{
|
||
while (serial->canReadLine()) {
|
||
QByteArray line = serial->readLine(); // 读取一行数据
|
||
qDebug() << "接收到一行数据: " << line;
|
||
|
||
// 处理数据
|
||
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|