您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“qt串口消息模擬器怎么實(shí)現(xiàn)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
//mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSerialPort> #include <QTimer> enum MsgType{ msg_Tacho = 0x00, msg_Speed = 0x01, msg_Tempt = 0x03 }; QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void openSerPort(); void SMD_01(unsigned char * message); void SMD_14(unsigned char * message); void showLog(QString str); void SendData(QString& msg,int type, QString temp = "all"); void SendRepeatData(); private slots: void on_horizontalSlider_valueChanged(int value); void on_horizontalSlider_3_valueChanged(int value); void on_horizontalSlider_2_valueChanged(int value); void on_horizontalSlider_4_valueChanged(int value); void on_pushButton_connect_clicked(); void on_pushButton_send_clicked(); void on_comboBox_currentIndexChanged(const QString &arg1); void on_pushButton_disconnect_clicked(); void on_action_automated_triggered(); void on_actionCancel_automated_triggered(); void on_textBrowser_textChanged(); private: void SlotMsgDecode(); private: Ui::MainWindow *ui; QSerialPort* m_port; QString m_ncom; QTimer* m_timer; unsigned char* message; unsigned char m_connectStatus; bool m_msgStatus; int m_sendRepeatNum; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> static unsigned char pbuff[512]; static unsigned char sendbuff_26[10] = {0x2e, 0x26, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; static unsigned char sendbuff_21[9] = {0x2e, 0x21, 0x05, 0x00, 0x00, 0x1e, 0x1e, 0x00, 0x00}; const int HEADLENTH = 2; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle("XXXX測(cè)試工具"); m_ncom = "COM1"; m_port = nullptr; message = nullptr; m_timer = new QTimer; m_timer->setInterval(160); m_connectStatus = 0x00; m_msgStatus = false; m_sendRepeatNum = 0; //轉(zhuǎn)速表 ui->horizontalSlider->setRange(0,8000); ui->horizontalSlider->setSingleStep(50); //車(chē)速表 ui->horizontalSlider_2->setRange(0,240); ui->horizontalSlider_2->setSingleStep(2); //左區(qū)溫度 ui->horizontalSlider_3->setRange(15,32); //右區(qū)溫度 ui->horizontalSlider_4->setRange(15,32); //默認(rèn)轉(zhuǎn)速選中 ui->radioButton->setChecked(true); } MainWindow::~MainWindow() { delete ui; disconnect(m_port, &QSerialPort::readyRead,this,&MainWindow::SlotMsgDecode); m_port->close(); delete m_port; delete m_timer; } void MainWindow::openSerPort() { m_port = new QSerialPort(m_ncom); bool b_startOK = false; if(m_port->open(QIODevice::ReadWrite)) { m_port->setBaudRate(QSerialPort::Baud38400); m_port->setParity(QSerialPort::NoParity); m_port->setDataBits(QSerialPort::Data8); m_port->setStopBits(QSerialPort::OneStop); m_port->setFlowControl(QSerialPort::UnknownFlowControl);// ? why not UnknownFlowControl NoFlowControl m_port->clearError(); m_port->clear(); showLog("打開(kāi)串口"+m_ncom+"成功..."); b_startOK = true; } else { b_startOK = false; QSerialPort::SerialPortError err= m_port->error(); printf("Error. Can't Open Serial Por,:error:%d !\n",err); showLog("打開(kāi)串口"+m_ncom+"失敗..."); m_port->clear(); m_port->close(); delete m_port; m_port = nullptr; } if(b_startOK) { connect(m_port, &QSerialPort::readyRead,this,&MainWindow::SlotMsgDecode); } } void MainWindow::SlotMsgDecode() { memset(pbuff,0,512); unsigned char flag,flag0xAF,flagtype,datalen; m_port->getChar((char*)&flag); if(flag == 0xFA) { m_port->getChar((char*)&flag0xAF); m_port->getChar((char*)&datalen); printf("%02x %02x %02x",flag,flag0xAF,datalen); int ret = m_port->read((char*)pbuff, datalen); message = pbuff+HEADLENTH; if(true) { for(int i = 0;i<ret;i++) { printf("%02x ",pbuff[i]); } printf("\n"); } unsigned char iDataID = message[0]; unsigned char bySmd = message[1]; switch (iDataID) { case 0x06: if(bySmd == 0x01) SMD_01(&message[2]); else if(bySmd == 0x14) SMD_14(&message[2]); break; default: break; } } else if(flag == 0x2E) { m_port->getChar((char*)&flagtype); m_port->getChar((char*)&datalen); printf("%02x %02x %02x ",flag,flagtype,datalen); int ret = m_port->read((char*)pbuff, datalen + 1); message = pbuff; if(true) { for(int i = 0;i<ret;i++) { printf("%02x ",pbuff[i]); } printf("\n"); } if(message[0] == 0x01) { m_connectStatus = 0xff; m_port->write((char*)&m_connectStatus,1); showLog("與MCU握手成功..."); } } else if(flag == 0xff) { m_msgStatus = true; m_sendRepeatNum = 0; } else { m_msgStatus = false; } m_port->readAll(); } void MainWindow::SMD_01(unsigned char *message) { printf("%s\n",__FUNCTION__); unsigned char byMsgdatatype = message[0]; switch (byMsgdatatype) { case 0x01: //車(chē)速 printf("datavalid:%d\n",message[1]); printf("alarminfo:%d\n",message[2]); printf("speedvalue:%d\n",(message[3]<<8)+message[4]); break; case 0x02: //轉(zhuǎn)速 printf("datavalid:%d\n",message[1]); printf("alarminfo:%d\n",message[2]); printf("tachovalue:%d\n",(message[3]<<8)+message[4]); break; default: break; } } void MainWindow::SMD_14(unsigned char *message) { printf("%s\n",__FUNCTION__); unsigned char byMsgdatatype = message[0]; switch (byMsgdatatype) { case 0x01: //車(chē)內(nèi)溫度 printf("datavalid:%d\n",message[1]); printf("leftTempture:%d\n",((message[2]<<8)+message[3])); printf("rightTempture:%d\n",(message[4]<<8)+message[5]); break; case 0x02: //時(shí)間 printf("%d:%d:%d\n",message[1],message[2],message[3]); break; default: break; } } void MainWindow::showLog(QString str) { QString strtemp = ui->textBrowser->toPlainText(); strtemp+=str; strtemp +="\n"; if(strtemp.length()>4096) { strtemp.clear(); } ui->textBrowser->setPlainText(strtemp); //qDebug()<<strtemp.length(); } void MainWindow::SendData(QString& msg, int type, QString temp) { switch (type) { case msg_Tacho: { int tacho = msg.toUInt(); sendbuff_26[5] = tacho >> 8; sendbuff_26[6] = tacho - (sendbuff_26[5]<<8); sendbuff_26[9] = ((sendbuff_26[1]+sendbuff_26[2]+sendbuff_26[3]+sendbuff_26[4]+sendbuff_26[5]+sendbuff_26[6] \ +sendbuff_26[7]+sendbuff_26[8])^0xff)&0xff; m_port->write((char*)sendbuff_26,sizeof(sendbuff_26)); showLog(QString("[發(fā)送]轉(zhuǎn)速:%1,校驗(yàn):%2\n").arg((sendbuff_26[5]<<8)+ sendbuff_26[6]).arg(sendbuff_26[9])); } break; case msg_Speed: { int speed = msg.toUInt(); sendbuff_26[3] = speed >> 8; sendbuff_26[4] = speed - (sendbuff_26[3]<<8); sendbuff_26[9] = ((sendbuff_26[1]+sendbuff_26[2]+sendbuff_26[3]+sendbuff_26[4]+sendbuff_26[5]+sendbuff_26[6] \ +sendbuff_26[7]+sendbuff_26[8])^0xff)&0xff; m_port->write((char*)sendbuff_26,sizeof(sendbuff_26)); showLog(QString("[發(fā)送]車(chē)速:%1,校驗(yàn):%2\n").arg((sendbuff_26[3]<<8)+ sendbuff_26[4]).arg(sendbuff_26[9])); } break; case msg_Tempt: { int tempture = int(0x1e + (msg.toFloat()-0x0f)*2.0); if(temp == "left") sendbuff_21[5] = tempture; else if(temp == "right") sendbuff_21[6] = tempture; else { sendbuff_21[5] = tempture; sendbuff_21[6] = tempture; } sendbuff_21[8] = ((sendbuff_21[1]+sendbuff_21[2]+sendbuff_21[3]+sendbuff_21[4]+sendbuff_21[5]+sendbuff_21[6] \ +sendbuff_21[7])^0xff)&0xff; m_port->write((char*)sendbuff_21,sizeof(sendbuff_21)); showLog(QString("[發(fā)送]左區(qū)溫度:%1,右區(qū)溫度%2,校驗(yàn):%3\n").arg(msg.toFloat()).arg(msg.toFloat()).arg(sendbuff_21[8])); } break; default: break; } } void MainWindow::SendRepeatData() { if(m_msgStatus == false) { m_port->write((char*)sendbuff_26,sizeof(sendbuff_26)); m_port->write((char*)sendbuff_21,sizeof(sendbuff_21)); ++m_sendRepeatNum; showLog(QString("重復(fù)第%1發(fā)送數(shù)據(jù)...").arg(m_sendRepeatNum)); if(m_sendRepeatNum == 3) { m_connectStatus = 0xf4; showLog("消息重發(fā)次數(shù)已達(dá)3次,串口將被斷開(kāi),請(qǐng)檢察后重新連接..."); on_pushButton_disconnect_clicked(); } } else { m_sendRepeatNum = 0; } } void MainWindow::on_horizontalSlider_valueChanged(int value) { QString data = QString("%1").arg(value); ui->label_tacho->setText(data); if(m_port == nullptr) return; SendData(data,msg_Tacho); } void MainWindow::on_horizontalSlider_2_valueChanged(int value) { QString data = QString("%1").arg(value); ui->label_speed->setText(data); if(m_port == nullptr) return; SendData(data,msg_Speed); } void MainWindow::on_horizontalSlider_3_valueChanged(int value) { QString data = QString("%1").arg(value); ui->label_tempture->setText(data); if(m_port == nullptr) return; SendData(data,msg_Tempt,"left"); showLog(QString("[發(fā)送]左區(qū)溫度:%1,校驗(yàn):%2\n").arg(value).arg(sendbuff_21[8])); } void MainWindow::on_horizontalSlider_4_valueChanged(int value) { QString data = QString("%1").arg(value); ui->label_tempture_2->setText(data); if(m_port == nullptr) return; SendData(data,msg_Tempt,"right"); showLog(QString("[發(fā)送]右區(qū)溫度:%1,校驗(yàn):%2\n").arg(value).arg(sendbuff_21[8])); } void MainWindow::on_pushButton_connect_clicked() { if(m_port == nullptr) { openSerPort(); } else { showLog("串開(kāi)已經(jīng)打開(kāi),斷開(kāi)后可嘗試重新連接..."); } } void MainWindow::on_pushButton_send_clicked() { QString senddata = ui->lineEdit->text(); if(m_port == nullptr) { showLog("請(qǐng)先連接到串口..."); return; } if(senddata.isEmpty()) { showLog("發(fā)送值為空,請(qǐng)輸入一個(gè)值后再發(fā)送..."); return; } if(m_connectStatus!=0xff) { showLog("與MCU握手失敗,無(wú)法發(fā)送消息..."); return; } if(ui->radioButton->isChecked()) //轉(zhuǎn)速被選中 { if(senddata.toUInt()<=8000) { SendData(senddata,msg_Tacho); } else { showLog("輸入的轉(zhuǎn)速值不符,請(qǐng)確認(rèn)值范圍0-8000中..."); } } else if(ui->radioButton_2->isChecked()) // 車(chē)速被選中 { if(senddata.toUInt()<=240) { SendData(senddata,msg_Speed); } else { showLog("輸入的車(chē)速值不符,請(qǐng)確認(rèn)值范圍0-240中..."); } } else //左右區(qū)溫度,設(shè)置為一樣 { if(senddata.toFloat()<=32 && senddata.toFloat()>=15) { SendData(senddata,msg_Tempt); } else { showLog("輸入的溫度值不符,請(qǐng)確認(rèn)值范圍15-32中..."); } } QTimer::singleShot(120,this,[this](){ SendRepeatData(); }); QTimer::singleShot(240,this,[this](){ SendRepeatData(); }); QTimer::singleShot(360,this,[this](){ SendRepeatData(); }); } void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) { m_ncom = arg1; } void MainWindow::on_pushButton_disconnect_clicked() { if(m_port!=nullptr && m_port->isOpen()) { disconnect(m_port, &QSerialPort::readyRead,this,&MainWindow::SlotMsgDecode); unsigned char resetbuf[4] = {0x2E,0x81,0x01,0xF4}; m_port->clear(); m_port->write((char*)resetbuf,sizeof(resetbuf)); QTimer::singleShot(100,this,[this](){ m_port->close(); delete m_port; m_port = nullptr; showLog("串口"+m_ncom+"已斷開(kāi)連接..."); m_connectStatus = 0x00; m_msgStatus = false; m_sendRepeatNum = 0; m_timer->stop(); }); } } void MainWindow::on_action_automated_triggered() { if(m_port == nullptr) { showLog("請(qǐng)先連接到串口..."); return; } showLog("自動(dòng)化測(cè)試程序已啟動(dòng)..."); if(m_timer!=nullptr && !m_timer->isActive()) { m_timer->start(); connect(m_timer,&QTimer::timeout,[=]{ uint tacho = ui->horizontalSlider->value(); tacho+=100; tacho%=8100; ui->horizontalSlider->setValue(tacho); uint speed = ui->horizontalSlider_2->value(); speed += 10; speed%=250; ui->horizontalSlider_2->setValue(speed); uint tempture = ui->horizontalSlider_3->value(); tempture+=1; if(tempture == 33) tempture = 15; ui->horizontalSlider_3->setValue(tempture); ui->horizontalSlider_4->setValue(tempture); }); } } void MainWindow::on_actionCancel_automated_triggered() { if(m_timer->isActive()) { m_timer->stop(); showLog("自動(dòng)化測(cè)試程序已關(guān)閉..."); } else { showLog("沒(méi)有自動(dòng)化測(cè)試在運(yùn)行..."); } } void MainWindow::on_textBrowser_textChanged() { ui->textBrowser->moveCursor(QTextCursor::End); }
“qt串口消息模擬器怎么實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。