QT串口助手(四):数据发送 (2)

If the button is checkable, checked is true if the button is checked, or false if the button is unchecked.

/* 函 数:SendData 描 述:串口发送数据 输 入:无 输 出:无 */ void Widget::SerialSendData(QByteArray baData) { if (baData.isEmpty() != true) { /*是否加回车换行*/ if (ui->AddNewLine_Box->isChecked()) { baData.append("\r\n"); } if (ui->HexSend_checkBox->isChecked()) // hex发送 { /*获取hex格式的数据*/ baData = baData.fromHex(baData); /*发送hex数据*/ serial->write(baData); /*是否显示时间戳*/ if (ui->TimeDisp_checkBox->isChecked()) { QString strdata = baData.toHex(' ').trimmed().toUpper(); ui->Receive_TextEdit->setTextColor(QColor("blue")); ui->Receive_TextEdit->append(QString("[%1]TX -> ").arg(QTime::currentTime().toString("HH:mm:ss:zzz"))); ui->Receive_TextEdit->setTextColor(QColor("black")); ui->Receive_TextEdit->insertPlainText(strdata); } } else //ascii发送 { /*发送ascii数据*/ serial->write(baData); /*是否显示时间戳*/ if (ui->TimeDisp_checkBox->isChecked()) { QString strdata = QString(baData); ui->Receive_TextEdit->setTextColor(QColor("red")); ui->Receive_TextEdit->append(QString("[%1]TX -> ").arg(QTime::currentTime().toString("HH:mm:ss:zzz"))); ui->Receive_TextEdit->setTextColor(QColor("black")); ui->Receive_TextEdit->insertPlainText(strdata); } } //移动光标到末尾 ui->Receive_TextEdit->moveCursor(QTextCursor::End); //更新发送计数 serialDataTotalTxCnt += baData.length(); ui->TxCnt_label->setText(QString::number(serialDataTotalTxCnt)); } else { QMessageBox::warning(this, "警告", "数据为空"); } }

如果勾选了显示时间戳则在每次数据发送后将数据填充到接收框进行显示;代码中对发送的不同格式数据进行了不同颜色的标记;发送后对发送计数框进行更新。

QSerialPort继承自QIODevice,串口发送数据就是使用QIODevice类的write方法:

qint64 QIODevice::write(const QByteArray&byteArray)

This is an overloaded function.

Writes the content of byteArray to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.

QT串口助手(四):数据发送

2.4、定时发送

使用QT中的定时器QTimer按照设定的时间对发送框数据进行自动发送。调用定时器的超时信号来触发槽函数中的发送操作:

/*定时发送定时器*/ TimerSend = new QTimer(this); /*定时器超时信号槽*/ connect(TimerSend, &QTimer::timeout, this, [=](){ SerialSendData(SendTextEditBa); }); [signal]void QTimer::timeout()

This signal is emitted when the timer times out.

Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.

当定时发送被勾选后,会触发QCheckBox的stateChanged信号来设定时间、开启定时器:

/* 函 数:on_TimeSend_checkBox_stateChanged 描 述:定时发送框勾选信号对应槽函数 输 入:无 输 出:无 */ void Widget::on_TimeSend_checkBox_stateChanged(int arg1) { int time; /*判断串口是否打开*/ if (false == isSerialOpen) { if (ui->TimeSend_checkBox->isChecked()) { QMessageBox::information(this, "提示", "串口未打开"); } return; } /*判断是否有数据*/ if (ui->Send_TextEdit->document()->isEmpty() == true) { if (ui->TimeSend_checkBox->isChecked()) { QMessageBox::warning(this, "警告", "数据为空"); } return; } /*判断勾选状态*/ if (arg1 == Qt::Checked) { /*获取设定时间*/ time = ui->TimeSend_lineEdit->text().toInt(); if (time > 0) { TimerSend->start(time); } else { QMessageBox::warning(this, "警告", "时间必须大于0"); } ui->TimeSend_lineEdit->setEnabled(false); } else { /*停止发送*/ TimerSend->stop(); ui->TimeSend_lineEdit->setEnabled(true); } }

[slot]void QTimer::start(int msec)

Starts or restarts the timer with a timeout interval of msec milliseconds.

If the timer is already running, it will be stopped and restarted.

If singleShot is true, the timer will be activated only once.

[slot]void QTimer::stop()

Stops the timer.

三、总结

本章主要讲解数据的发送与格式转换。除了要知道各控件的信号槽应用之外,还应该对QT中数据类型的一些操作方法有所了解。比如上面的代码中使用到的一些QString、QByteArray的常用数据操作方法:QString::replaceQString::atQString::toUtf8QByteArray::toHexQByteArray::fromHex等。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyydsg.html