这里的功能主要开发用于读取bin文件并发送到硬件内存中进行更新设备
以下是读取bin文件后发送的相关代码
以1024字节为一包进行发送
仅供参考
//通讯协议: 0x55,0xAA,datalen(2),total_packet(2),cur_packet(2), data(1024), checksum(1)
const static int PACKET_SIZE = 1024;//每次发送的字节数
const static int PACKET_HEAD_SIZE = 10; //协议字节数
void ReadBin::OnReadBin()
{
qDebug() << "==============";
QString filePath("D:/demo/QtWidgetsApplication1/vec.bin");
QFile file;
file.setFileName(filePath);
QFileInfo fileInfo(filePath);
auto fileLen = fileInfo.size();//待发送文件大小
auto sendCnt = (int)ceil(fileLen / (PACKET_SIZE * 1.0));//发送次数
auto lastLen = fileLen % PACKET_SIZE;//最后一次发送的大小
if (0 == lastLen) {//恰巧是PACKET_SIZE的整数倍
lastLen = PACKET_SIZE;
}
qDebug() << "fileLen,lastLen,sendCnt:" << fileLen << lastLen << sendCnt;
if (file.open(QIODevice::ReadOnly)) {
QDataStream dataStream(&file);
char* pBuf = new char[fileLen];
memset(pBuf, 0, fileLen);
dataStream.readRawData(pBuf, fileLen);
//qDebug()<<QString::asprintf("%02X %02X",(uint8_t)pBuf[0],(uint8_t)pBuf[1]);
//qDebug()<<QString::asprintf("%02X %02X",(uint8_t)pBuf[fileInfo.size()-2],(uint8_t)pBuf[fileInfo.size()-1]);
for (int i = 0; i < sendCnt; i++) {
#if 0 //测试文件原始数据输出
QString strAll;
if (i != sendCnt - 1) {
for (int k = 0; k < PACKET_SIZE; k++) {
strAll += QString::asprintf("%02X ", (uint8_t)pBuf[i * PACKET_SIZE + k]);
}
}
else {
for (int k = 0; k < lastLen; k++) {
strAll += QString::asprintf("%02X ", (uint8_t)pBuf[i * PACKET_SIZE + k]);
}
}
qDebug() << strAll;
continue;
#endif
if (i != sendCnt - 1) {
const int PACKET_LEN = PACKET_SIZE;
qDebug() << "---1----PACKET_LEN:" << PACKET_LEN;
//数据填充---------------------------------------------------
char tx_buf[PACKET_LEN] = { 0 };
//1: head
//tx_buf[0] = 0x55;
//tx_buf[1] = (uint8_t)0xAA;
//tx_buf[2] = PACKET_LEN >> 8;//数据长度
//tx_buf[3] = PACKET_LEN & 0xFF;
//tx_buf[4] = sendCnt >> 8;//总包数
//tx_buf[5] = sendCnt;
//tx_buf[6] = (i + 1) >> 8;//当前包数
//tx_buf[7] = i + 1;
//2: data
//memcpy(tx_buf + 8, pBuf + i * PACKET_SIZE, PACKET_SIZE);
memcpy(tx_buf, pBuf + i * PACKET_SIZE, PACKET_SIZE);
qDebug() << sizeof(pBuf);
//3: checksum
//int8_t checksum(0);
//for (int j = 0; j < PACKET_LEN - 1; j++) {
// checksum += tx_buf[j];
//}
//tx_buf[PACKET_LEN - 1] = checksum;
#if 1//debug out
QString strAll;
for (int k = 0; k < PACKET_LEN; k++) {
strAll += QString::asprintf("%02X ", (uint8_t)tx_buf[k]);
}
qDebug() << strAll;
#endif
//---------------------------------------------------------
//m_socket.writeDatagram(tx_buf, PACKET_LEN, QHostAddress("192.168.1.10"), 8888);
}
else {//最后一次发送
int PACKET_LEN = PACKET_HEAD_SIZE + lastLen;
qDebug() << "-----2----PACKET_LEN:" << PACKET_LEN;
//数据填充---------------------------------------------------
char* tx_buf = new char[PACKET_LEN];
memset(tx_buf, 0, PACKET_LEN);
//1: head
//tx_buf[0] = 0x55;
//tx_buf[1] = (uint8_t)0xAA;
//tx_buf[2] = PACKET_LEN >> 8;//数据长度
//tx_buf[3] = PACKET_LEN & 0xFF;
//tx_buf[4] = sendCnt >> 8;//总包数
//tx_buf[5] = sendCnt;
//tx_buf[6] = (i + 1) >> 8;//当前包数
//tx_buf[7] = i + 1;
//2: data
//memcpy(tx_buf + 8, pBuf + i * PACKET_SIZE, lastLen);
memcpy(tx_buf, pBuf + i * PACKET_SIZE, lastLen);
qDebug() << sizeof(pBuf);
//3: checksum
//int8_t checksum(0);
//for (int j = 0; j < PACKET_LEN - 1; j++) {
// checksum += tx_buf[j];
//}
//tx_buf[PACKET_LEN - 1] = checksum;
#if 1//debug out
QString strAll;
for (int k = 0; k < PACKET_LEN; k++) {
strAll += QString::asprintf("%02X ", (uint8_t)tx_buf[k]);
}
qDebug() << strAll;
#endif
//---------------------------------------------------------
//m_socket.writeDatagram(tx_buf, PACKET_LEN, QHostAddress("192.168.1.10"), 8888);
delete[]tx_buf;
}
//sleep(100);
}
delete[]pBuf;
}
file.close();
}