日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

qt开发环境 - 简易二进制文件打开,串口自发自收

發布時間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 qt开发环境 - 简易二进制文件打开,串口自发自收 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

qt版本:5.9.1

win版本:10.1706

下載:https://download.csdn.net/download/zn2857/10194028

改自上篇文章的串口助手,加入linEdit顯示文件路徑,加入新pushButton加載文件,

文件加載后顯示在串口發送窗口

短接串口助手收發,后可以實現把文件發送到textBrowser

目前效果一般般發送大文件十分的卡

下面是代碼

?

/**************************************************************************** ** ** 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> //! [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] } //! [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()) // { // buf = temp;// ui->textBrowser->setText(ui->textBrowser->document()->toPlainText() + buf); // ui->textBrowser->moveCursor(QTextCursor::End); // }if(!temp.isEmpty()){ // 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->moveCursor(QTextCursor::End);// //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() {serial->write(ui->textEdit->toPlainText().toLatin1()); }void MainWindow::clearTextBrowser() {ui->textBrowser->setText(NULL); }void MainWindow::on_openFileButton_clicked() {//get file nameQString 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);in.readRawData (binByte, binSize); //讀出文件到緩存QByteArray *tempByte = new QByteArray(binByte, binSize); //格式轉換delete[] binByte;QString *tempStr = new QString(tempByte->toHex ().toUpper ()); // delete tempByte;//顯示文件內容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->textEdit->insertPlainText (*tempStr);serial->write(*tempByte);delete tempByte;delete tempStr;file->close ();delete file; } /**************************************************************************** ** ** 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 "settingsdialog.h" #include "ui_settingsdialog.h"#include <QtSerialPort/QSerialPortInfo> #include <QIntValidator> #include <QLineEdit>QT_USE_NAMESPACEstatic const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A");SettingsDialog::SettingsDialog(QWidget *parent) :QDialog(parent),ui(new Ui::SettingsDialog) {ui->setupUi(this);intValidator = new QIntValidator(0, 4000000, this);ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert);connect(ui->applyButton, &QPushButton::clicked,this, &SettingsDialog::apply);connect(ui->serialPortInfoListBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this, &SettingsDialog::showPortInfo);connect(ui->baudRateBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this, &SettingsDialog::checkCustomBaudRatePolicy);connect(ui->serialPortInfoListBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this, &SettingsDialog::checkCustomDevicePathPolicy);fillPortsParameters();fillPortsInfo();updateSettings(); }SettingsDialog::~SettingsDialog() {delete ui; }SettingsDialog::Settings SettingsDialog::settings() const {return currentSettings; }void SettingsDialog::showPortInfo(int idx) {if (idx == -1)return;QStringList list = ui->serialPortInfoListBox->itemData(idx).toStringList();ui->descriptionLabel->setText(tr("Description: %1").arg(list.count() > 1 ? list.at(1) : tr(blankString)));ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(list.count() > 2 ? list.at(2) : tr(blankString)));ui->serialNumberLabel->setText(tr("Serial number: %1").arg(list.count() > 3 ? list.at(3) : tr(blankString)));ui->locationLabel->setText(tr("Location: %1").arg(list.count() > 4 ? list.at(4) : tr(blankString)));ui->vidLabel->setText(tr("Vendor Identifier: %1").arg(list.count() > 5 ? list.at(5) : tr(blankString)));ui->pidLabel->setText(tr("Product Identifier: %1").arg(list.count() > 6 ? list.at(6) : tr(blankString))); }void SettingsDialog::apply() {updateSettings();hide(); }void SettingsDialog::checkCustomBaudRatePolicy(int idx) {bool isCustomBaudRate = !ui->baudRateBox->itemData(idx).isValid();ui->baudRateBox->setEditable(isCustomBaudRate);if (isCustomBaudRate) {ui->baudRateBox->clearEditText();QLineEdit *edit = ui->baudRateBox->lineEdit();edit->setValidator(intValidator);} }void SettingsDialog::checkCustomDevicePathPolicy(int idx) {bool isCustomPath = !ui->serialPortInfoListBox->itemData(idx).isValid();ui->serialPortInfoListBox->setEditable(isCustomPath);if (isCustomPath)ui->serialPortInfoListBox->clearEditText(); }void SettingsDialog::fillPortsParameters() {ui->baudRateBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);ui->baudRateBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);ui->baudRateBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);ui->baudRateBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);ui->baudRateBox->addItem(tr("Custom"));ui->dataBitsBox->addItem(QStringLiteral("5"), QSerialPort::Data5);ui->dataBitsBox->addItem(QStringLiteral("6"), QSerialPort::Data6);ui->dataBitsBox->addItem(QStringLiteral("7"), QSerialPort::Data7);ui->dataBitsBox->addItem(QStringLiteral("8"), QSerialPort::Data8);ui->dataBitsBox->setCurrentIndex(3);ui->parityBox->addItem(tr("None"), QSerialPort::NoParity);ui->parityBox->addItem(tr("Even"), QSerialPort::EvenParity);ui->parityBox->addItem(tr("Odd"), QSerialPort::OddParity);ui->parityBox->addItem(tr("Mark"), QSerialPort::MarkParity);ui->parityBox->addItem(tr("Space"), QSerialPort::SpaceParity);ui->stopBitsBox->addItem(QStringLiteral("1"), QSerialPort::OneStop); #ifdef Q_OS_WINui->stopBitsBox->addItem(tr("1.5"), QSerialPort::OneAndHalfStop); #endifui->stopBitsBox->addItem(QStringLiteral("2"), QSerialPort::TwoStop);ui->flowControlBox->addItem(tr("None"), QSerialPort::NoFlowControl);ui->flowControlBox->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl);ui->flowControlBox->addItem(tr("XON/XOFF"), QSerialPort::SoftwareControl); }void SettingsDialog::fillPortsInfo() {ui->serialPortInfoListBox->clear();QString description;QString manufacturer;QString serialNumber;const auto infos = QSerialPortInfo::availablePorts();for (const QSerialPortInfo &info : infos) {QStringList list;description = info.description();manufacturer = info.manufacturer();serialNumber = info.serialNumber();list << info.portName()<< (!description.isEmpty() ? description : blankString)<< (!manufacturer.isEmpty() ? manufacturer : blankString)<< (!serialNumber.isEmpty() ? serialNumber : blankString)<< info.systemLocation()<< (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)<< (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);ui->serialPortInfoListBox->addItem(list.first(), list);}ui->serialPortInfoListBox->addItem(tr("Custom")); }void SettingsDialog::updateSettings() {currentSettings.name = ui->serialPortInfoListBox->currentText();if (ui->baudRateBox->currentIndex() == 4) {currentSettings.baudRate = ui->baudRateBox->currentText().toInt();} else {currentSettings.baudRate = static_cast<QSerialPort::BaudRate>(ui->baudRateBox->itemData(ui->baudRateBox->currentIndex()).toInt());}currentSettings.stringBaudRate = QString::number(currentSettings.baudRate);currentSettings.dataBits = static_cast<QSerialPort::DataBits>(ui->dataBitsBox->itemData(ui->dataBitsBox->currentIndex()).toInt());currentSettings.stringDataBits = ui->dataBitsBox->currentText();currentSettings.parity = static_cast<QSerialPort::Parity>(ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt());currentSettings.stringParity = ui->parityBox->currentText();currentSettings.stopBits = static_cast<QSerialPort::StopBits>(ui->stopBitsBox->itemData(ui->stopBitsBox->currentIndex()).toInt());currentSettings.stringStopBits = ui->stopBitsBox->currentText();currentSettings.flowControl = static_cast<QSerialPort::FlowControl>(ui->flowControlBox->itemData(ui->flowControlBox->currentIndex()).toInt());currentSettings.stringFlowControl = ui->flowControlBox->currentText();currentSettings.localEchoEnabled = ui->localEchoCheckBox->isChecked(); }

?

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的qt开发环境 - 简易二进制文件打开,串口自发自收的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 五月激情开心网 | 国产极品福利 | 成人拍拍拍 | 国产精品中文字幕在线观看 | 天天射日日干 | 午夜不卡久久精品无码免费 | 精品国产一区二区三区在线观看 | 欧美色xxxx| 午夜av大片 | 91麻豆精品91久久久久同性 | 91快射 | 久久久精选 | 久久看片 | 亚洲人成777| 日韩欧美三级在线观看 | 国产无遮挡18禁无码网站不卡 | 日韩精品人妻中文字幕 | 黄色特级片| 性一交一乱一色一免费无遮挡 | 午夜精品久久久久久久第一页按摩 | 日日弄天天弄美女bbbb | 最好看十大无码av | 乳色吐息在线观看 | 国产欧美啪啪 | 日本中文字幕有码 | 国产喷水福利在线视频 | 久久国产免费看 | 日韩精品高清在线 | 成人毛片在线免费观看 | 国产精品国产三级国产aⅴ 欧美bbbbbbbbbbbb18av | 日韩美女毛片 | 乱色精品无码一区二区国产盗 | 98视频在线 | 五月天激情婷婷 | 久久精品国产亚洲AV成人雅虎 | 91香蕉在线看 | 求一个黄色网址 | 青青色在线视频 | 色二区| 啪啪的网站 | 国产片91 | 天使色吧 | 亚洲成人黄色 | 涩涩综合 | 爱乃なみ加勒比在线播放 | 热99精品| 99久草| 精品人妻一区二区三区四区五区 | 午夜影院18 | 免费看麻豆 | 亚洲视频免费 | 91国产视频在线观看 | 夜夜激情| 青草视频免费在线观看 | 幸福宝在线观看 | 亚洲自拍偷拍精品视频 | 欧美成人高清 | 久操视频精品 | 亚洲涩情| 国产精品一页 | 欧美第二页 | 日韩成人av网 | 日韩va视频| 国产欧美日韩精品在线观看 | 国产精品1234区 | 超碰成人免费在线 | 伊人影视网 | 日本精品久久久久中文字幕 | 国产精品麻豆一区二区 | 国产一二三在线视频 | 国产午夜精品一区二区三区 | 中文字幕人妻一区 | 成人一级在线 | a在线观看免费 | 亚洲乱亚洲 | 日韩视频免费观看高清完整版 | 国产在线极品 | 精品乱码久久久久久中文字幕 | 成为性瘾网黄的yy对象后 | 久久婷婷色综合 | 男人天堂网在线观看 | 欧美成人免费网站 | 成年人午夜视频 | 午夜啪啪网站 | 亚洲黄色免费在线观看 | 72pao成人国产永久免费视频 | 葵司av电影 | 在线国产毛片 | 九色影院| 久久六 | 青娱乐毛片| 久久久久久久久免费视频 | 91美女啪啪| 四季av一区二区凹凸精品 | av先锋影音 | 黄色片视频免费看 | 国产激情一区二区三区 | 国久久久 | 五月天精品 |