qt串口消息模拟器怎么实现
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇内容介绍了"qt串口消息模拟器怎么实现"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!//main
千家信息网最后更新 2025年12月02日qt串口消息模拟器怎么实现
本篇内容介绍了"qt串口消息模拟器怎么实现"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
//mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include#include #include enum MsgType{ msg_Tacho = 0x00, msg_Speed = 0x01, msg_Tempt = 0x03};QT_BEGIN_NAMESPACEnamespace Ui { class MainWindow; }QT_END_NAMESPACEclass MainWindow : public QMainWindow{ Q_OBJECTpublic: 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"#includestatic 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("x x x",flag,flag0xAF,datalen); int ret = m_port->read((char*)pbuff, datalen); message = pbuff+HEADLENTH; if(true) { for(int i = 0;i getChar((char*)&flagtype); m_port->getChar((char*)&datalen); printf("x x x ",flag,flagtype,datalen); int ret = m_port->read((char*)pbuff, datalen + 1); message = pbuff; if(true) { for(int i = 0;i 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()< > 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串口消息模拟器怎么实现"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
温度
串口
车速
转速
消息
测试
自动化
输入
模拟器
成功
内容
更多
知识
程序
范围
实用
学有所成
接下来
困境
实际
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
dede查询表单数据库
静安区市场软件开发咨询热线
2018 两会 网络安全
辽宁省高新技术产业数据库
储存过程删除数据库
云南省委网信办网络安全管理处
数据库怎么执行语句
dna数据库采集点
提高分布式数据库性能的技术
郑州软件开发外包
cisco网络技术挑战赛
网络安全工程师证书好考么
qq空间相册链接服务器错误
u盘如何做服务器系统启动盘
暑假网络安全作业
说说关于网络安全的话
国家实施网络安全身份战略
软件开发保险业务
app连接服务器地址无法反编译
什么什么搞服务器
sql多表查询并更新数据库
vf三种数据库类型
网络安全的新闻标题类怎么写
在轻量应用服务器中安装软件
网络安全宣传海报画
福建师范大学数据库期末考试
计算机网络技术大专招
吉林大学研究生网络安全专业
数据库是计算机三级吗
数据库中小数类型是什么