qt开发环境 - 丑陋的串口助手

qt版本:5.9.1

win版本:10.1706

本助手改自qt自带exmple中的terminal,去掉console相关内容,加入button textbowser textedit,只改变了mainwindow

下面是代码

/****************************************************************************
**
** 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$
**
****************************************************************************/#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QtCore/QtGlobal>#include <QMainWindow>#include <QtSerialPort/QSerialPort>QT_BEGIN_NAMESPACEclass QLabel;namespace Ui {
class MainWindow;
}QT_END_NAMESPACEclass Console;
class SettingsDialog;class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void openSerialPort();void closeSerialPort();void about();void writeData(const QByteArray &data);void readData();void handleError(QSerialPort::SerialPortError error);void on_pushButton_clicked();void clearTextBrowser();
private:void initActionsConnections();private:void showStatusMessage(const QString &message);Ui::MainWindow *ui;QLabel *status;Console *console;SettingsDialog *settings;QSerialPort *serial;
};#endif // MAINWINDOW_H

/****************************************************************************
**
** 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>//! [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);}}
//! [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);
}



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/243606.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OOo-MySpell 一个C++的拼写语法检查开源项目

地址: http://code.google.com/a/apache-extras.org/p/ooo-myspell/ 这个软件被应用到了 Pspell, OpenOffice, AbiWord, 等等.

与狼共舞·美女与野兽

收了一张法国女钢琴家埃莱娜格瑞莫的影碟&#xff0c;名为《埃莱娜格瑞莫&#xff1a;与狼共舞》&#xff08;Helene Grimaud:LIVING WITH WOLVES&#xff09;。前年曾读过她的一个自传《野变奏》&#xff08;上海教育出版社出版&#xff09;&#xff0c;去年还买过她一本传记《…

HDMI显示器驱动设计与验证

HDMI显示器驱动设计与验证 HDMI组成 HMDI数据传输图 TMDS信号连接图 HDMI彩条显示实验整体框图

“不要尝试欺骗我一个吻”(奥·安达尔摄影展)

少作 NO.33 &#xff08;眼睛&#xff09;, 2009 昨天&#xff0c;在伊比利亚艺术中心看挪威艺术家奥利约翰安达尔&#xff08;Ole John Aandal&#xff09;的题为“少作 ——构建资本主义社会的灵魂”的摄影展览。这些图片大多影象模糊&#xff0c;有的还有马赛克&#xff0…

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

qt版本&#xff1a;5.9.1 win版本&#xff1a;10.1706 下载&#xff1a;https://download.csdn.net/download/zn2857/10194028 改自上篇文章的串口助手&#xff0c;加入linEdit显示文件路径&#xff0c;加入新pushButton加载文件&#xff0c; 文件加载后显示在串口发送窗口…

SystemParametersInfo (SPI_GETNONCLIENTMETRICS... 在VC 2008 里不能正常工作

在codeproject上面看见一个颜色选择控件CColourPopup, 地址是 http://www.codeproject.com/Articles/713/A-color-picker-button 这里控件看到很多人再用, 但是在这个函数里面有点小问题 void CColourPopup::Initialise() {//other code// Create the fontNONCLIENTMETRICS n…

TFT_LCD液晶屏驱动设计与验证

TFT_LCD液晶屏驱动设计与验证 注:在本实验工程中,输出信号中包含 HV 同步模式下需要的行、场同步信号(hsync、 vsync)和 DE 同步模式下的 tft_de 信号,各信号正确输出。读者若想要使用 HV 同步模式进行图像显示,可在代码中注释掉 tft_de 信号;若想要使用 DE 同步模式进行图…

成吉思汗的子孙:中国游牧蒙古人

6月28日下午。大河画廊。内蒙古摄影家阿音题为“成吉思汗的子孙&#xff1a;中国游牧蒙古人”的个展。阿音&#xff0c;1970年出生于内蒙古。现居住在锡林郭勒盟的东乌珠穆沁旗。1989自学摄影。曾在国内外举办过多次摄影展&#xff0c;获得过2007年美国国家地理杂志世界纪实摄影…

保护Eclipse RCP应用的商业Java编译器

近日Excelsior LLC发布了最新版的Excelsior JET&#xff08;此前InfoQ对其做过介绍&#xff09;——Excelsior JET 6.5。该版本增加的一个主要特性就是处理Eclipse RCP应用的能力。\u0026#xD;\n凭借Excelsior JET&#xff0c;开发者可以将Eclipse RCP应用预编译为本地代码并分发…

ucos ii 文件分析

1. 平台介绍 本文档为基于uCOSII操作系统做开发的人员描述了一些uCOSII操作系统的基本特征。 1.1 结构图 图 1 整体结构图 2. 基本特征 2.1 uCOSII基本特征 uCOSII操作系统目前支持如下的基本特征&#xff1a; 1. 提供抢占式任务调度服务 2. 提供任务间同步与通信服务…

PJ Naughter's Freeware Library

http://www.codeproject.com/Articles/519/PJ-Naughter-s-Freeware-Library

FIFO求和实验

FIFO求和实验 实验目标:使用 Matlab 生成一个*.txt 文件,文件中包含模拟求和的数据, PC 机通过串口 RS232 将数据传给 FPGA,使用双 fifo 实现三行数据的 FIFO 求和,通过串口 RS232将求和后的数据回传给 PC 机,并通过串口助手打印出求和数据。 实验要求: *.txt 文件包含…

《老北大讲义》及其背后的故事

原北大校长胡适 不久前读到陈平原先生的一篇文章《早期北大文学史讲义三种》&#xff0c;该文对上世纪初北京大学的文科教学尤其是讲义的风行&#xff0c;做了非常细致的披露和分析。他说&#xff1a;&#xff08;老师授课&#xff09;“很少采纳通用教材&#xff0c;而喜欢临时…

日语过级 JLPT简介

JLPT(Japanese Language Proficiency Test),“日本语能力测试”是为了适应世界各国学习日语人数日渐增加的趋势&#xff0c;日本国际交流基金及其财团法人日本国际教育协会于1984年建立了一套较为完整的考试评价体系--日本语能力测试&#xff0c;并于同年开始在有关国家和地区实…

jtag相关

http://www.cnblogs.com/TaigaCon/archive/2012/12/20/2826941.html JTAG(Joint Test Action Group)联合测试行动小组)是一种国际标准测试协议&#xff08;IEEE 1149.1兼容&#xff09;&#xff0c;主要用于芯片内部测试。现在多数的高级器件都支持JTAG协议&#xff0c;如DSP、…

为中国游牧蒙古人造像

第一次见到阿音&#xff0c;近距离地观看他的摄影&#xff0c;让我有一种愧疚感。作为蒙古人&#xff0c;在北京生活了30多年&#xff0c;我与自己民族的根和那片正在消逝的草原故地越来越疏远了。曾有人问我对故乡的理解&#xff0c;我说&#xff1a;我的故乡不过是记忆&#…

基于sobel算法的边缘检测设计与实现

基于sobel算法的边缘检测设计与实现 边缘是图像的基本特征。边缘检测针对的是灰度图像,目的是标识数字图像中灰度变化明显的点。 边缘检测的方法大致可以分为两类:基于查找的一类,通过寻找图像一阶导数中最大值和最小值来检测边界。 基于零穿越的一类,通过寻找图像二阶导数…

基于SPI协议的Flash全擦除

基于SPI协议的Flash全擦除 `timescale 1ns / 1ps module flash_be_ctrl(input wire sys_clk,//系统时钟频率50MHZ input wire sys_rst_n,//复位信号,低电平有效 input wire key,//按键输入信号 output reg cs_n,//片选信号 output reg sck,//串行时钟 output reg mosi //主输出…

单片机运行相关

1. 类似51&#xff0c;AVR这类的单片机&#xff0c;程序只能在ROM或FLASH里运行。STC的芯片一般是标准51或增强51&#xff0c;用的是FLASH&#xff0c;程序只能在FLASH中运行。2. ARM的程序即可以在FLASH里运行也可以在RAM里运行&#xff0c;不过能运行程序的FLASH只能使NorFla…

《心欢喜,灵快乐》出版

深圳女作家、画家崔文僮的散文绘画集出版。该书是她观察生活、体验人生、感悟艺术的真实记录。著名女作家徐小斌看了她的文章和绘画&#xff0c;写道&#xff1a;“‘生活家’这个概念令人非常感兴趣。文僮决定做一个‘有追求’的‘生活家’&#xff0c;这本身就十分有趣&#…