找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2764|回复: 12

QT串口编程基础

 火.. [复制链接]
  • 打卡等级:即来则安
  • 打卡总天数:29
  • 打卡月天数:1
  • 打卡总奖励:7791
  • 最近打卡:2025-12-13 17:25:16

2540

主题

1355

回帖

2万

积分

管理员

积分
21304
发表于 2021-3-4 12:48:59 | 显示全部楼层 |阅读模式
一、QtSerialPort简介
1、串口通信基础
目前使用最广泛的串口为DB9接口,适用于较近距离的通信。一般小于10米。DB9接口有9个针脚。
串口通信的主要参数如下:
A、波特率:衡量通信速度的参数,表示每秒钟传送的bit的个数。例如9600波特表示每秒钟发送9600个bit。
B、数据位:衡量通信中实际数据位的参数,当计算机发送一个信息包,实际包含的有效数据位个数。
C、停止位:用于表示单个包的最后一位。典型的值为1和2位。
D、奇偶校验位:串口通信中一种检错方式。常用的检错方式有:偶、奇校验。
2、QtSerialPort模块简介
QtSerialPort模块是QT5中附加模块的一个模块,为硬件和虚拟的串口提供统一的接口。
串口由于其简单和可靠,目前在像嵌入式系统、机器人等工业中依旧用得很多。使用QtSerialPort模块,开发者可以大大缩短开发串口相关的应用程的周期。
Qt SerialPort提供了基本的功能,包括配置、I/O操作、获取和设置RS-232引脚的信号。
Qt SerialPort模块暂不支持以下特性:
A、终端的特性,例如回显,控制CR/LF等等
B、文本模式
C、读或写操作的超时和延时配置
D、当RS-232引脚信号变化通知
要在应用程序中使用QtSerialPort,需要包括如下的声明:
#include <QtSerialPort/QtSerialPort>
要链接QtSerialPort模块,需要在.pro文件中添加如下内容:
QT += serialport
二、QSerialPort
1、QSerialPort简介
QSerialPort提供了访问串口的接口函数。使用辅助类QSerialPortInfo可以获取可用的串口信息。将QSerialPortInfo辅助类对象做为参数,使用setPort()或setPortName()函数可以设置要访问的串口设备。
设置好端口后,可以使用open()函数以只读、只写或读写的模式打开使用。
注意,串口使用独占方式打开。
使用close()函数关闭串口并且取消IO操作。
串口成功打开后,QSerialPort会尝试确定串口的当前配置并初始化。可以使用setBaudRate()、setDataBits()、setParity()、setStopBits()和setFlowControl()函数重新配置端口设置。
有一对名为QSerialPort::dataTerminalReady、QSerialPort::requestToSend的属性
QSerialPort提供了中止正在调用线程直到信号触发的一系列函数。这些函数用于阻塞串口。
waitForReadyRead():阻塞调用,直到有新的数据可读
waitForBytesWritten():阻塞调用,直到数据以及写入串口
阻塞串口编程与非阻塞串口编程完全不同。阻塞串口不会要求时间循环并且通常会简化代码。然而,在GUI程序中,为了避免冻结用户界面,阻塞串口编程只能用于非GUI线程。
QSerialPort也能使用QTextStream和QDataStream的流操作符。在试图使用流操作符>>读时,需要确保有足够可用的数据。
2、QSerialPort成员函数
QSerialPort:SerialPort(QObject *parent = Q_NULLPTR)
QSerialPort:SerialPort(const QString &name, QObject *parent = Q_NULLPTR)
QSerialPort:SerialPort(const QSerialPortInfo &serialPortInfo, QObject *parent = Q_NULLPTR)
构造函数
[virtual] bool QSerialPort::atEnd() const
如果当前没有数据可读,返回true
[signal] void QSerialPort::baudRateChanged(qint32 baudRate, QSerialPort:irections directions)
波特率改变后,信号触发
[virtual] qint64 QSerialPort::bytesAvailable() const
返回可读数据的字节数
[virtual] qint64 QSerialPort::bytesToWrite() const
返回可写数据的字节数
[virtual] void QSerialPort::close()
关闭串口
void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo)
设置串口端口信息为serialPortInfo
void QSerialPort::setPortName(const QString &name)
设置串口名为name
三、QSerialPortInfo
1、QSerialPortInfo简介
QSerialPortInfo类提供已有串口设备的信息。使用QSerialPortInfo类的静态成员函数生成QSerialPortInfo对象的链表。链表中的每个QSerialPortInfo对象代表一个串口,每个串口可以使用端口名、系统定位、描述、制造商查询。QSerialPortInfo类对象也可以用做QSerialPort类的setPort()成员函数的参数。
2、QSerialPortInfo成员函数
QSerialPortInfo:SerialPortInfo(const QSerialPort &port)
QSerialPortInfo:SerialPortInfo(const QString &name)
QSerialPortInfo:SerialPortInfo(const QSerialPortInfo &other)
构造函数
[static] QList<QSerialPortInfo> QSerialPortInfo::availablePorts()
返回当前系统可用串口的链表
QString QSerialPortInfo::description() const
如果串口可用,返回串口的描述信息
bool QSerialPortInfo::hasProductIdentifier() const
如果有一个合法的16位生产码,返回true
bool QSerialPortInfo::hasVendorIdentifier() const
如果有一个合法的16位制造商编码,返回true
bool QSerialPortInfo::isBusy() const
如果串口当前正忙,返回true
QString QSerialPortInfo::manufacturer() const
如果串口可用,返回串口的制造商的名字
QString QSerialPortInfo::portName() const
返回串口的名字
quint16 QSerialPortInfo::productIdentifier() const
如果串口可用,返回串口的16位的生产编码
QString QSerialPortInfo::serialNumber() const
如果串口可用,返回串口的序列号
[static] QList<qint32> QSerialPortInfo::standardBaudRates()
返回目标平台支持的可用的标准波特率的链表
void QSerialPortInfo::swap(QSerialPortInfo &other)
使用other交换QSerialPortInfo对象
QString QSerialPortInfo::systemLocation() const
返回串口的系统位置
quint16 QSerialPortInfo::vendorIdentifier() const
如果串口可用,返回16位的制造商编码
3、QSerialPortInfo显示串口信息实例
#include <QCoreApplication>#include <QtSerialPort/QtSerialPort>#include <QList>#include <QDebug> int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QList<QSerialPortInfo> list = QSerialPortInfo::availablePorts(); qDebug() << "Total number of availiable ports:" << list.count(); foreach(const QSerialPortInfo &serialportinfo, list) { qDebug() << "ort: " << serialportinfo.portName(); qDebug() << "Location: " << serialportinfo.systemLocation(); qDebug() << "Description: " << serialportinfo.description(); qDebug() << "Manufactutor: " << serialportinfo.manufacturer(); qDebug() << "Vendor Indentifier: " << serialportinfo.vendorIdentifier(); qDebug() << "Busy: " << serialportinfo.isBusy(); } return a.exec();}
工控课堂 www.gkket.com

0

主题

86

回帖

291

积分

注册会员

积分
291
发表于 2021-3-4 15:11:09 | 显示全部楼层
楼主您的技术水准,我最服你,其他都是浮云
工控课堂 www.gkket.com

0

主题

118

回帖

276

积分

注册会员

积分
276
发表于 2021-3-4 20:47:07 | 显示全部楼层
绝对干货,楼主给力,支持了!!!
工控课堂 www.gkket.com

0

主题

160

回帖

339

积分

注册会员

积分
339
发表于 2025-11-17 05:37:33 | 显示全部楼层
同款经历!简直是世另我
工控课堂 www.gkket.com

0

主题

51

回帖

62

积分

新手上路

积分
62
QQ
发表于 2025-11-17 05:57:02 | 显示全部楼层
蹲一波同款,有没有姐妹 / 兄弟推荐?
工控课堂 www.gkket.com

0

主题

262

回帖

963

积分

中级会员

积分
963
发表于 2025-11-17 06:33:17 | 显示全部楼层
原来还有这种操作,长见识了!
工控课堂 www.gkket.com

0

主题

69

回帖

101

积分

新手上路

积分
101
发表于 2025-11-17 11:29:07 | 显示全部楼层
已转发给朋友,一起感受这份快乐~
工控课堂 www.gkket.com

0

主题

71

回帖

103

积分

新手上路

积分
103
发表于 2025-11-17 11:35:52 | 显示全部楼层
学到了学到了,这波分享太实用啦!
工控课堂 www.gkket.com

0

主题

97

回帖

149

积分

新手上路

积分
149
发表于 2025-11-17 11:36:31 | 显示全部楼层
笑不活了,评论区比正文还精彩
已转发给朋友,一起快乐一下
工控课堂 www.gkket.com

0

主题

108

回帖

167

积分

新手上路

积分
167
发表于 2025-11-17 11:36:47 | 显示全部楼层
蹲一波同款,有没有小伙伴推荐?
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-22 22:41 , Processed in 0.112180 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表