Qt-上位机-串口助手
前言:参考自:https://blog.csdn.net/u014695839/article/details/50611549
一、新建Widgets Appliaction工程
二、设计ui界面

三、修改文件
3.1 修改mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSerialPort>
#include <QSerialPortInfo>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
);
~MainWindow();
private slots:
void serialPort_readyRead();
void on_searchButton_clicked();
void on_openButton_clicked();
void on_sendButton_clicked();
void on_clearButton_clicked();
private:
Ui::MainWindow *ui;
QSerialPort serial;
};
#endif
3.2 修改mainwindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//连接信号和槽
QObject::connect(&serial, &QSerialPort::readyRead, this, &MainWindow::serialPort_readyRead);
//发送按键失能
ui->sendButton->setEnabled(false);
//波特率默认选择下拉第三项:9600
ui->baudrateBox->setCurrentIndex();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::serialPort_readyRead()
{
//从接收缓冲区中读取数据
QByteArray buffer = serial.readAll();
//从界面中读取以前收到的数据
QString recv = ui->recvTextEdit->toPlainText();
recv += QString(buffer);
//清空以前的显示
ui->recvTextEdit->clear();
//重新显示
ui->recvTextEdit->append(recv);
}
void MainWindow::on_searchButton_clicked()
{
ui->portNameBox->clear();
//通过QSerialPortInfo查找可用串口
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->portNameBox->addItem(info.portName());
}
}
void MainWindow::on_openButton_clicked()
{
if(ui->openButton->text()==QString("打开串口"))
{
//设置串口名
serial.setPortName(ui->portNameBox->currentText());
//设置波特率
serial.setBaudRate(ui->baudrateBox->currentText().toInt());
//设置数据位数
switch(ui->dataBitsBox->currentIndex())
{
: serial.setDataBits(QSerialPort::Data8); break;
default: break;
}
//设置奇偶校验
switch(ui->ParityBox->currentIndex())
{
: serial.setParity(QSerialPort::NoParity); break;
default: break;
}
//设置停止位
switch(ui->stopBitsBox->currentIndex())
{
: serial.setStopBits(QSerialPort::OneStop); break;
: serial.setStopBits(QSerialPort::TwoStop); break;
default: break;
}
//设置流控制
serial.setFlowControl(QSerialPort::NoFlowControl);
//打开串口
if(!serial.open(QIODevice::ReadWrite))
{
QMessageBox::about(NULL, "提示", "无法打开串口!");
return;
}
//下拉菜单控件失能
ui->portNameBox->setEnabled(false);
ui->baudrateBox->setEnabled(false);
ui->dataBitsBox->setEnabled(false);
ui->ParityBox->setEnabled(false);
ui->stopBitsBox->setEnabled(false);
ui->openButton->setText(QString("关闭串口"));
//发送按键使能
ui->sendButton->setEnabled(true);
}
else
{
//关闭串口
serial.close();
//下拉菜单控件使能
ui->portNameBox->setEnabled(true);
ui->baudrateBox->setEnabled(true);
ui->dataBitsBox->setEnabled(true);
ui->ParityBox->setEnabled(true);
ui->stopBitsBox->setEnabled(true);
ui->openButton->setText(QString("打开串口"));
//发送按键失能
ui->sendButton->setEnabled(false);
}
}
void MainWindow::on_sendButton_clicked()
{
//获取界面上的数据并转换成utf8格式的字节流
QByteArray data = ui->sendTextEdit->toPlainText().toUtf8();
serial.write(data);
}
void MainWindow::on_clearButton_clicked()
{
ui->recvTextEdit->clear();
}
三、运行
-END-
Qt-上位机-串口助手的更多相关文章
- 基于uFUN开发板的心率计(三)Qt上位机的实现
前言 上两周利用周末的时间,分别写了基于uFUN开发板的心率计(一)DMA方式获取传感器数据和基于uFUN开发板的心率计(二)动态阈值算法获取心率值,介绍了AD采集传感器数据和数据的滤波处理获取心率值 ...
- C#上位机串口控制12864显示
实现的效果 上面是用Proteus仿真的,,对了如果自己想用proteus仿真需要安装下面这个软件 再看一下实物显示效果 先做上位机部分........... 为了程序一启动就把电脑上能用的串口号显示 ...
- 14-ESP8266 SDK开发基础入门篇--上位机串口控制 Wi-Fi输出PWM的占空比,调节LED亮度,8266程序编写
https://www.cnblogs.com/yangfengwu/p/11102026.html 首先规定下协议 ,CRC16就不加了哈,最后我会附上CRC16的计算程序,大家有兴趣自己加上 上 ...
- ROS常用库(二) Serial库(单片机和上位机串口通讯)
比如我们做了个单片机,在win里面用串口调试助手接收和下发数据,那么在ubuntu里用ros怎么实现?换个说法,怎么实现上位机和下位机的通讯? 首先,用python自带的库就可以实现这个功能. 安装p ...
- 13-ESP8266 SDK开发基础入门篇--上位机串口控制 Wi-Fi输出PWM的占空比,IEEE754规约
https://www.cnblogs.com/yangfengwu/p/11100552.html 这节做个上位机控制Wi-Fi引脚输出的PWM占空比信号,灯的亮度就可以用上位机控制了 大家可以自己 ...
- VS2008基于对话框的MFC上位机串口通信(C++实现)简单例程
首先,在 vs2008 环境下创建 MFC 运用程序 设置项目名称为 ComTest(这个地方随意命名,根据个人习惯),点击确定后,点击下一步 出现如下界面 选择"基于对话框"模式 ...
- QT上位机
程序是用QT写的,通过COM口进行数据的读取. 源码地址:https://github.com/kunkunlin/QT-SWJ
- 15-ESP8266 SDK开发基础入门篇--上位机串口控制 Wi-Fi输出PWM的占空比,调节LED亮度,上位机程序编写
https://www.cnblogs.com/yangfengwu/p/11104167.html 先说一下整体思路哈.. 咱滑动的时候 会进入这个,然后咱呢不直接从这个里面写发送 因为这样的话太快 ...
- 嵌入式Linux学习笔记(六) 上位机QT界面实现和串口通讯实现
目录 (1).参考资料 (2).QT界面布局实现 (3).数据和操作逻辑 在上一章我们实现了下位机的协议制定,并通过串口通讯工具完成了对设备内外设(LED)的状态修改,下面就要进行上位机软件的实现了( ...
随机推荐
- javaweb 之 代理模式
一.动态代理 1.1.代理模式 什么是代理模式及其作用 Proxy Pattern(即:代理模式),23种常用的面向对象软件的设计模式之一 代理模式的定义:为其他对象提供一种代理以控制对这个对象的访问 ...
- [翻译]内存一致性模型 --- memory consistency model
I will just give the analogy with which I understand memory consistency models (or memory models, fo ...
- undefined reference to “boost” in Qt—Ubuntu
一:使用PCL时遇到的问题 原因:缺少boost的lib包含: 在Pro文件里面添加: LIBS += -lboost_system .....................等库文件包含 二:编译B ...
- html form表单追加input元素后在提交
form.append(input); //input为对象 (设置name和val有效) $("#form1").submit();//提交事件
- CentOS7环境RabbitMQ集群配置管理(转载)
CentOS7环境RabbitMQ集群配置管理(转载) CentOS7系统内核版本:3.10.0-514.26.2.el7.x86_64 一.对应主机host地址(三台主机host文件要保持一致) ...
- 日常记录-Pandas Cookbook
Cookbook 1.更新内容 2.关于安装 3.Pandas使用注意事项 4.包环境 5.10分钟Pandas初识 6.教程 7.Cookbook 8.数据结构简介 9.基本功能 10.使用文本数据 ...
- Python笔记12-----画图Matplotlib
1.matplotlib:pyplot和pylab 如: import pylab as pl pl.figure(figsize=(8,6),dpi=100)[建立的图像大小和图的精度] pl.pl ...
- 封装自己的jquery框架
jQuery is a fast small JavaScript library 如何封装自己的jQuery <script> // 这里使用沙箱模式,可以防止全局污染 (functio ...
- Project Euler 11 Largest product in a grid
题意:在这个20×20方阵中,四个在同一方向(从下至上.从上至下.从右至左.从左至右或者对角线)上相邻的数的乘积最大是多少? 思路:暴力去枚举以 ( x , y ) 为中心拓展的四个方向 /***** ...
- vue 如何清除定时器
在页面中需要定时刷新局部数据,在数据变化是否频繁的情况下,没有必要使用webSocket,因为数据变化频繁,数据实时变化太快看不清楚.因此页面会定时调用后台接口以达到实时刷新数据的效果. 1.在dat ...