1. 在简单串口协议实验基础上进行改进,完善上位机功能,主要工作为,增加区分发送文本和HEX数值的功能,增加接收区区分文本和HEX数值的功能,增加清空发送区和接收区功能上位机改为c++版本,写出代码
时间: 2023-07-31 19:07:55 浏览: 101
以下是一个简单的C++串口通信程序,实现了发送文本和HEX数值、接收区区分文本和HEX数值、清空发送区和接收区功能。需要注意的是,该程序仅供参考,具体实现方式会因硬件平台和串口驱动的不同而有所差异。同时,该程序使用Qt框架,需要在编译前安装Qt库。
```c++
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QMessageBox>
#include <QTextCodec>
#include <QDebug>
class SerialPort : public QObject
{
Q_OBJECT
public:
SerialPort();
~SerialPort();
private slots:
void readData();
void sendData(QString data);
void on_btnOpen_clicked();
void on_btnSend_clicked();
void on_btnClearSend_clicked();
void on_btnClearRecv_clicked();
private:
QSerialPort *m_serialPort;
QTextCodec *m_codec;
};
SerialPort::SerialPort()
{
m_serialPort = new QSerialPort(this);
m_codec = QTextCodec::codecForName("UTF-8");
connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(readData()));
}
SerialPort::~SerialPort()
{
if (m_serialPort->isOpen()) {
m_serialPort->close();
}
delete m_serialPort;
delete m_codec;
}
void SerialPort::readData()
{
QByteArray data = m_serialPort->readAll();
if (ui->rbRecvHex->isChecked()) {
QString hexData = QString(data.toHex());
ui->teRecv->insertPlainText(hexData);
} else {
QString textData = m_codec->toUnicode(data);
ui->teRecv->insertPlainText(textData);
}
}
void SerialPort::sendData(QString data)
{
if (m_serialPort->isOpen()) {
if (ui->rbSendHex->isChecked()) {
QByteArray hexData = QByteArray::fromHex(data.toUtf8());
m_serialPort->write(hexData);
} else {
QByteArray textData = m_codec->fromUnicode(data);
m_serialPort->write(textData);
}
}
}
void SerialPort::on_btnOpen_clicked()
{
if (m_serialPort->isOpen()) {
m_serialPort->close();
ui->btnOpen->setText(tr("打开串口"));
} else {
m_serialPort->setPortName(ui->cbPort->currentText());
m_serialPort->setBaudRate(ui->cbBaud->currentText().toInt());
m_serialPort->setDataBits(QSerialPort::Data8);
m_serialPort->setParity(QSerialPort::NoParity);
m_serialPort->setStopBits(QSerialPort::OneStop);
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);
if (m_serialPort->open(QIODevice::ReadWrite)) {
ui->btnOpen->setText(tr("关闭串口"));
} else {
QMessageBox::warning(this, tr("警告"), tr("打开串口失败!"));
}
}
}
void SerialPort::on_btnSend_clicked()
{
QString data = ui->teSend->toPlainText();
sendData(data);
}
void SerialPort::on_btnClearSend_clicked()
{
ui->teSend->clear();
}
void SerialPort::on_btnClearRecv_clicked()
{
ui->teRecv->clear();
}
```
在上述代码中,readData()函数用于读取串口数据,并根据接收区选项来判断是否需要进行HEX编码;sendData()函数用于发送数据,并根据发送区选项来判断是否需要进行HEX解码;on_btnOpen_clicked()函数用于打开或关闭串口;on_btnSend_clicked()函数用于发送数据;on_btnClearSend_clicked()函数用于清空发送区;on_btnClearRecv_clicked()函数用于清空接收区。可以根据自己的需求进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)