first version

This commit is contained in:
168492376 2025-01-13 23:00:58 +08:00
commit fdc93e494e
16 changed files with 648 additions and 0 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "3rdparty/CSerialPort"]
path = 3rdparty/CSerialPort
url = https://github.com/itas109/CSerialPort.git

1
3rdparty/CSerialPort vendored Submodule

@ -0,0 +1 @@
Subproject commit c3928be6d674bd3bb6df2c81b2486bcd4b82304b

BIN
images/left_arrow_gray.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B

BIN
images/left_arrow_green.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

BIN
images/right_arrow_gray.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 673 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 659 B

8
img.qrc Normal file
View File

@ -0,0 +1,8 @@
<RCC>
<qresource prefix="/images">
<file>images/right_arrow_green.png</file>
<file>images/left_arrow_green.png</file>
<file>images/right_arrow_gray.png</file>
<file>images/left_arrow_gray.png</file>
</qresource>
</RCC>

BIN
logo.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

13
main.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

22
mainwindow.cpp Normal file
View File

@ -0,0 +1,22 @@

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "serialConfigWidget.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("串口转发工具V1.0");
QWidget *wdg = new SerialConfigWidget();
ui->verticalLayout->addWidget(wdg);
}
MainWindow::~MainWindow()
{
delete ui;
}

26
mainwindow.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

42
mainwindow.ui Normal file
View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>555</width>
<height>500</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout"/>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<resources/>
<connections/>
</ui>

169
serialConfigWidget.cpp Normal file
View File

@ -0,0 +1,169 @@
#include "serialConfigWidget.h"
#include "ui_serialConfigWidget.h"
#include <QSerialPortInfo>
#include <QMessageBox>
#include <QByteArray>
#include <QThread>
#include <QMutexLocker>
#include <QDebug>
SerialConfigWidget::SerialConfigWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::SerialConfigWidget)
{
ui->setupUi(this);
m_SerialPort1.connectReadEvent(this);
m_SerialPort2.connectReadEvent(this);
refreshCom();
connect(this,SIGNAL(displayTextSignal(QString)),this,SLOT(displayText(QString)),Qt::QueuedConnection);
}
SerialConfigWidget::~SerialConfigWidget()
{
delete ui;
}
void SerialConfigWidget::refreshCom()
{
QString currentTxt1 = ui->cmbCom1->currentText();
QString currentTxt2 = ui->cmbCom2->currentText();
QStringList nameList;
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
nameList.append(info.portName());
}
std::sort(nameList.begin(), nameList.end(), [&](QString &s1, QString &s2){
if(s1.size()==s2.size()){
return s1<s2;
}else{
return s1.size()<s2.size();
} });
ui->cmbCom1->clear();
ui->cmbCom1->addItems(nameList);
if(!currentTxt1.isEmpty()){
ui->cmbCom1->setCurrentText(currentTxt1);
}
ui->cmbCom2->clear();
ui->cmbCom2->addItems(nameList);
if(!currentTxt2.isEmpty()){
ui->cmbCom2->setCurrentText(currentTxt2);
}
}
void SerialConfigWidget::on_toolBtnRefresh_clicked()
{
refreshCom();
}
void SerialConfigWidget::on_toolBtnOk_clicked()
{
if(ui->toolBtnOk->isChecked()){
QString portName1 = ui->cmbCom1->currentText();
QString baud1 = ui->cmbBaud1->currentText();
m_SerialPort1.init(portName1.toStdString().c_str(),baud1.toInt(),
itas109::Parity::ParityNone,itas109::DataBits::DataBits8,
itas109::StopBits::StopOne);
m_SerialPort1.setReadIntervalTimeout(readIntervalTimeoutMS);
if(!m_SerialPort1.open()){
QMessageBox::warning(NULL,tr("警告"),portName1 + tr("打开错误") + QString("\n\ncode: %1\nmessage: %2").arg(
m_SerialPort1.getLastError()).arg(m_SerialPort1.getLastErrorMsg()));
ui->toolBtnOk->setChecked(false);
return ;
}
QString portName2 = ui->cmbCom2->currentText();
QString baud2 = ui->cmbBaud2->currentText();
m_SerialPort2.init(portName2.toStdString().c_str(),baud2.toInt(),
itas109::Parity::ParityNone,itas109::DataBits::DataBits8,
itas109::StopBits::StopOne);
m_SerialPort2.setReadIntervalTimeout(readIntervalTimeoutMS);
if(!m_SerialPort2.open()){
QMessageBox::warning(NULL,tr("警告"),portName2 + tr("打开错误") + QString("\n\ncode: %1\nmessage: %2").arg(
m_SerialPort2.getLastError()).arg(m_SerialPort2.getLastErrorMsg()));
m_SerialPort1.close();
ui->toolBtnOk->setChecked(false);
return ;
}
ui->toolBtnOk->setStyleSheet("color:green");
ui->cmbCom1->setDisabled(true);
ui->cmbBaud1->setDisabled(true);
ui->cmbCom2->setDisabled(true);
ui->cmbBaud2->setDisabled(true);
ui->toolBtnRefresh->setDisabled(true);
ui->toolBtnOk->setChecked(true);
}else{
m_SerialPort1.close();
m_SerialPort2.close();
ui->toolBtnOk->setStyleSheet("color:balck");
ui->cmbCom1->setEnabled(true);
ui->cmbBaud1->setEnabled(true);
ui->cmbCom2->setEnabled(true);
ui->cmbBaud2->setEnabled(true);
ui->toolBtnRefresh->setEnabled(true);
ui->toolBtnOk->setChecked(false);
}
}
void SerialConfigWidget::displayText(const QString &txt)
{
ui->plainTextEdit->appendPlainText(txt.trimmed());
}
void SerialConfigWidget::onReadEvent(const char *portName, unsigned int readBufferLen)
{
QMutexLocker locker(&mtx);
if(portName && std::strcmp(portName,m_SerialPort1.getPortName())==0){
int recLen = 0;
char * str = NULL;
str = new char[readBufferLen];
if(str){
recLen = m_SerialPort1.readData(str, readBufferLen);
}
if(str && recLen > 0)
{
// TODO: 中文需要由两个字符拼接,否则显示为空""
//QString m_str = QString::fromLocal8Bit(str,recLen);
//emitUpdateReceive(m_str);
m_SerialPort2.writeData(str,recLen);
QString txt = QString("%1:%2").arg(QString::fromLocal8Bit(portName,std::strlen(portName))).arg(QString::fromLocal8Bit(str,recLen));
emit displayTextSignal(txt);
}
if(str)
{
delete[] str;
str = NULL;
}
}else if(portName && std::strcmp(portName,m_SerialPort2.getPortName())==0){
int recLen = 0;
char * str = NULL;
str = new char[readBufferLen];
if(str){
recLen = m_SerialPort2.readData(str, readBufferLen);
}
if(str && recLen > 0)
{
// TODO: 中文需要由两个字符拼接,否则显示为空""
//QString m_str = QString::fromLocal8Bit(str,recLen);
//emitUpdateReceive(m_str);
m_SerialPort1.writeData(str,recLen);
QString txt = QString("%1:%2").arg(QString::fromLocal8Bit(portName,std::strlen(portName))).arg(QString::fromLocal8Bit(str,recLen));
emit displayTextSignal(txt);
}
if(str)
{
delete[] str;
str = NULL;
}
}
}

41
serialConfigWidget.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef SERIALCONFIGWIDGET_H
#define SERIALCONFIGWIDGET_H
#include <QWidget>
#include <QMutex>
#include "CSerialPort/SerialPort.h"
using namespace itas109;
namespace Ui {
class SerialConfigWidget;
}
class SerialConfigWidget : public QWidget ,public CSerialPortListener
{
Q_OBJECT
public:
explicit SerialConfigWidget(QWidget *parent = nullptr);
~SerialConfigWidget();
signals:
void displayTextSignal(const QString &txt);
private slots:
void on_toolBtnRefresh_clicked();
void on_toolBtnOk_clicked();
void displayText(const QString &txt);
private slots:
void onReadEvent(const char *portName, unsigned int readBufferLen) override;
private:
Ui::SerialConfigWidget *ui;
CSerialPort m_SerialPort1,m_SerialPort2;
int readIntervalTimeoutMS = 50;
QMutex mtx;
void refreshCom();
};
#endif // SERIALCONFIGWIDGET_H

284
serialConfigWidget.ui Normal file
View File

@ -0,0 +1,284 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SerialConfigWidget</class>
<widget class="QWidget" name="SerialConfigWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>702</width>
<height>374</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout" columnstretch="1,0,1,0">
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>4</number>
</property>
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<number>4</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="2">
<widget class="QGroupBox" name="groupBox_2">
<property name="styleSheet">
<string notr="true">QGroupBox{border:none}</string>
</property>
<property name="title">
<string/>
</property>
<property name="flat">
<bool>true</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>串口号:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cmbCom2"/>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>波特率:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cmbBaud2">
<property name="editable">
<bool>true</bool>
</property>
<item>
<property name="text">
<string>115200</string>
</property>
</item>
<item>
<property name="text">
<string>230400</string>
</property>
</item>
<item>
<property name="text">
<string>460800</string>
</property>
</item>
<item>
<property name="text">
<string>921600</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="styleSheet">
<string notr="true">QGroupBox{border:none}</string>
</property>
<property name="title">
<string/>
</property>
<property name="flat">
<bool>true</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>串口号:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cmbCom1"/>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>波特率:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cmbBaud1">
<property name="editable">
<bool>true</bool>
</property>
<item>
<property name="text">
<string>115200</string>
</property>
</item>
<item>
<property name="text">
<string>230400</string>
</property>
</item>
<item>
<property name="text">
<string>460800</string>
</property>
</item>
<item>
<property name="text">
<string>921600</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="3">
<widget class="QGroupBox" name="groupBox_4">
<property name="styleSheet">
<string notr="true">QGroupBox{border:none}</string>
</property>
<property name="title">
<string/>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QToolButton" name="toolBtnRefresh">
<property name="text">
<string>刷新</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolBtnOk">
<property name="text">
<string>确定</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QGroupBox" name="groupBox_3">
<property name="styleSheet">
<string notr="true">QGroupBox{border:none}</string>
</property>
<property name="title">
<string/>
</property>
<property name="flat">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="lbArrow1">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>40</width>
<height>12</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>40</width>
<height>12</height>
</size>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="img.qrc">:/images/images/left_arrow_gray.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="lbArrow2">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>40</width>
<height>12</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>40</width>
<height>12</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="img.qrc">:/images/images/right_arrow_gray.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0" colspan="4">
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="maximumBlockCount">
<number>10000</number>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="img.qrc"/>
</resources>
<connections/>
</ui>

39
serialTransferTool.pro Normal file
View File

@ -0,0 +1,39 @@
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
msvc{
QMAKE_CFLAGS += /utf-8
QMAKE_CXXFLAGS += /utf-8
}
include($$PWD/3rdparty/CSerialPort/examples/CommQT/commqt.pri)
SOURCES += \
main.cpp \
mainwindow.cpp \
serialConfigWidget.cpp
HEADERS += \
mainwindow.h \
serialConfigWidget.h
FORMS += \
mainwindow.ui \
serialConfigWidget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
img.qrc
RC_ICONS = logo.ico