zoukankan      html  css  js  c++  java
  • QSerialPortQt串口通讯

    博客地址已更改,文章数量较多不便批量修改,若想访问源文请到 coologic博客 查阅,网址:www.coologic.cn

    如本文记录地址为 techieliang.com/A/B/C/ 请改为 www.coologic.cn/A/B/C/ 即可查阅

    版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
    本文标题:QSerialPort-Qt串口通讯     本文地址:http://techieliang.com/2017/12/534/

    1. 介绍

    Qt对串口通讯提供了专用类QSerialPort,需要在pro文件增加:QT += serialport,其继承自QIODevice

    相关类还有QSerialPortInfo 提供当前设备串口信息

    2. QSerialPortInfo

    QSerialPortInfo::availablePorts();

    可以获取当前设备的所有串口信息,提供了以下操作函数,可获得对应的信息类型。

    1. QString description() const
    2. bool hasProductIdentifier() const
    3. bool hasVendorIdentifier() const
    4. bool isBusy() const
    5. bool isNull() const
    6. QString manufacturer() const
    7. QString portName() const
    8. quint16 productIdentifier() const
    9. QString serialNumber() const
    10. void swap(QSerialPortInfo &other)
    11. QString systemLocation() const
    12. quint16 vendorIdentifier() const

    portName一般为“COMX”;Description为描述信息;serialNumber为编号,此号一般不相同可用于串口设备识别。

    3. QSerialPort

    参考类帮助文档

    相关串口配置函数:

    1. bool sendBreak(int duration = 0)
    2. bool setBaudRate(qint32 baudRate, Directions directions = AllDirections)
    3. bool setBreakEnabled(bool set = true)
    4. bool setDataBits(DataBits dataBits)
    5. bool setDataTerminalReady(bool set)
    6. bool setFlowControl(FlowControl flowControl)
    7. bool setParity(Parity parity)
    8. void setPort(const QSerialPortInfo &serialPortInfo)
    9. void setPortName(const QString &name)
    10. void setReadBufferSize(qint64 size)
    11. bool setRequestToSend(bool set)
    12. bool setStopBits(StopBits stopBits)

    先配置完成后,调用open可开启串口

    阻塞数据读写:

    1. virtual bool open(OpenMode mode)
    2. virtual bool waitForBytesWritten(int msecs = 30000)
    3. virtual bool waitForReadyRead(int msecs = 30000)

    同时可以利用QIODevice类的readyRead信号,connect以后可在收到信息后在槽中响应,利用

    1. qint64 read(char *data, qint64 maxSize)
    2. QByteArray read(qint64 maxSize)
    3. QByteArray readAll()

    读取内容。

    串口可能在发送一串字符时每一个字符收到均有一次readyRead响应,此时需要自行判断终止符。

  • 相关阅读:
    博客迁移至wordpress--http://i1994898w1.imwork.net/wordpress
    超外差接收机的中频选择
    The Basics of the Doherty Amplifier-Bill Slade [转载]
    闻灾情 忆国殇 山河呜咽 寄哀思
    Simplest Doppler Radar System
    开发人员的奋斗目标
    敏捷开发的推理
    在创业型软件公司的收获
    人才市场的IT职位分析
    MySQL 基础及性能优化工具
  • 原文地址:https://www.cnblogs.com/techiel/p/7989903.html
Copyright © 2011-2022 走看看