QT5 串列埠收發例項程式碼
阿新 • • 發佈:2018-12-31
以下程式碼是自己測試門禁系統使用的
主要用到了串列埠的接收和傳送
開發環境:xp QT5.1.1
串列埠:38400 N 8 1
自動檢測可用串列埠
在xp上測試沒問題
有些usb轉串列埠會出現波特率不準的問題,CH340的usb轉232使用完全正常
以下為收發的執行緒.h 和.cpp兩個檔案
最後附上轉sacii顯示的程式碼
#ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> #include <stdio.h> #include <QtGlobal> class MyThread : public QThread { Q_OBJECT public: QByteArray requestData; QByteArray TxData; MyThread(); void setMessage(const QString &message); void setPortnum(const QString &num); void stop(); void startCom(); void changeTxState(bool stat); void changeRxState(bool stat); void changeComState(bool stat); signals: void request(const QString &s); void comRecive(); protected: void run(); private: QString messageStr; QString portnum; volatile bool com_opened; volatile bool stopped; volatile bool tx_event; volatile bool rx_event; }; #endif // MYTHREAD_H
#include "mythread.h" #include <QtDebug> //FOR RS232 #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> MyThread::MyThread() { stopped=false; } void MyThread::run() { QSerialPort *my_serialport= new QSerialPort; while(!stopped) { if(stopped&&com_opened) { my_serialport->close(); com_opened=false; } if(!com_opened) { //open com qDebug() << "Brush:" <<"---open com port------"; com_opened=true; my_serialport->setPortName(portnum); my_serialport->open(QIODevice::ReadWrite); my_serialport->setBaudRate(QSerialPort::Baud38400,QSerialPort::AllDirections); my_serialport->setDataBits(QSerialPort::Data8); my_serialport->setParity(QSerialPort::NoParity); my_serialport->setStopBits(QSerialPort::OneStop); // my_serialport->setStopBits(QSerialPort::TwoStop); my_serialport->setFlowControl(QSerialPort::NoFlowControl); com_opened=true; } if(this->com_opened&&this->tx_event) { this->tx_event=false; my_serialport->clear(QSerialPort::AllDirections); qDebug() << "Brush:" <<"send data to com2"<<this->TxData.length(); qDebug() << "arr size:" <<this->TxData.length(); my_serialport->write(this->TxData); if (my_serialport->waitForBytesWritten(5)) { qDebug() << "Brush:" <<"send data success"; if (my_serialport->waitForReadyRead(1500)) //1s { requestData = my_serialport->readAll(); while (my_serialport->waitForReadyRead(15)) requestData += my_serialport->readAll(); emit(this->comRecive()); }else { qDebug() << "Brush:" <<"wait return time out"; } }else { qDebug() << "Brush:" <<"send time out"; } } if (my_serialport->waitForReadyRead(5)) //50ms { while (my_serialport->waitForReadyRead(5)) this->msleep(20); requestData = my_serialport->readAll(); emit(this->comRecive()); } if(stopped&&com_opened) { my_serialport->close(); com_opened=false; } } } void MyThread::stop() { stopped=true; } void MyThread::startCom() { stopped=false; } void MyThread::changeComState(bool stat) { com_opened=stat; } void MyThread::setMessage(const QString &message) { messageStr = message; } void MyThread::setPortnum(const QString &num) { portnum=num; } void MyThread:: changeTxState(bool stat) { tx_event=stat; } void MyThread:: changeRxState(bool stat) { rx_event=stat; }
顯示部分 比如收到0xff 在text框顯示 FF
void MainWindow::displayRxData() { QString str; char tmp[100]; char *buf; char var; QDateTime *datatime=new QDateTime(QDateTime::currentDateTime()); if(threadA.requestData.size()>0) { str="收到資料: "; str+=datatime->time().toString(); ui->textEdit_rx->append(str); str.clear(); buf=threadA.requestData.data(); if(buf[3]==0x01) ui->textEdit_tx->append("加卡成功!"); if(buf[3]==0x02) ui->textEdit_tx->append("刪卡成功!"); if(buf[3]==0x03) ui->textEdit_tx->append("開門成功!"); if(buf[3]==0x04) ui->textEdit_tx->append("關門成功!"); qDebug() << "receive num:" <<threadA.requestData.size(); for(var=0;var<threadA.requestData.size();var++) { ::snprintf(tmp,100, "%02X", (unsigned char)(*buf)); buf++; str+=QString::fromUtf8(tmp); str+=" "; } ui->textEdit_rx->append(str); } threadA.requestData.clear(); }