QT打开二进制文件(.bin),串口定时发送

本例由《qt开发环境 - 简易二进制文件打开,串口自发自收》更改来。(由qt官方terminal demo 修改)

实现功能:打开.bin文件,显示文件内容

          通过串口按固定字节大小发送文件

          显示串口收到的内容

下面是源代码:

代码下载地址:http://download.csdn.net/download/zn2857/10194028

/****************************************************************************
**
** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "console.h"
#include "settingsdialog.h"

#include <QMessageBox>
#include <QLabel>
#include <QtSerialPort/QSerialPort>

#include <QFile>
#include <QFileDialog>
#include <QDir>
#include <QTextStream>
#include <QDataStream>
#include <QTimer>


#define APP_DISPLAY_SIZE 64

//! [0]
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
//! [0]
    ui->setupUi(this);
//    console = new Console;
//    console->setEnabled(false);
//    setCentralWidget(console);
//! [1]
    serial = new QSerialPort(this);
//! [1]
    settings = new SettingsDialog;

    ui->actionConnect->setEnabled(true);
    ui->actionDisconnect->setEnabled(false);
    ui->actionQuit->setEnabled(true);
    ui->actionConfigure->setEnabled(true);

    status = new QLabel;
    ui->statusBar->addWidget(status);

    initActionsConnections();

    connect(serial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error),
            this, &MainWindow::handleError);

//! [2]
    connect(serial, &QSerialPort::readyRead, this, &MainWindow::readData);
//! [2]
//    connect(console, &Console::getData, this, &MainWindow::writeData);
//! [3]
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerTransDate()));
    ulNum = 0;
//    timer->start(1000);
//    setCentralWidget(MainWindow);
}
//! [3]

MainWindow::~MainWindow()
{
    delete settings;
    delete ui;
}


//! [4]
void MainWindow::openSerialPort()
{
    SettingsDialog::Settings p = settings->settings();
    serial->setPortName(p.name);
    serial->setBaudRate(p.baudRate);
    serial->setDataBits(p.dataBits);
    serial->setParity(p.parity);
    serial->setStopBits(p.stopBits);
    serial->setFlowControl(p.flowControl);
    if (serial->open(QIODevice::ReadWrite)) {
//        console->setEnabled(true);
//        console->setLocalEchoEnabled(p.localEchoEnabled);
        ui->actionConnect->setEnabled(false);
        ui->actionDisconnect->setEnabled(true);
        ui->actionConfigure->setEnabled(false);
        showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
                          .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
                          .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
    } else {
        QMessageBox::critical(this, tr("Error"), serial->errorString());

        showStatusMessage(tr("Open error"));
    }
}
//! [4]

//! [5]
void MainWindow::closeSerialPort()
{
    if (serial->isOpen())
        serial->close();
//    console->setEnabled(false);
    ui->actionConnect->setEnabled(true);
    ui->actionDisconnect->setEnabled(false);
    ui->actionConfigure->setEnabled(true);
    showStatusMessage(tr("Disconnected"));
}
//! [5]

void MainWindow::about()
{
    QMessageBox::about(this, tr("About Simple Terminal"),
                       tr("The <b>Simple Terminal</b> example demonstrates how to "
                          "use the Qt Serial Port module in modern GUI applications "
                          "using Qt, with a menu bar, toolbars, and a status bar."));
}

//! [6]
void MainWindow::writeData(const QByteArray &data)
{
    serial->write(data);
}
//! [6]

//! [7]
void MainWindow::readData()
{
//    QByteArray data = serial->readAll();

//    console->putData(data);
    QByteArray temp = serial->readAll();
    QString buf;

        if(!temp.isEmpty()){
            ui->textBrowser->setTextColor(Qt::black);
//                if(chrReceive->isChecked()){
//                    buf = temp;
//                }else if(hexReceive->isChecked()){
                    for(int i = 0; i < temp.count(); i++){
                        QString s;
                        s.sprintf("%02X ", (unsigned char)temp.at(i));
                        buf += s;
                    }
//                }
//            ui->textBrowser->setText(ui->textBrowser->document()->toPlainText() + buf);
            ui->textBrowser->append(buf);

            QTextCursor cursor = ui->textBrowser->textCursor();
            cursor.movePosition(QTextCursor::End);
            ui->textBrowser->setTextCursor(cursor);

            ui->receivebyteLcdNumber->display(ui->receivebyteLcdNumber->value() + temp.size());
//            ui->statusBar->showMessage(tr("成功读取%1字节数据").arg(temp.size()));

        }

}
//! [7]

//! [8]
void MainWindow::handleError(QSerialPort::SerialPortError error)
{
    if (error == QSerialPort::ResourceError) {
        QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
        closeSerialPort();
    }
}
//! [8]

void MainWindow::initActionsConnections()
{
    connect(ui->actionConnect, &QAction::triggered, this, &MainWindow::openSerialPort);
    connect(ui->actionDisconnect, &QAction::triggered, this, &MainWindow::closeSerialPort);
    connect(ui->actionQuit, &QAction::triggered, this, &MainWindow::close);
    connect(ui->actionConfigure, &QAction::triggered, settings, &SettingsDialog::show);
    connect(ui->actionClear, &QAction::triggered, this, &MainWindow::clearTextBrowser);
    connect(ui->actionAbout, &QAction::triggered, this, &MainWindow::about);
    connect(ui->actionAboutQt, &QAction::triggered, qApp, &QApplication::aboutQt);
}

void MainWindow::showStatusMessage(const QString &message)
{
    status->setText(message);
}

void MainWindow::on_pushButton_clicked()
{
    timer->start(500);
}

void MainWindow::clearTextBrowser()
{
    ui->textBrowser->setText(NULL);
}

void MainWindow::on_openFileButton_clicked()
{
    //get file name
    fileName = QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
//    qDebug()<< "fileName is" << fileName;
    ui->filePathLineEdit->setText (fileName);
    if(fileName.isEmpty())
    {
        QMessageBox::information(this,"Error Message", "Please Select a Text File");
        return;
    }
    QFileInfo *pcsfileInfo = new QFileInfo(fileName);
    binSize = pcsfileInfo->size ();

    QFile* file = new QFile;
    file->setFileName(fileName);
    bool ok = file->open(QIODevice::ReadOnly);
    if(ok)
    {
//        QTextStream in(file);
//        ui->textEdit->setText(in.readAll());//read all context from the file
    }
    else
    {
        QMessageBox::information(this,"Error Message", "File Open Error" + file->errorString());
        return;
    }
    QDataStream in(file);
    char * binByte = new char[binSize];
    in.setVersion (QDataStream::Qt_5_9);

    ui->statusBar->showMessage(tr("准备读取数据"));
    in.readRawData (binByte, binSize);      //读出文件到缓存
    ui->statusBar->showMessage(tr("读取数据完毕"));

    tempByte = new QByteArray(binByte, binSize);                //格式转换

    QString *tempStr = new QString(tempByte->toHex ().toUpper ());

    //显示文件内容
    qint8 cnt = 1;
    qint16 kcnt = 0;
    for(qint64 i = 2; i < tempStr->size ();)
    {
        tempStr->insert (i, ' ');//每个字节之间空一格
        i += 3;
        cnt++;
        if(cnt == 8)//每8个字节空2格
        {
            tempStr->insert (i, ' ');
            i += 1;
        }
        if(cnt == 16)//每16个字节空一格
        {
            cnt = 1;
            kcnt ++;
            if(kcnt == 64)//每64行,即1K数据,空一行
            {
                kcnt = 0;
                tempStr->insert (i, '\n');
                i++;
            }
            tempStr->insert (i, '\n');
            i += 3;         //避免换行后开头一个空格,换行相当于从新插入
        }

    }
    ui->statusBar->showMessage(tr("准备显示"));
    ui->fileViewPlainTextEdit->insertPlainText (*tempStr);
    ui->statusBar->showMessage(tr("显示完毕"));

//    timer->start(1000);
//    serial->write(binByte,25);
    delete tempByte;
    delete[] binByte;
    delete tempStr;

    file->close ();
    delete file;
}
void MainWindow::timerTransDate()
{
    qint16 temp = 0;            //剩余待传数据
    qint16 FileSendEndFg;


    QFile *binFile = new QFile(fileName);
    binFile->open (QIODevice::ReadOnly);
    binFile->seek (ulNum * 1024);

    QDataStream in(binFile);
    char * binByte = new char[binSize];
    in.setVersion (QDataStream::Qt_5_9);

    in.readRawData (binByte, binSize);      //读出文件到缓存

    char * binLitByte = new char[64];//bin缓存
    static int binfileseek = 0;

    if(binfileseek > binSize)
    {
        binfileseek = 0;
        timer->stop();
        return;
    }
    memcpy (binLitByte, binByte + binfileseek, 64);
    binfileseek += 64;

    temp = binSize - 1024*ulNum;

    serial->write(binLitByte,64);

    delete binByte;

}


  • 11
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: Qt是一种跨平台的应用程序开发框架,不仅提供了GUI程序开发所需的控件等基本功能,还提供了网络通信等重要的功能模块。其中,UDP(User Datagram Protocol)是一种无连接的传输协议,在网络通信中应用广泛。 Qt提供了丰富的网络编程接口以及UDP相关的类和函数。使用Qt进行UDP通信可以方便快捷地实现网络数据传输功能。在进行UDP通信时,需要将要发送数据进行编码和解码,这时候就需要使用二进制文件进行数据传输二进制文件是一种以二进制形式来存储数据文件,相比于文本文件更加高效,并且可以直接进行存取操作。在网络通信中,二进制文件能够减少数据传输的大小,提高数据传输的速度。 Qt提供了QByteArray类用于存储和操作二进制数据,在UDP通信中可以利用QByteArray对二进制数据进行编码和解码,便于在网络传输过程中进行数据的处理和存储。 总之,使用Qt进行UDP通信时,二进制文件可以提高数据传输的效率和速度,而QByteArray类可以方便地进行二进制数据的存储和操作。 ### 回答2: Qt是一种跨平台的GUI开发框架,支持C++编程语言,在网络编程中也有广泛应用。UDP是一种无连接的传输协议,可以快速传输数据包,但是数据可靠性较差。 在Qt中使用UDP协议进行网络编程,可以通过QUdpSocket类实现。此类可以用于接收和发送UDP数据报。发送数据时,可以使用writeDatagram()函数将二进制文件转换为QByteArray类型并发送出去。接收数据时,使用readyRead信号和pendingDatagramSize()函数获取数据报的大小,再使用readDatagram()函数读取数据报并转换为二进制文件。 在传输大量二进制文件时,可以使用分包技术,将一个文件分成多个小数据包进行传输,接收端再将多个小数据包合并成完整的文件。另外,在传输时可以对数据进行压缩和加密处理,提高文件传输的安全性和效率。 总之,Qt UDP协议可以用于快速传输二进制文件,具有高效性和跨平台特性,为网络编程提供了良好的支持。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值