代码拉取完成,页面将自动刷新
#include "ComAistant.h"
ComAistant::ComAistant(QWidget *parent){
this->setFixedSize(1280,720);
this->setWindowTitle(QString::fromLocal8Bit("串口助手"));
userInterfaceInit();
this->startTimer(1000);
searchSerialPort();
signalConnectSlot();
}
ComAistant::~ComAistant()
{}
//初始化界面
void ComAistant::userInterfaceInit(void)
{
//发送区和接收区
sendArea = new QPlainTextEdit(this);
sendArea->setFixedSize(900, 200);
sendArea->move(30, 450);
recvArea = new QPlainTextEdit(this);
recvArea->setFixedSize(900, 400);
recvArea->move(30, 20);
recvArea->setReadOnly(true);
//发送按钮
sendButton = new QPushButton(QString::fromLocal8Bit("发送"), this);
sendButton->setFixedSize(100, 35);
sendButton->move(700, 660);
//清除接收区按钮
clearRecvAeraButton = new QPushButton(QString::fromLocal8Bit("清空接收区"), this);
clearRecvAeraButton->setFixedSize(100, 35);
clearRecvAeraButton->move(570, 660);
//清除发送区按钮
clearSendAeraButton = new QPushButton(QString::fromLocal8Bit("清空发送区"), this);
clearSendAeraButton->setFixedSize(100, 35);
clearSendAeraButton->move(440, 660);
//打开串口按钮
openButton = new QPushButton(QString::fromLocal8Bit("打开串口"), this);
openButton->setFixedSize(100, 35);
openButton->move(830, 660);
//关闭串口按钮
closeButton = new QPushButton(QString::fromLocal8Bit("关闭串口"), this);
closeButton->setFixedSize(100, 35);
closeButton->move(830, 660);
closeButton->hide();//在未打开串口的时候先隐藏
//串口属性
serialNumLabel = new QLabel(QString::fromLocal8Bit("串口号:"), this);
baudRateLabel = new QLabel(QString::fromLocal8Bit("波特率:"), this);
dataBitsLabel = new QLabel(QString::fromLocal8Bit("数据位:"), this);
parityLabel = new QLabel(QString::fromLocal8Bit("校验位:"), this);
stopBitsLabel = new QLabel(QString::fromLocal8Bit("停止位:"), this);
flowControlLabel = new QLabel(QString::fromLocal8Bit("流控制:"), this);
serialNum = new QComboBox(this);
baudRate = new QComboBox(this);
dataBits = new QComboBox(this);
parity = new QComboBox(this);
stopBits = new QComboBox(this);
flowControl = new QComboBox(this);
QVector<QLabel*> labels;
labels.push_back(serialNumLabel);
labels.push_back(baudRateLabel);
labels.push_back(dataBitsLabel);
labels.push_back(parityLabel);
labels.push_back(stopBitsLabel);
labels.push_back(flowControlLabel);
QVector<QComboBox*> comboboxs;
comboboxs.push_back(serialNum);
comboboxs.push_back(baudRate);
comboboxs.push_back(dataBits);
comboboxs.push_back(parity);
comboboxs.push_back(stopBits);
comboboxs.push_back(flowControl);
for (int i = 0; i < labels.size(); i++) {
labels[i]->setFont(QFont("微软雅黑", 14));
labels[i]->setFixedSize(80, 30);
labels[i]->move(950, 20 + 40 * i);
}
for (int i = 0; i < comboboxs.size(); i++) {
comboboxs[i]->setFixedSize(150, 30);
comboboxs[i]->move(1030,20 + 40 * i);
}
//发送模式
sendModeLabel = new QLabel(QString::fromLocal8Bit("发送模式:"), this);
sendModeLabel->setFont(QFont("微软雅黑", 14));
sendModeLabel->setFixedSize(100, 30);
sendModeLabel->move(950, 260);
sendMode_ASCII = new QCheckBox(QString::fromLocal8Bit("ASCII"), this);
sendMode_ASCII->setFont(QFont("微软雅黑", 14));
sendMode_ASCII->move(1060, 260);
sendMode_HEX = new QCheckBox(QString::fromLocal8Bit("HEX"), this);
sendMode_HEX->setFont(QFont("微软雅黑", 14));
sendMode_HEX->move(1060, 300);
sendMode_ASCII->setChecked(true);//默认为ASCII发送模式
//接收模式
recvModeLabel = new QLabel(QString::fromLocal8Bit("接收模式:"), this);
recvModeLabel->setFont(QFont("微软雅黑", 14));
recvModeLabel->setFixedSize(100, 30);
recvModeLabel->move(950, 340);
recvMode_ASCII = new QCheckBox(QString::fromLocal8Bit("ASCII"), this);
recvMode_ASCII->setFont(QFont("微软雅黑", 14));
recvMode_ASCII->move(1060, 340);
recvMode_HEX = new QCheckBox(QString::fromLocal8Bit("HEX"), this);
recvMode_HEX->setFont(QFont("微软雅黑", 14));
recvMode_HEX->move(1060, 380);
recvMode_ASCII->setChecked(true);//默认为ASCII接收模式
//添加波特率
QList<QString> baudRateList = { "9600","19200","57600","115200","230400"};
for (int i = 0; i < baudRateList.size(); i++) {
baudRate->addItem(baudRateList[i]);
}
baudRate->setFont(QFont("微软雅黑", 12));
//添加数据位
QList<QString> dataBitsList = { "5","6","7","8"};
for (int i = 0; i < dataBitsList.size(); i++) {
dataBits->addItem(dataBitsList[i]);
}
dataBits->setFont(QFont("微软雅黑", 12));
//添加校验位
QList<QString> parityList = { "NONE","ODD","EVEN"};
for (int i = 0; i < parityList.size(); i++) {
parity->addItem(parityList[i]);
}
parity->setFont(QFont("微软雅黑", 12));
//添加停止位
QList<QString> stopBitsList = { "1","1.5","2"};
for (int i = 0; i < stopBitsList.size(); i++) {
stopBits->addItem(stopBitsList[i]);
}
stopBits->setFont(QFont("微软雅黑", 12));
//添加流控制
QList<QString> flowControlList = { "NONE","XON/XOFF","RTS/CTS"};
for (int i = 0; i < flowControlList.size(); i++) {
flowControl->addItem(flowControlList[i]);
}
flowControl->setFont(QFont("微软雅黑", 12));
}
//信号与槽函数的连接
void ComAistant::signalConnectSlot(void)
{
connect(clearSendAeraButton, &QPushButton::clicked, this, &ComAistant::clearSendArea);
connect(clearRecvAeraButton, &QPushButton::clicked, this, &ComAistant::clearRecvArea);
connect(openButton, &QPushButton::clicked, this, &ComAistant::openSerialPort);
connect(closeButton, &QPushButton::clicked, this, &ComAistant::closeSerialPort);
connect(sendMode_ASCII, &QCheckBox::stateChanged, this, &ComAistant::sendModeStateChanged);
connect(sendMode_HEX, &QCheckBox::stateChanged, this, &ComAistant::sendModeStateChanged);
connect(recvMode_ASCII, &QCheckBox::stateChanged, this, &ComAistant::recvModeStateChanged);
connect(recvMode_HEX, &QCheckBox::stateChanged, this, &ComAistant::recvModeStateChanged);
connect(sendButton, &QPushButton::clicked, this, &ComAistant::sendData);
}
//清空发送区
void ComAistant::clearSendArea(void)
{
sendArea->clear();
}
//清空接收区
void ComAistant::clearRecvArea(void)
{
recvArea->clear();
}
//打开串口
void ComAistant::openSerialPort(void)
{
openButton->hide();
closeButton->show();
setupSerialPort();
}
//关闭串口
void ComAistant::closeSerialPort(void)
{
closeButton->hide();
openButton->show();
serialPort->close();
}
//发送模式改变
void ComAistant::sendModeStateChanged(int state)
{
QCheckBox* currentCheckBox = qobject_cast<QCheckBox*>(sender());
if (currentCheckBox == sendMode_ASCII) {
if (sendMode_ASCII->isChecked()) {
sendMode_HEX->setEnabled(true);
if (sendMode_HEX->isChecked()) {
sendMode_HEX->setChecked(false);
sendMode_ASCII->setChecked(true);
}
else {
sendMode_HEX->setChecked(false);
sendMode_ASCII->setChecked(true);
}
}
else {
if (!sendMode_ASCII->isChecked()) {
if (!sendMode_HEX->isChecked()) {
sendMode_ASCII->setChecked(true);
sendMode_ASCII->setEnabled(false);
}
}
}
}
else if (currentCheckBox == sendMode_HEX) {
if (sendMode_HEX->isChecked()) {
sendMode_ASCII->setEnabled(true);
if (sendMode_ASCII->isChecked()) {
sendMode_ASCII->setChecked(false);
sendMode_HEX->setChecked(true);
}
else {
sendMode_ASCII->setChecked(false);
sendMode_HEX->setChecked(true);
}
}
else {
if (!sendMode_HEX->isChecked()) {
if (!sendMode_ASCII->isChecked()) {
sendMode_HEX->setChecked(true);
sendMode_HEX->setEnabled(false);
}
}
}
}
}
//接收模式改变
void ComAistant::recvModeStateChanged(int state)
{
QCheckBox* currentCheckBox = qobject_cast<QCheckBox*>(sender());
if (currentCheckBox == recvMode_ASCII) {
if (recvMode_ASCII->isChecked()) {
recvMode_HEX->setEnabled(true);
if (recvMode_HEX->isChecked()) {
recvMode_HEX->setChecked(false);
recvMode_ASCII->setChecked(true);
}
else {
recvMode_HEX->setChecked(false);
recvMode_ASCII->setChecked(true);
}
}
else {
if (!recvMode_ASCII->isChecked()) {
if (!recvMode_HEX->isChecked()) {
recvMode_ASCII->setChecked(true);
recvMode_ASCII->setEnabled(false);
}
}
}
}
else if (currentCheckBox == recvMode_HEX) {
if (recvMode_HEX->isChecked()) {
recvMode_ASCII->setEnabled(true);
if (recvMode_ASCII->isChecked()) {
recvMode_ASCII->setChecked(false);
recvMode_HEX->setChecked(true);
}
else {
recvMode_ASCII->setChecked(false);
recvMode_HEX->setChecked(true);
}
}
else {
if (!recvMode_HEX->isChecked()) {
if (!recvMode_ASCII->isChecked()) {
recvMode_HEX->setChecked(true);
recvMode_HEX->setEnabled(false);
}
}
}
}
}
//配置串口
void ComAistant::setupSerialPort(void)
{
QSerialPort::BaudRate baud{};
QSerialPort::DataBits data{};
QSerialPort::Parity pari{};
QSerialPort::StopBits stop{};
QSerialPort::FlowControl flow{};
//波特率
if (baudRate->currentText() == "9600") baud = QSerialPort::Baud9600;
else if (baudRate->currentText() == "19200") baud = QSerialPort::Baud19200;
else if (baudRate->currentText() == "57600") baud = QSerialPort::Baud57600;
else if (baudRate->currentText() == "115200") baud = QSerialPort::Baud115200;
//数据位
if (dataBits->currentText() == "5") data = QSerialPort::Data5;
else if (dataBits->currentText() == "6") data = QSerialPort::Data6;
else if (dataBits->currentText() == "7") data = QSerialPort::Data7;
else if (dataBits->currentText() == "8") data = QSerialPort::Data8;
//校验位
if(parity->currentText() == "NONE") pari = QSerialPort::NoParity;
else if(parity->currentText() == "ODD") pari = QSerialPort::OddParity;
else if(parity->currentText() == "EVEN") pari = QSerialPort::EvenParity;
//停止位
if (stopBits->currentText() == "1") stop = QSerialPort::OneStop;
else if (stopBits->currentText() == "1.5") stop = QSerialPort::OneAndHalfStop;
else if (stopBits->currentText() == "2") stop = QSerialPort::TwoStop;
//流控制
if (flowControl->currentText() == "NONE") flow = QSerialPort::NoFlowControl;
else if (flowControl->currentText() == "XON/XOFF") flow = QSerialPort::SoftwareControl;
else if (flowControl->currentText() == "RTS/CTS") flow = QSerialPort::HardwareControl;
serialPort = new QSerialPort(this);
serialPort->setBaudRate(baud);
serialPort->setDataBits(data);
serialPort->setParity(pari);
serialPort->setStopBits(stop);
serialPort->setFlowControl(flow);
serialPort->setPortName(serialNum->currentText());
if (serialPort->open(QIODevice::ReadWrite)) {
//控件失效
connect(serialPort, &QSerialPort::readyRead, [&]() {
auto data = serialPort->readAll();
if (recvMode_HEX->isChecked()) {
QString hex = data.toHex(' ').toUpper();
recvArea->appendPlainText(hex);
}
else if(recvMode_ASCII->isChecked()) {
QString str = QString(data).toLocal8Bit();
recvArea->appendPlainText(str);
}
});
}
else {
//qDebug() << QString::fromLocal8Bit("串口打开失败");
QMessageBox::critical(this, QString::fromLocal8Bit("打开失败"), QString::fromLocal8Bit("请确认串口是否连接"));
openButton->show();
closeButton->hide();
return;
}
}
//搜索串口
void ComAistant::searchSerialPort(void)
{
foreach(const QSerialPortInfo& info, QSerialPortInfo::availablePorts()) {
serialNum->addItem(info.portName());
}
serialNum->setFont(QFont("微软雅黑", 12));
}
//定时查询串口
void ComAistant::timerEvent(QTimerEvent* e)
{
QList<QString> portList;
static int count = 0;
foreach(const QSerialPortInfo& info, QSerialPortInfo::availablePorts()) {
portList << info.portName();
}
if (count != portList.size()) {
count = portList.size();
serialNum->clear();
serialNum->addItems(portList);
}
}
//发送数据
void ComAistant::sendData(void)
{
QString data = sendArea->toPlainText();
if (sendMode_HEX->isChecked()) {
//HEX
QByteArray arr;
for (int i = 0; i < data.size(); i++) {
if (data[i] == ' ') continue;
int num = data.mid(i, 2).toUInt(nullptr, 16);
i++;
arr.append(num);
}
serialPort->write(arr);
}
else {
//ASCII
serialPort->write(data.toLocal8Bit().data());
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。