QT串口助手(四):数据发送 | 您所在的位置:网站首页 › qt串口接收数据后马上发送信号 › QT串口助手(四):数据发送 |
作者:zzssdd2 E-mail:[email protected] 一、前言开发环境:Qt5.12.10 + MinGW 实现的功能 串口数据的发送 ascii字符与hex字符的相互转换 自动追加回车换行符(\r\n) 发送数据的统计与显示 发送清零 定时发送涉及的知识点 QSerialPort类的使用 数据格式的转换 QTimer类的使用 控件QPlainTextEdit、QCheckBox、QPushButton、QLabel的使用 二、功能实现在《QT串口助手(三):数据接收》实现了接收模块的功能,本章讲解发送模块的各个功能。 2.1、字符判断若勾选了HEX格式发送,那么需要对发送框的字符进行合法判断。这里使用到QPlainTextEdit的textChanged信号来监测发送框数据的改变,在槽函数中对数据进行判别: /*发送文本框信号槽*/ connect(ui->Send_TextEdit, &QPlainTextEdit::textChanged, this, [=](){ //获取发送框字符 SendTextEditStr = ui->Send_TextEdit->document()->toPlainText(); if (SendTextEditStr.isEmpty()) { return; } //勾选hex发送则判断是否有非法hex字符 if (ui->HexSend_checkBox->isChecked()) { char ch; bool flag = false; uint32_t i, len; //去掉无用符号 SendTextEditStr = SendTextEditStr.replace(' ',""); SendTextEditStr = SendTextEditStr.replace(',',""); SendTextEditStr = SendTextEditStr.replace('\r',""); SendTextEditStr = SendTextEditStr.replace('\n',""); SendTextEditStr = SendTextEditStr.replace('\t',""); SendTextEditStr = SendTextEditStr.replace("0x",""); SendTextEditStr = SendTextEditStr.replace("0X",""); //判断数据合法性 for(i = 0, len = SendTextEditStr.length(); i < len; i++) { ch = SendTextEditStr.at(i).toLatin1(); if (ch >= '0' && ch = 'a' && ch = 'A' && ch |
CopyRight 2018-2019 实验室设备网 版权所有 |