本篇内容介绍了“qt串口消息模拟器怎么实现”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
//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测试工具"); 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; //转速表 ui->horizontalSlider->setRange(0,8000); ui->horizontalSlider->setSingleStep(50); //车速表 ui->horizontalSlider_2->setRange(0,240); ui->horizontalSlider_2->setSingleStep(2); //左区温度 ui->horizontalSlider_3->setRange(15,32); //右区温度 ui->horizontalSlider_4->setRange(15,32); //默认转速选中 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("打开串口"+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("打开串口"+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: //车速 printf("datavalid:%d\n",message[1]); printf("alarminfo:%d\n",message[2]); printf("speedvalue:%d\n",(message[3]<<8)+message[4]); break; case 0x02: //转速 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: //车内温度 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: //时间 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("[发送]转速:%1,校验:%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("[发送]车速:%1,校验:%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("[发送]左区温度:%1,右区温度%2,校验:%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("重复第%1发送数据...").arg(m_sendRepeatNum)); if(m_sendRepeatNum == 3) { m_connectStatus = 0xf4; showLog("消息重发次数已达3次,串口将被断开,请检察后重新连接..."); 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("[发送]左区温度:%1,校验:%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("[发送]右区温度:%1,校验:%2\n").arg(value).arg(sendbuff_21[8])); } void MainWindow::on_pushButton_connect_clicked() { if(m_port == nullptr) { openSerPort(); } else { showLog("串开已经打开,断开后可尝试重新连接..."); } } void MainWindow::on_pushButton_send_clicked() { QString senddata = ui->lineEdit->text(); if(m_port == nullptr) { showLog("请先连接到串口..."); return; } if(senddata.isEmpty()) { showLog("发送值为空,请输入一个值后再发送..."); return; } if(m_connectStatus!=0xff) { showLog("与MCU握手失败,无法发送消息..."); return; } if(ui->radioButton->isChecked()) //转速被选中 { if(senddata.toUInt()<=8000) { SendData(senddata,msg_Tacho); } else { showLog("输入的转速值不符,请确认值范围0-8000中..."); } } else if(ui->radioButton_2->isChecked()) // 车速被选中 { if(senddata.toUInt()<=240) { SendData(senddata,msg_Speed); } else { showLog("输入的车速值不符,请确认值范围0-240中..."); } } else //左右区温度,设置为一样 { if(senddata.toFloat()<=32 && senddata.toFloat()>=15) { SendData(senddata,msg_Tempt); } else { showLog("输入的温度值不符,请确认值范围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+"已断开连接..."); m_connectStatus = 0x00; m_msgStatus = false; m_sendRepeatNum = 0; m_timer->stop(); }); } } void MainWindow::on_action_automated_triggered() { if(m_port == nullptr) { showLog("请先连接到串口..."); return; } showLog("自动化测试程序已启动..."); 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("自动化测试程序已关闭..."); } else { showLog("没有自动化测试在运行..."); } } void MainWindow::on_textBrowser_textChanged() { ui->textBrowser->moveCursor(QTextCursor::End); }
“qt串口消息模拟器怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。