QT5 串口收发实例代码

以下代码是自己测试门禁系统使用的

主要用到了串口的接收和发送

 开发环境:xp  QT5.1.1

串口:38400   N  8  1

自动检测可用串口

在xp上测试没问题

有些usb转串口会出现波特率不准的问题,CH340的usb转232使用完全正常

以下为收发的线程.h 和.cpp两个文件

最后附上转sacii显示的代码

如果要参考的话,源代码已上传:http://download.csdn.net/detail/liang890319/6463227

  1. #ifndef MYTHREAD_H  
  2. #define MYTHREAD_H  
  3.   
  4. #include <QThread>  
  5. #include <stdio.h>  
  6. #include <QtGlobal>  
  7.   
  8. class MyThread : public QThread  
  9. {  
  10.     Q_OBJECT  
  11. public:  
  12.     QByteArray requestData;  
  13.     QByteArray TxData;  
  14.     MyThread();  
  15.   
  16.     void setMessage(const QString &message);  
  17.     void setPortnum(const QString &num);  
  18.     void stop();  
  19.     void startCom();  
  20.     void changeTxState(bool stat);  
  21.     void changeRxState(bool stat);  
  22.     void changeComState(bool stat);  
  23.   
  24. signals:  
  25.     void request(const QString &s);  
  26.     void comRecive();  
  27.   
  28.   
  29.   
  30. protected:  
  31.     void run();  
  32.   
  33. private:  
  34.     QString messageStr;  
  35.     QString portnum;  
  36.     volatile bool com_opened;  
  37.     volatile bool stopped;  
  38.     volatile bool tx_event;  
  39.     volatile bool rx_event;  
  40. };  
  41.   
  42. #endif // MYTHREAD_H  


 

  1. #include "mythread.h"  
  2. #include <QtDebug>  
  3. //FOR RS232  
  4. #include <QtSerialPort/QSerialPort>  
  5. #include <QtSerialPort/QSerialPortInfo>  
  6.   
  7. MyThread::MyThread()  
  8. {  
  9.    stopped=false;  
  10.   
  11. }  
  12.   
  13. void MyThread::run()  
  14. {  
  15.     QSerialPort *my_serialport= new QSerialPort;  
  16.   
  17.     while(!stopped)  
  18.     {  
  19.         if(stopped&&com_opened)  
  20.         {  
  21.             my_serialport->close();  
  22.             com_opened=false;  
  23.   
  24.         }  
  25.         if(!com_opened)  
  26.         {  
  27.             //open com  
  28.             qDebug() << "Brush:" <<"---open com port------";  
  29.             com_opened=true;  
  30.             my_serialport->setPortName(portnum);  
  31.             my_serialport->open(QIODevice::ReadWrite);  
  32.             my_serialport->setBaudRate(QSerialPort::Baud38400,QSerialPort::AllDirections);  
  33.             my_serialport->setDataBits(QSerialPort::Data8);  
  34.             my_serialport->setParity(QSerialPort::NoParity);  
  35.             my_serialport->setStopBits(QSerialPort::OneStop);  
  36.            // my_serialport->setStopBits(QSerialPort::TwoStop);  
  37.             my_serialport->setFlowControl(QSerialPort::NoFlowControl);  
  38.             com_opened=true;  
  39.         }  
  40.         if(this->com_opened&&this->tx_event)  
  41.         {  
  42.                 this->tx_event=false;  
  43.                 my_serialport->clear(QSerialPort::AllDirections);  
  44.                 qDebug() << "Brush:" <<"send data to com2"<<this->TxData.length();  
  45.                 qDebug() << "arr size:" <<this->TxData.length();  
  46.                my_serialport->write(this->TxData);  
  47.               if (my_serialport->waitForBytesWritten(5))  
  48.               {  
  49.                 qDebug() << "Brush:" <<"send data success";  
  50.                 if (my_serialport->waitForReadyRead(1500))  //1s  
  51.                 {  
  52.                     requestData = my_serialport->readAll();  
  53.                     while (my_serialport->waitForReadyRead(15))  
  54.                     requestData += my_serialport->readAll();  
  55.                     emit(this->comRecive());  
  56.                 }else  
  57.                 {  
  58.                         qDebug() << "Brush:" <<"wait return time out";  
  59.                 }  
  60.               }else  
  61.               {  
  62.                   qDebug() << "Brush:" <<"send time out";  
  63.               }  
  64.         }  
  65.     if (my_serialport->waitForReadyRead(5))  //50ms  
  66.     {  
  67.         while (my_serialport->waitForReadyRead(5))  
  68.         this->msleep(20);  
  69.         requestData = my_serialport->readAll();  
  70.         emit(this->comRecive());  
  71.     }  
  72.     if(stopped&&com_opened)  
  73.     {  
  74.         my_serialport->close();  
  75.         com_opened=false;  
  76.     }  
  77.     }  
  78. }  
  79.   
  80. void MyThread::stop()  
  81. {  
  82.    stopped=true;  
  83.   
  84. }  
  85. void MyThread::startCom()  
  86. {  
  87.    stopped=false;  
  88.   
  89. }  
  90. void MyThread::changeComState(bool stat)  
  91.  {  
  92.         com_opened=stat;  
  93. }  
  94. void MyThread::setMessage(const QString &message)  
  95. {  
  96.    messageStr = message;  
  97.   
  98. }  
  99. void MyThread::setPortnum(const QString &num)  
  100. {  
  101.     portnum=num;  
  102.   
  103. }  
  104. void MyThread:: changeTxState(bool stat)  
  105. {  
  106.         tx_event=stat;  
  107. }  
  108. void MyThread:: changeRxState(bool stat)  
  109. {  
  110.          rx_event=stat;  
  111. }  


 

显示部分 比如收到0xff  在text框显示 FF

  1. void MainWindow::displayRxData()  
  2. {  
  3.     QString str;  
  4.     char tmp[100];  
  5.     char *buf;  
  6.     char var;  
  7.     QDateTime *datatime=new QDateTime(QDateTime::currentDateTime());  
  8.     if(threadA.requestData.size()>0)  
  9.    {  
  10.         str="收到数据: ";  
  11.         str+=datatime->time().toString();  
  12.         ui->textEdit_rx->append(str);  
  13.         str.clear();  
  14.   
  15.         buf=threadA.requestData.data();  
  16.         if(buf[3]==0x01) ui->textEdit_tx->append("加卡成功!");  
  17.         if(buf[3]==0x02) ui->textEdit_tx->append("删卡成功!");  
  18.         if(buf[3]==0x03) ui->textEdit_tx->append("开门成功!");  
  19.         if(buf[3]==0x04) ui->textEdit_tx->append("关门成功!");  
  20.         qDebug() << "receive num:" <<threadA.requestData.size();  
  21.         for(var=0;var<threadA.requestData.size();var++)  
  22.         {  
  23.             ::snprintf(tmp,100, "%02X", (unsigned char)(*buf));  
  24.             buf++;  
  25.             str+=QString::fromUtf8(tmp);  
  26.             str+=" ";  
  27.         }  
  28.         ui->textEdit_rx->append(str);  
  29.     }  
  30.     threadA.requestData.clear();  
  31. }  

 

原文地址:https://www.cnblogs.com/wanghuaijun/p/6132680.html