当前位置:网站首页>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;
}
边栏推荐
- 2022.02.15 - 240. Lucky number in matrix
- Fault: KDC warning log for id29
- MySQL learning notes
- Error: GPG check FAILED Once install MySQL
- json tobean
- 数据库-同义词
- Go basic data types: characters and strings
- QT (x): innosetup for software packaging
- 力扣今日题-324. 摆动排序 II
- How to combine two byte arrays [repeat] - how to combine two byte arrays [duplicate]
猜你喜欢

Hyperledger Fabric 2. X custom smart contract

MySQL learning notes

Teach you how to develop your own NPM package (publish to the NPM official website)

json tobean

Observer mode vs publish subscribe mode

层次分析法

Small program large screen adaptation Guide

Games101 Lecture 10 geometry 1 Notes

Are there too many programmers in China at present?

jetson tx2
随机推荐
How does schedulerx help users solve distributed task scheduling problems?
关于 localStorage 的一些高阶用法
Creating a new generation of production and service tools with robot education
2022.02.15 - SX10-31. House raiding III
VerilogA——动态比较器
更改主机名的方法(永久)
力扣每日一题-第30天-1281.整数的各位积和之差
JDBC | Chapter 6: simple use of database connection pool
分享 10 个 JS Promise 相关的面试题
Unity ar shadow shadow
Case of single file component files
Some thoughts on port forwarding program
About: deleting unwanted event log lists
力扣每日一题-第30天-1523.位1的个数
Part 63 - interpreter and compiler adaptation (II)
Output of character pointer to string in C language
Vite快速上手
Haar cascades and LBP cascades in face detection [closed] - Haar cascades vs. LBP cascades in face detection [closed]
Li Kou daily question - day 30 -1281 Difference of sum of bit product of integer
Activiti Designer