qt串口消息模拟器怎么实现
发表于:2024-10-10 作者:千家信息网编辑
千家信息网最后更新 2024年10月10日,本篇内容介绍了"qt串口消息模拟器怎么实现"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!//main
千家信息网最后更新 2024年10月10日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安全错误
数据库的锁怎样保障安全
软件开发过程中安全占比
数据库 水平分解
黄浦区银联数据库服务费
微乐棋牌服务器连不上
互联网智慧教育科技
华夏恒生互联网科技基金发行
即时通讯软件开发哪家便宜
数据库加管锁
香港奇门遁甲软件开发
数据库设计一个图书表
我所认识网络安全
网络安全班级群消息
银行的数据库是干嘛的
登录百度账号提示网络安全风险
两排筛选重复数据库
表单提交是到数据库庅
虎丘区推广网络技术包括什么
数据库 项
淄博化工软件开发报价
黄河鲲鹏服务器和台式机生产线
白狐吉他谱软件开发
郑州人工智能软件开发收费报价表
软件开发控标参数
服务器待开通怎么解决
国家对网络安全的要求
四川saas管理软件开发商
福汇 服务器
发型设计软件开发
app软件开发服务企业
华为bmc软件开发部