1 Star 0 Fork 0

zthuai/gps串口助手

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mainwindow.cpp 7.37 KB
一键复制 编辑 原始数据 按行查看 历史
zthuai 提交于 2021-06-20 19:44 . 基本功能
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QList>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->m_serialPort = nullptr;
this->m_timer = new QTimer(this);
// 查找可用串口
this->serialPortDetect();
}
MainWindow::~MainWindow()
{
delete ui;
}
// ------------------------------------------------
// 初始化并打开串口
void MainWindow::serialPortInit()
{
if (this->m_serialPort == nullptr)
{
this->m_serialPort = new QSerialPort(this);
// ---------------初始化串口
this->m_serialPort->setPortName(ui->serialName_comboBox->currentText());
this->setSerialPortBaud();
this->setSerialPortDataBits();
this->setSerialPortParity();
this->setSerialPortStopBits();
// ---------------打开串口
if (!m_serialPort->open(QIODevice::ReadWrite))
{
delete m_serialPort;
this->m_serialPort = nullptr;
ui->open2close_Btn->setText("Open");
ui->open2close_Btn->setStyleSheet("background-color:rgb(255,184,61)");
QMessageBox::information(this, "error", "open faild!");
}
else
{
// 打开成功后,自动开始接收数据
ui->open2close_Btn->setText("Close");
connect(this->m_serialPort, &QSerialPort::readyRead, this, &MainWindow::serialPortReceive);
}
}
else
{
m_serialPort->close();
delete m_serialPort;
m_serialPort = nullptr;
ui->open2close_Btn->setText("Open");
}
}
void MainWindow::serialPortDetect()
{
this->m_detectFlg = true;
QList<QSerialPortInfo> serialPortList(QSerialPortInfo::availablePorts());
QSerialPort tmpSerial;
if (serialPortList.isEmpty())
{
QMessageBox::information(this, "error", "cannot find com!");
}
ui->serialName_comboBox->clear();
for (int i = 0; i < serialPortList.size(); ++i)
{
tmpSerial.setPort(serialPortList.at(i));
ui->serialName_comboBox->addItem(serialPortList.at(i).portName());
tmpSerial.close();
}
this->m_detectFlg = false;
}
// ------------------------------------------------
void MainWindow::serialPortReceive()
{
QByteArray tmp(this->m_serialPort->readAll());
QString tmpStr;
// 不以Hex显示
if (ui->hexShow_checkBox->checkState() == Qt::Unchecked)
{
tmpStr = QString::fromLocal8Bit(tmp);
// tmpStr = QString(tmp);
}
// Hex显示
else
{
tmpStr=tmp.toHex();
}
if (!tmp.isEmpty())
{
ui->Recv_textEdit->moveCursor(QTextCursor::End);
ui->Recv_textEdit->insertPlainText(tmpStr.toUpper()); // 转成大写显示
}
this->m_RecvNum += tmp.size();
ui->RecvNum_label->setText(QString::number(this->m_RecvNum));
tmp.clear();
tmpStr.clear();
}
void MainWindow::serialPortSend()
{
if (ui->hexSend_checkBox->checkState() == Qt::Unchecked)
{
this->m_serialPort->write(ui->Send_textEdit->toPlainText().toLocal8Bit());
this->m_sendNum += ui->Send_textEdit->toPlainText().toLocal8Bit().size();
}
else
{
this->m_sendNum += this->m_serialPort->write(
QByteArray::fromHex(ui->Send_textEdit->toPlainText().toLocal8Bit())
);
}
if (ui->sendNewLine_checkBox->isChecked())
{
this->m_sendNum += this->m_serialPort->write("\r\n", 2);
}
ui->SendNum_label->setText(QString::number(this->m_sendNum));
}
// ------------------------------------------------
void MainWindow::setSerialPortBaud()
{
if (this->m_serialPort != nullptr)
{
this->m_serialPort->setBaudRate(ui->baud_comboBox->currentText().toInt());
}
}
void MainWindow::setSerialPortDataBits()
{
if (this->m_serialPort != nullptr)
{
switch (ui->dataBits_comboBox->currentText().toInt())
{
case 8:
this->m_serialPort->setDataBits(QSerialPort::Data8);
break;
default:
break;
}
}
}
void MainWindow::setSerialPortParity()
{
if (this->m_serialPort != nullptr)
{
switch (ui->parity_comboBox->currentIndex())
{
case 0:
this->m_serialPort->setParity(QSerialPort::NoParity);
break;
default:
break;
}
}
}
void MainWindow::setSerialPortStopBits()
{
if (this->m_serialPort != nullptr)
{
switch (ui->stopBits_comboBox->currentIndex())
{
case 0:
this->m_serialPort->setStopBits(QSerialPort::OneStop);
break;
default:
break;
}
}
}
// ------------------------------------------------
void MainWindow::on_open2close_Btn_clicked()
{
this->serialPortInit();
}
void MainWindow::on_clearRecv_Btn_clicked()
{
ui->Recv_textEdit->clear();
ui->RecvNum_label->setNum(0);
}
void MainWindow::on_clearSend_Btn_clicked()
{
ui->Send_textEdit->clear();
ui->SendNum_label->setNum(0);
}
void MainWindow::on_send_Btn_clicked()
{
if (this->m_serialPort != nullptr)
{
this->serialPortSend();
}
}
void MainWindow::on_serialName_comboBox_currentIndexChanged(const QString &)
{
if (this->m_serialPort && !this->m_detectFlg)
{
this->m_serialPort->close();
delete m_serialPort;
this->m_serialPort = nullptr;
this->serialPortInit();
}
}
void MainWindow::on_baud_comboBox_currentIndexChanged(const QString &arg1)
{
if (this->m_serialPort)
{
this->m_serialPort->setBaudRate(arg1.toInt());
}
}
void MainWindow::on_dataBits_comboBox_currentIndexChanged(int index)
{
if (this->m_serialPort)
{
switch (index)
{
case 0:
this->m_serialPort->setDataBits(QSerialPort::Data8);
break;
default:
break;
}
}
}
void MainWindow::on_stopBits_comboBox_currentIndexChanged(int index)
{
if (this->m_serialPort)
{
switch (index)
{
case 0:
this->m_serialPort->setStopBits(QSerialPort::OneStop);
break;
default:
break;
}
}
}
void MainWindow::on_parity_comboBox_currentIndexChanged(int index)
{
if (this->m_serialPort)
{
switch (index)
{
case 0:
this->m_serialPort->setParity(QSerialPort::NoParity);
break;
default:
break;
}
}
}
void MainWindow::on_timerSend_checkBox_stateChanged(int arg1)
{
if (this->m_serialPort)
{
if (arg1 == Qt::Checked)
{
this->m_timer->start(ui->setTimer_lineEdit->text().toInt());
}
if (arg1 == Qt::Unchecked)
{
this->m_timer->stop();
}
}
}
void MainWindow::on_serialName_comboBox_activated(const QString &)
{
this->serialPortDetect();
if (this->m_serialPort != nullptr)
{
this->serialPortInit();
}
}
void MainWindow::on_baud_comboBox_activated(const QString &)
{
this->setSerialPortBaud();
}
void MainWindow::on_dataBits_comboBox_activated(const QString &)
{
this->setSerialPortDataBits();
}
void MainWindow::on_stopBits_comboBox_activated(const QString &)
{
this->setSerialPortStopBits();
}
void MainWindow::on_parity_comboBox_activated(const QString &)
{
this->setSerialPortParity();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zthuai/gps-serial-port-assistant.git
git@gitee.com:zthuai/gps-serial-port-assistant.git
zthuai
gps-serial-port-assistant
gps串口助手
master

搜索帮助