当前位置:网站首页>Qt 串口编程
Qt 串口编程
2022-06-29 06:37:00 【Mr.codeee】
1.使用的类
需要pro中添加模块 QT += serialport
QSerialPort
常用接口:
//设置串口号
void setPortName(const QString &name)
//设置波特率
bool setBaudRate(qint32 baudRate, Directions directions = AllDirections)
//设置数据位
bool setDataBits(DataBits dataBits)
//设置校验位
bool setParity(Parity parity)
//设置停止位
bool setStopBits(StopBits stopBits)
2.运行截图

3.源码
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include <QSerialPort>
#include <QSerialPortInfo>
namespace Ui {
class MainWidget;
}
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
private slots:
void on_btnOpen_clicked();
void slotReadRead();
private:
Ui::MainWidget *ui;
private:
QSerialPort *m_port = nullptr;
};
#endif // MAINWIDGET_H
#include "MainWidget.h"
#include "ui_MainWidget.h"
#include <QMessageBox>
#include <QDebug>
#include <QThread>
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->cbxSerialNum->addItem(info.portName());
}
QStringList baud;
baud.append("1800");
baud.append("2400");
baud.append("4800");
baud.append("9600");
baud.append("19200");
baud.append("38400");
baud.append("57600");
baud.append("115200");
baud.append("230400");
ui->cbxBuad->addItems(baud);
ui->cbxBuad->setCurrentText("115200");
m_port = new QSerialPort(this);
connect(m_port,&QSerialPort::readyRead,this,&MainWidget::slotReadRead);
}
MainWidget::~MainWidget()
{
delete ui;
}
void MainWidget::on_btnOpen_clicked()
{
if(m_port->isOpen())
{
m_port->close();
ui->btnOpen->setText("打开");
return;
}
m_port->setPortName(ui->cbxSerialNum->currentText());
m_port->setBaudRate(ui->cbxBuad->currentText().toInt());
m_port->setDataBits((QSerialPort::DataBits)ui->cbxDataBit->currentText().toInt());
if(ui->cbxJybit->currentIndex() == 0)
{
m_port->setParity(QSerialPort::NoParity);
}
else
{
m_port->setParity((QSerialPort::Parity)(ui->cbxJybit->currentIndex()+1));
}
m_port->setStopBits((QSerialPort::StopBits)ui->cbxStopBit->currentIndex());
if(!m_port->open(QIODevice::ReadWrite))
{
QMessageBox::critical(NULL,"tips","serial open failed");
}
qDebug()<<"serial open success";
ui->btnOpen->setText("关闭");
}
//当串口过来数据时,触发槽
void MainWidget::slotReadRead()
{
QByteArray resvBuffer;
resvBuffer = m_port->readAll();
qDebug()<<"strlen "<<resvBuffer.size();
qDebug()<<resvBuffer;
}
边栏推荐
- Single application and microservice application
- QT (x): packaging and deployment
- idea使用
- Subtotal of C language -- basic data types and their representations
- json tobean
- Overlay histogram with density curve
- Unity AR Shadow 阴影
- Service grid ASM year end summary: how do end users use the service grid?
- 作为一名合格的网工,你必须掌握的 DHCP Snooping 知识!
- 力扣每日一题-第30天-594.最长和谐子序列
猜你喜欢

The annual technology inventory of cloud primitives was released, and it was the right time to ride the wind and waves

json tobean

百度小程序自动提交搜索

mongostat性能分析

Clickhouse data type

Print Yanghui triangle

作为一名合格的网工,你必须掌握的 DHCP Snooping 知识!

Games101 Lecture 10 geometry 1 Notes

【OSPF引入直连路由时巧借静态黑洞路由做汇总】

2022.02.14 - 239. A single element in an ordered array
随机推荐
UVM验证平台
更改主机名的方法(永久)
Haar cascades and LBP cascades in face detection [closed] - Haar cascades vs. LBP cascades in face detection [closed]
Vite quick start
Fault: administrator account cannot be selected for login
Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- length and angle measurement function
VerilogA——动态比较器
Draw smooth curves - methods needed - drawing smooth curves - methods needed
Character pointer as function parameter
融入STEAM教育的劳动技能课程
How to change the password after forgetting the MySQL password (the latest version of 2022 detailed tutorial nanny level)
Li Kou daily question - day 30 -1523 Number of bit 1
[Flink] flinksql and table programming cases
2022.02.15 - 240. Lucky number in matrix
Open source 23 things shardingsphere and database mesh have to say
Browser local storage
Mongostat performance analysis
Multithreading tool class completabilefuture
flutter配置国内镜像,连接真机
Teach you how to develop your own NPM package (publish to the NPM official website)