E-mail:zzssdd2@foxmail.com
一、前言开发环境: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 <= '9') { flag = false; } else if (ch >= 'a' && ch <= 'f') { flag = false; } else if (ch >= 'A' && ch <= 'F') { flag = false; } else { flag = true; } } if(flag) QMessageBox::warning(this,"警告","输入内容包含非法16进制字符"); } //QString转QByteArray SendTextEditBa = SendTextEditStr.toUtf8(); }); [signal]void QPlainTextEdit::textChanged()This signal is emitted whenever the document's content changes; for example, when text is inserted or deleted, or when formatting is applied.
Note: Notifier signal for property plainText.
这样我们在进行输入时,如果包含非法字符就会有弹框提示(我这里对','、"0x"、"0X"等字符过滤是为了方便有时从代码中直接复制数组数据发送):
2.2、数据转换通过是否勾选HEX发送判断使用ascii格式还是hex格式发送数据,使用QCheckBox的stateChanged信号对勾选状态进行检测,然后在对应的槽函数中进行数据格式的转换。
/*HEX发送chexkBox信号槽*/ connect(ui->HexSend_checkBox,&QCheckBox::stateChanged,this,[=](int state){ if (SendTextEditStr.isEmpty()) { return; } //asccii与hex转换 if (state == Qt::Checked) { //转换成QByteArray -> 转换成16进制数,按空格分开 -> 转换为大写 SendTextEditBa = SendTextEditBa.toHex(' ').toUpper(); ui->Send_TextEdit->document()->setPlainText(SendTextEditBa); } else { //从QByteArray转换为QString SendTextEditStr = SendTextEditBa.fromHex(SendTextEditBa); ui->Send_TextEdit->document()->setPlainText(SendTextEditStr); } }); [signal]void QCheckBox::stateChanged(int state)This signal is emitted whenever the checkbox's state changes, i.e., whenever the user checks or unchecks it.
state contains the checkbox's new Qt::CheckState.
2.3、手动发送当点击发送按钮时触发QPushButton的点击信号,在对应的槽函数中将发送框的数据按照选定格式发送出去,程序主体如下:
/* 函 数:on_Send_Bt_clicked 描 述:发送按键点击槽函数 输 入:无 输 出:无 */ void Widget::on_Send_Bt_clicked() { if (isSerialOpen != false) { /*将发送框数据发送*/ SerialSendData(SendTextEditBa); } else { QMessageBox::information(this, "提示", "串口未打开"); } }QpushButton继承自QAbstractButton。关于按键点击信号的描述:
[signal]void QAbstractButton::clicked(bool checked = false)This signal is emitted when the button is activated (i.e., pressed down then released while the mouse cursor is inside the button), when the shortcut key is typed, or when click() or animateClick() is called. Notably, this signal is not emitted if you call setDown(), setChecked() or toggle().