美文网首页
Qt5学习:串口编程基础

Qt5学习:串口编程基础

作者: 虞锦雯 | 来源:发表于2018-08-29 08:53 被阅读231次

转载自QT开发(五十)——QT串口编程基础

一、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成员函数

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

构造函数

[virtual] bool QSerialPort::atEnd() const

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

[signal] void QSerialPort::baudRateChanged(qint32 baudRate, QSerialPort::Directions 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

(一)QSerialPortInfo简介

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

(二)QSerialPortInfo成员函数

QSerialPortInfo::QSerialPortInfo(const QSerialPort &port)
QSerialPortInfo::QSerialPortInfo(const QString &name)
QSerialPortInfo::QSerialPortInfo(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位的制造商编码

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

相关文章

  • Qt5学习:串口编程基础

    转载自QT开发(五十)——QT串口编程基础 一、QtSerialPort简介 (一)串口通信基础 目前使用最广泛的...

  • Qt串口通信

    1. Qt串口通信类QSerialPort 在Qt5的的更新中,新增了串口通信的相关接口类QSerialPort,...

  • Qt学习笔记(一)

    画图 qcustomplot 串口通信 Qt5以后自带QSerialPort 1 在工程文件(即.pro文件)中增...

  • 简述:在QT5中使用串口类

    在QT5中,我们友好的编译器亲切的为我们提供了串口类,让我们不再为简单的使用串口而发愁,这里就简单介绍一下我对串口...

  • 2019-07-11

    串口及串口编程 1、串口编程界面 (2)功能效果实现 2、使用串口调试工具实现数据传输 (1)在下拉列表中列出本机...

  • Qt5 串口读写

    还是直接上干货!! 1. 遍历所有可用串口 2. 初始化串口参数 3. 发送数据 4. 接受数据 4.1 一次性读...

  • QT串口编程 - QSerialPort类

    QT串口编程 - QSerialPort类 提供对串口的访问 头文件:#include qmake: QT += ...

  • QT-参考书

    《Qt Creator快速入门》《Qt5编程入门》 Qt进阶书籍推荐官方的《C++ GUI Qt4编程》

  • Qt5 串口数据读取

    由于RS232串口操作简单、通讯可靠,所以在工业领域中有大量的应用。而普通家用PC已经逐步淘汰该串口,但usb转串...

  • 串口编程

    串口连接界面 主要控件 打开串口 自动获取串口列表并显示 初始化串口 写入卡片 插入打卡记录并显示打卡成功

网友评论

      本文标题:Qt5学习:串口编程基础

      本文链接:https://www.haomeiwen.com/subject/jixhwftx.html