• Qt 串口 封装好的类 直接使用


    QT+= serialport

    serialPort_.h

      1 #ifndef SERIALPORT__H
      2 #define SERIALPORT__H
      3 
      4 #include <QObject>
      5 #include <QSerialPort>
      6 #include <QSerialPortInfo>
      7 
      8 
      9 
     10 class serialPort_:public QObject
     11 {
     12     Q_OBJECT
     13 
     14 public:
     15     serialPort_(QObject*o=0);
     16 
     17     /**
     18 * @projectName   testMyClass
     19 * @brief         打开串口 根据名字 波特率 数据位 校验位 停止位 流控制
     20 * 返回打开成功还是失败
     21 * @author        SMY
     22 * @date          2019-03-21
     23 */
     24 
     25     bool openPort(QString portName,qint32 baudRate,QSerialPort::DataBits dataBits,
     26                   QSerialPort::Parity parity,QSerialPort::StopBits stopBits,
     27                   QSerialPort::FlowControl flowControl);
     28 
     29     /**
     30 * @projectName   testMyClass
     31 * @brief         是否打开串口
     32 * @author        SMY
     33 * @date          2019-03-21
     34 */
     35     bool isOpen()const{
     36         return m_port->isOpen();
     37     }
     38 
     39     /**
     40 * @projectName   testMyClass
     41 * @brief         关闭串口
     42 * @author        SMY
     43 * @date          2019-03-21
     44 */
     45     void closePort(){
     46         m_port->clear();
     47         m_port->close();
     48     }
     49     /**
     50 * @projectName   testMyClass
     51 * @brief         获取可用的串口名字
     52 * @author        SMY
     53 * @date          2019-03-21
     54 */
     55     QStringList getAvailablePortNameList()const
     56     {
     57         QStringList portName;
     58         foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
     59             portName<<info.portName();
     60 
     61         }
     62         return portName;
     63     }
     64 
     65     /**
     66 * @projectName   testMyClass
     67 * @brief         串口写数据
     68 * @author        SMY
     69 * @date          2019-03-21
     70 */
     71     void write(QString str);
     72     void write(const char *data, qint64 len);
     73 
     74     /**
     75 * @projectName   testMyClass
     76 * @brief         QByteArray 转换为 QString
     77 * @author        SMY
     78 * @date          2019-03-21
     79 */
     80     QString ByteArrayToHexString(QByteArray data);
     81 
     82 signals:
     83     /**
     84 * @projectName   testMyClass
     85 * @brief         串口读取到数据的信号
     86 * @author        SMY
     87 * @date          2019-03-21
     88 */
     89 
     90     void readyRead(QByteArray data);
     91 
     92 private:
     93     QSerialPort *m_port;
     94 
     95     void readyRead_slot();
     96 
     97 
     98 
     99 };
    100 
    101 #endif // SERIALPORT__H

    serialPort_.cpp

     1 #include "serialport_.h"
     2 
     3 serialPort_::serialPort_(QObject *o)
     4 {
     5     m_port = new QSerialPort();
     6 
     7 
     8     connect(m_port,&QSerialPort::readyRead,this,&serialPort_::readyRead_slot);
     9 
    10 }
    11 
    12 bool serialPort_::openPort(QString portName, qint32 baudRate,
    13                          QSerialPort::DataBits dataBits, QSerialPort::Parity parity,
    14                            QSerialPort::StopBits stopBits, QSerialPort::FlowControl flowControl)
    15 {
    16 
    17     m_port->setPortName(portName);
    18     m_port->setBaudRate(baudRate);
    19     m_port->setDataBits(dataBits);
    20     m_port->setParity(parity);
    21     m_port->setStopBits(stopBits);
    22     m_port->setFlowControl(flowControl);
    23 
    24     if(m_port->open(QIODevice::ReadWrite))
    25     {
    26         return true;
    27     }
    28     else
    29         return false;
    30 }
    31 
    32 void serialPort_::write(QString str)
    33 {
    34     m_port->write(str.toUtf8().data());
    35 }
    36 
    37 void serialPort_::write(const char *data, qint64 len)
    38 {
    39     m_port->write(data,len);
    40 }
    41 
    42 QString serialPort_::ByteArrayToHexString(QByteArray data)
    43 {
    44     QString ret(data.toHex().toUpper());
    45     int len = ret.length()/2;
    46     for(int i=1;i<len;i++)
    47     {
    48         ret.insert(2*i+i-1," ");
    49     }
    50 
    51     return ret;
    52 }
    53 
    54 void serialPort_::readyRead_slot()
    55 {
    56     QByteArray data = m_port->readAll();
    57     emit readyRead(data);
    58 }
  • 相关阅读:
    Python+Selenium学习笔记8
    Python+Selenium学习笔记7
    Python+Selenium学习笔记6
    Python+Selenium学习笔记5
    Python+Selenium学习笔记4
    java类为什么是单继承。类的继承,实现接口。
    类,对象之间的关系
    java 向上转型 向下转型
    jsp 中 request.getHeader() 相关详细
    MyEclipse使用经验总结
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/14863521.html
Copyright © 2020-2023  润新知