QT-读取二进制bin文件并发送

16 篇文章 0 订阅

这里的功能主要开发用于读取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();
}
  • 6
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值