Qt5串口通讯简介

一、QtSerialPort简介

(一)串口通信基础

目前使用最广泛的串口为DB9接口,适用于较近距离的通信。一般小于10米。DB9接口有9个针脚。
串口通信的主要参数如下:

  1. 波特率:衡量通信速度的参数,表示每秒钟传送的bit的个数。例如9600波特表示每秒钟发送9600个bit。
  2. 数据位:衡量通信中实际数据位的参数,当计算机发送一个信息包,实际包含的有效数据位个数。
  3. 停止位:用于表示单个包的最后一位。典型的值为1和2位。
  4. 奇偶校验位:串口通信中一种检错方式。常用的检错方式有:偶、奇校验。

(二)QtSerialPort模块简介

QtSerialPort模块是QT5中附加模块的一个模块,为硬件和虚拟的串口提供统一的接口。
串口由于其简单和可靠,目前在像嵌入式系统、机器人等工业中依旧用得很多。使用QtSerialPort模块,开发者可以大大缩短开发串口相关的应用程的周期。
Qt SerialPort提供了基本的功能,包括配置、I/O操作、获取和设置RS-232引脚的信号。
Qt SerialPort模块暂不支持以下特性:

  1. 终端的特性,例如回显,控制CR/LF等等
  2. 文本模式
  3. 读或写操作的超时和延时配置
  4. 当RS-232引脚信号变化通知

要在应用程序中使用QtSerialPort,需要包括如下的声明:

#include <QtSerialPort/QtSerialPort>

要链接QtSerialPort模块,需要在.pro文件中添加如下内容:

QT += serialport

二、QSerialPort

(一)QSerialPort简介

QSerialPort提供了访问串口的接口函数。使用辅助类QSerialPortInfo可以获取可用的串口信息。将QSerialPortInfo辅助类对象做为参数,使用setPort()setPortName()函数可以设置要访问的串口设备。
设置好端口后,可以使用open()函数以只读、只写或读写的模式打开使用。
注意,串口使用独占方式打开。
使用close()函数关闭串口并且取消IO操作。
串口成功打开后,QSerialPort会尝试确定串口的当前配置并初始化。可以使用setBaudRate()setDataBits()setParity()setStopBits()setFlowControl()函数重新配置端口设置。
有一对名为QSerialPort::dataTerminalReadyQSerialPort::requestToSend的属性
QSerialPort提供了中止正在调用线程直到信号触发的一系列函数。这些函数用于阻塞串口。
waitForReadyRead():阻塞调用,直到有新的数据可读
waitForBytesWritten():阻塞调用,直到数据以及写入串口
阻塞串口编程与非阻塞串口编程完全不同。阻塞串口不会要求时间循环并且通常会简化代码。然而,在GUI程序中,为了避免冻结用户界面,阻塞串口编程只能用于非GUI线程。
QSerialPort也能使用QTextStreamQDataStream的流操作符。在试图使用流操作符>>读时,需要确保有足够可用的数据。

(二)QSerialPort成员函数

1、构造函数

QSerialPort::QSerialPort(QObject *parent = Q_NULLPTR)
QSerialPort::QSerialPort(const QString &name, QObject *parent = Q_NULLPTR)
QSerialPort::QSerialPort(const QSerialPortInfo &serialPortInfo, QObject *parent = Q_NULLPTR)

2、如果当前没有数据可读,返回true

[virtual] bool QSerialPort::atEnd() const

3、波特率改变后,信号触发

[signal] void QSerialPort::baudRateChanged(qint32 baudRate, QSerialPort::Directions directions)

4、返回可读数据的字节数

[virtual] qint64 QSerialPort::bytesAvailable() const

5、返回可写数据的字节数

[virtual] qint64 QSerialPort::bytesToWrite() const

6、关闭串口

[virtual] void QSerialPort::close()

7、设置串口端口信息为serialPortInfo

void QSerialPort::setPort(const QSerialPortInfo &serialPortInfo)

8、设置串口名为name

void QSerialPort::setPortName(const QString &name)

三、QSerialPortInfo

(一)QSerialPortInfo简介

QSerialPortInfo类提供已有串口设备的信息。使用QSerialPortInfo类的静态成员函数生成QSerialPortInfo对象的链表。链表中的每个QSerialPortInfo对象代表一个串口,每个串口可以使用端口名、系统定位、描述、制造商查询。QSerialPortInfo类对象也可以用做QSerialPort类的setPort()成员函数的参数。

(二)QSerialPortInfo成员函数

1、构造函数

QSerialPortInfo::QSerialPortInfo(const QSerialPort &port)
QSerialPortInfo::QSerialPortInfo(const QString &name)
QSerialPortInfo::QSerialPortInfo(const QSerialPortInfo &other)

2、返回当前系统可用串口的链表

[static] QList<QSerialPortInfo> QSerialPortInfo::availablePorts()

3、如果串口可用,返回串口的描述信息

QString QSerialPortInfo::description() const

4、如果有一个合法的16位生产码,返回true

bool QSerialPortInfo::hasProductIdentifier() const

5、如果有一个合法的16位制造商编码,返回true

bool QSerialPortInfo::hasVendorIdentifier() const

6、如果串口当前正忙,返回true

bool QSerialPortInfo::isBusy() const

7、如果串口可用,返回串口的制造商的名字

QString QSerialPortInfo::manufacturer() const

8、返回串口的名字

QString QSerialPortInfo::portName() const

9、如果串口可用,返回串口的16位的生产编码

quint16 QSerialPortInfo::productIdentifier() const

10、如果串口可用,返回串口的序列号

QString QSerialPortInfo::serialNumber() const

11、返回目标平台支持的可用的标准波特率的链表

[static] QList<qint32> QSerialPortInfo::standardBaudRates()

12、使用other交换QSerialPortInfo对象

void QSerialPortInfo::swap(QSerialPortInfo &other)

13、返回串口的系统位置

QString QSerialPortInfo::systemLocation() const

14、如果串口可用,返回16位的制造商编码

quint16 QSerialPortInfo::vendorIdentifier() const

(三)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() << "Port: " << 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();
}

 

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值