优化tcp发送数据

This commit is contained in:
zhangsheng 2025-03-01 17:36:57 +08:00
parent b5038030c6
commit 5018130ea1

View File

@ -363,6 +363,7 @@ void MainWindow::OnButtonGroup(QAbstractButton * slot_btn)
Tachometer *tachometer = new Tachometer(); Tachometer *tachometer = new Tachometer();
tachometer->setWindowModality(Qt::ApplicationModal); tachometer->setWindowModality(Qt::ApplicationModal);
tachometer->show(); tachometer->show();
} }
} }
if(slot_btn != NULL && ui->pushButton_alarm->isChecked()){ if(slot_btn != NULL && ui->pushButton_alarm->isChecked()){
@ -483,54 +484,64 @@ void MainWindow::sendUpgradePackage(int slot)
// 读取文件内容 // 读取文件内容
QByteArray fileData = file.readAll(); QByteArray fileData = file.readAll();
int fileSize = fileData.size(); int fileSize = fileData.size();
qDebug() << "fileSize1" << fileSize; //fileSize = myHtonl(fileSize);
fileSize = myHtonl(fileSize);
qDebug() << "fileSize2" << fileSize;
qDebug() << "fileSize" << fileSize <<endl;
// 创建 PackageHead 结构体 // 创建 PackageHead 结构体
PackageHead header = { {0xAA, 0x55, 0xAA}, 3, fileSize,0,{} }; PackageHead header = { {0xAA, 0x55, 0xAA}, 3, fileSize,0,{} };
// 计算文件的 CRC 校验和 // 计算文件的 CRC 校验和
qDebug() << "filheader.file_md5" << slot <<endl; qDebug() << "filheader.file_md5" << slot <<endl;
UpgradeCardReq upgrade_car_req; UpgradeCardReq upgrade_car_req;
uint8_t card_id;
if(slot == 0) if(slot == 0)
upgrade_car_req.card_id = 0xFF; card_id = 0xFF;
else else
upgrade_car_req.card_id = slot & 0xFF; card_id = slot & 0xFF;
header.crc = calculate_crc(upgrade_car_req.card_id,fileData); header.crc = calculate_crc(upgrade_car_req.card_id,fileData);
header.len = fileSize;
qDebug() << "filheader.crc" << header.crc; qDebug() << "filheader.crc" << header.crc;
// 发送头部数据 // 发送头部数据
QByteArray packet(reinterpret_cast<char*>(&header), sizeof(PackageHead)); //QByteArray packet(reinterpret_cast<char*>(&header), sizeof(PackageHead));
// 发送文件内容 // 发送文件内容
socket.write(packet); // 发送头部 // socket.write(packet); // 发送头部
// socket.waitForBytesWritten();
// QByteArray packet2(reinterpret_cast<char*>(&upgrade_car_req), sizeof(UpgradeCardReq));
// socket.write(packet2); // 发送头部
// socket.waitForBytesWritten();
// socket.write(fileData); // 发送文件数据
// socket.waitForBytesWritten();
//QByteArray send_buf;
char *send_buf = NULL;
send_buf = (char *)malloc(sizeof(PackageHead) + sizeof(UpgradeCardReq) + fileData.size() + 1);
memset(send_buf,0,sizeof(PackageHead) + sizeof(UpgradeCardReq) + fileData.size() + 1);
memcpy(send_buf, &header, sizeof(PackageHead));
memcpy(send_buf + sizeof(PackageHead), &card_id, 1);
memcpy(send_buf + sizeof(PackageHead) + 1, fileData.data(), fileData.size());
socket.write(send_buf);
socket.waitForBytesWritten(); socket.waitForBytesWritten();
// 发送头部
// qDebug() << "File sent successfully";
// // 等待服务器响应(根据需要处理响应)
QByteArray packet2(reinterpret_cast<char*>(&upgrade_car_req), sizeof(UpgradeCardReq)); // if (socket.waitForReadyRead()) {
socket.write(packet2); // 发送头部 // QByteArray response = socket.readAll();
socket.waitForBytesWritten(); // UpgradeRsp resp;
// QByteArray byteArray = response.mid(sizeof(PackageHead));
socket.write(fileData); // 发送文件数据 // QDataStream stream(&byteArray, QIODevice::ReadOnly);
socket.waitForBytesWritten(); // stream >> resp.code ;
// if(resp.code == 1){
qDebug() << "File sent successfully"; // QMessageBox::information(this, QStringLiteral("提示"), "上传成功!");
// 等待服务器响应(根据需要处理响应) // }
// qDebug() << "Server response: " << resp.code;
if (socket.waitForReadyRead()) { // }
QByteArray response = socket.readAll();
UpgradeRsp resp;
QByteArray byteArray = response.mid(sizeof(PackageHead));
QDataStream stream(&byteArray, QIODevice::ReadOnly);
stream >> resp.code ;
if(resp.code == 1){
QMessageBox::information(this, QStringLiteral("提示"), "上传成功!");
}
qDebug() << "Server response: " << resp.code;
}
// 关闭文件和连接 // 关闭文件和连接
file.close(); file.close();
socket.disconnectFromHost(); socket.disconnectFromHost();
if(send_buf != NULL)
free(send_buf);
} }