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

来源:互联网 发布:apache php -v phpinfo 编辑:程序博客网 时间:2024/05/16 06:00

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

一、QtSerialPort简介

1、串口通信基础

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

    串口通信的主要参数如下:

    A、波特率:衡量通信速度的参数,表示每秒钟传送的bit的个数。例如9600波特表示每秒钟发送9600个bit。

    B、数据位:衡量通信中实际数据位的参数,当计算机发送一个信息包,实际包含的有效数据位个数。

    C、停止位:用于表示单个包的最后一位。典型的值为1和2位。

    D、奇偶校验位:串口通信中一种检错方式。常用的检错方式有:偶、奇校验。

2QtSerialPort模块简介

    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

1QSerialPort简介

    QSerialPort提供了访问串口的接口函数。使用辅助类QSerialPortInfo可以获取可用的串口信息。将QSerialPortInfo辅助类对象做为参数,使用setPort()setPortName()函数可以设置要访问的串口设备。

    设置好端口后,可以使用open()函数以只读、只写或读写的模式打开使用。

    注意,串口使用独占方式打开。

    使用close()函数关闭串口并且取消IO操作。

    串口成功打开后,QSerialPort会尝试确定串口的当前配置并初始化。可以使用setBaudRate()setDataBits()setParity()setStopBits()setFlowControl()函数重新配置端口设置。

    有一对名为QSerialPort::dataTerminalReadyQSerialPort::requestToSend的属性

    QSerialPort提供了中止正在调用线程直到信号触发的一系列函数。这些函数用于阻塞串口。

    waitForReadyRead():阻塞调用,直到有新的数据可读

    waitForBytesWritten():阻塞调用,直到数据以及写入串口

阻塞串口编程与非阻塞串口编程完全不同。阻塞串口不会要求时间循环并且通常会简化代码。然而,在GUI程序中,为了避免冻结用户界面,阻塞串口编程只能用于非GUI线程。

    QSerialPort也能使用QTextStreamQDataStream的流操作符。在试图使用流操作符>>读时,需要确保有足够可用的数据。

2QSerialPort成员函数

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

1QSerialPortInfo简介

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

2、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位的制造商编码

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() << "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 0