QSerialPort-Qt串口通信

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

1. 介绍

Qt对串口通信提供了专用类QSerialPort,须要在pro文件增长:QT += serialport,其继承自QIODevicehtml

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

2. QSerialPortInfo

QSerialPortInfo::availablePorts();函数

能够获取当前设备的全部串口信息,提供了如下操做函数,可得到对应的信息类型。ui

  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为编号,此号通常不相同可用于串口设备识别。spa

3. QSerialPort

参考类帮助文档htm

相关串口配置函数:继承

  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可开启串口ip

阻塞数据读写:文档

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

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

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

读取内容。

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

相关文章
相关标签/搜索