add multi serial transfer v1.2

This commit is contained in:
168492376 2025-05-02 02:34:00 +00:00
parent fdc93e494e
commit cc5008c2f0
25 changed files with 1960 additions and 607 deletions

30
baseConnection.cpp Normal file
View File

@ -0,0 +1,30 @@

#include "baseConnection.h"
BaseConnection::BaseConnection(QObject *parent)
: QObject{parent}
{
}
void BaseConnection::setting(QMap<QString, QString> &map)
{
paras = map;
}
QString BaseConnection::getProtocol()
{
return protocol;
}
QMap<QString, QString> BaseConnection::getParas()
{
return paras;
}
QString BaseConnection::getConnectionName()
{
return connectionName;
}

34
baseConnection.h Normal file
View File

@ -0,0 +1,34 @@

#ifndef BASECONNECTION_H
#define BASECONNECTION_H
#include <QObject>
#include <QMap>
class BaseConnection : public QObject
{
Q_OBJECT
public:
explicit BaseConnection(QObject *parent = nullptr);
QString getProtocol();
QMap<QString,QString> getParas();
QString getConnectionName();
virtual void setting(QMap<QString,QString> &map);
virtual bool isConnected() = 0;
virtual bool open() = 0;
virtual bool close() = 0;
virtual int sendData(const QByteArray &data) = 0;
signals:
void recvDataSignal(const QByteArray &data);
void recvUserSendDataSignal(const QByteArray &data);
void connectionStatusChangeSignal(bool bconnected);
protected:
QString protocol;
QMap<QString,QString> paras;
QString connectionName;
};
#endif // BASECONNECTION_H

92
connectionManager.cpp Normal file
View File

@ -0,0 +1,92 @@

#include "connectionManager.h"
#include <QDebug>
ConnectionManager::ConnectionManager(QObject *parent): QObject{parent}
{
}
ConnectionManager::~ConnectionManager()
{
}
void ConnectionManager::insert(std::shared_ptr<BaseConnection> conn)
{
if(conn){
bool same = false;
for(int i=0;i<connectionVec.size();i++){
if(conn==connectionVec[i]){
same = true;
break;
}
}
if(!same){
connectionVec.append(conn);
emit connectionAddedSignal(conn);
}else{
emit connectionChangeSignal(conn);
}
}
}
void ConnectionManager::remove(int idx)
{
if(idx>=0 && idx<=connectionVec.length()){
if(connectionVec[idx]->isConnected()){
connectionVec[idx]->close();
}
QString name = connectionVec[idx]->getConnectionName();
connectionVec[idx]->disconnect();
std::shared_ptr<BaseConnection> connPtr = connectionVec[idx];
connectionVec.remove(idx);
emit connectionRemovedSignal(connPtr);
}
}
void ConnectionManager::remove(std::shared_ptr<BaseConnection> conn)
{
int idx = -1;
for(int i=0;i<connectionVec.size();i++){
if(connectionVec[i] == conn){
if(conn->isConnected()){
conn->close();
}
idx = i;
break;
}
}
if(idx>=0){
QString name = connectionVec[idx]->getConnectionName();
connectionVec[idx]->disconnect();
connectionVec.remove(idx);
emit connectionRemovedSignal(conn);
}
}
int ConnectionManager::getConnectionCounts()
{
return connectionVec.length();
}
std::shared_ptr<BaseConnection> ConnectionManager::getConnection(int idx)
{
std::shared_ptr<BaseConnection> conn = nullptr;
if(idx>=0 && idx<=connectionVec.length()){
conn = connectionVec[idx];
}
return conn;
}
std::shared_ptr<BaseConnection> ConnectionManager::getConnection(const QString &connectionName)
{
std::shared_ptr<BaseConnection> conn = nullptr;
for(int i=0;i<connectionVec.size();i++){
if(connectionName==connectionVec[i]->getConnectionName()){
conn = connectionVec[i];
}
}
return conn;
}

37
connectionManager.h Normal file
View File

@ -0,0 +1,37 @@

#ifndef CONNECTIONMANAGER_H
#define CONNECTIONMANAGER_H
#include <QObject>
#include <QVector>
#include "baseConnection.h"
#include <memory>
class ConnectionManager:public QObject
{
Q_OBJECT
public:
static ConnectionManager& getInstance()
{
static ConnectionManager instance;
return instance;
}
void insert(std::shared_ptr<BaseConnection> conn);
void remove(int idx);
void remove(std::shared_ptr<BaseConnection> conn);
int getConnectionCounts();
std::shared_ptr<BaseConnection> getConnection(int idx);
std::shared_ptr<BaseConnection> getConnection(const QString &connectionName);
signals:
void connectionAddedSignal(std::shared_ptr<BaseConnection> connPtr);
void connectionRemovedSignal(std::shared_ptr<BaseConnection> connPtr);
void connectionChangeSignal(std::shared_ptr<BaseConnection> connPtr);
private:
ConnectionManager(QObject *parent = nullptr);
~ConnectionManager();
QVector<std::shared_ptr<BaseConnection>> connectionVec;
};
#endif // CONNECTIONMANAGER_H

157
connectionSettingWidget.cpp Normal file
View File

@ -0,0 +1,157 @@
#include "connectionSettingWidget.h"
#include "ui_connectionSettingWidget.h"
#include "connectionManager.h"
#include <QSerialPortInfo>
#include <QDebug>
ConnectionSettingWidget::ConnectionSettingWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ConnectionSettingWidget)
{
ui->setupUi(this);
refreshComList();
ui->toolBtnConnectSerial->setText(tr("连接"));
ui->toolBtnConnectTcp->setText(tr("连接"));
serialConnection = std::make_shared<SerialConnection>();
}
ConnectionSettingWidget::ConnectionSettingWidget(const QString &proto, QWidget *parent):
QWidget(parent),
ui(new Ui::ConnectionSettingWidget)
{
ui->setupUi(this);
setProtocol(proto);
refreshComList();
ui->toolBtnConnectSerial->setText(tr("连接"));
ui->toolBtnConnectTcp->setText(tr("连接"));
serialConnection = std::make_shared<SerialConnection>();
}
ConnectionSettingWidget::~ConnectionSettingWidget()
{
if(serialConnection->isConnected()){
serialConnection->close();
}
ConnectionManager::getInstance().remove(serialConnection);
delete ui;
}
void ConnectionSettingWidget::setProtocol(const QString &proto)
{
protocol = proto;
if(protocol=="TCP"){
ui->gpbTcp->setVisible(true);
ui->gpbSerial->setVisible(false);
}else{
ui->gpbTcp->setVisible(false);
ui->gpbSerial->setVisible(true);
}
}
void ConnectionSettingWidget::setParam(const QMap<QString, QString> &para)
{
QString proto = para.value("protocol","");
if(proto=="SERIAL"){
QString serialCom = para.value("serialCom","");
QString baud = para.value("baud","");
if(serialCom.isEmpty() || baud.isEmpty()){
return;
}
ui->cmbCom->setCurrentText(serialCom);
if(ui->cmbCom->currentText()!=serialCom){
ui->cmbCom->addItem(serialCom);
ui->cmbCom->setCurrentText(serialCom);
}
ui->cmbBaud->setCurrentText(baud);
if(ui->cmbBaud->currentText()!=baud){
ui->cmbBaud->addItem(baud);
ui->cmbBaud->setCurrentText(baud);
}
}
setProtocol(proto);
}
bool ConnectionSettingWidget::isConnected()
{
if(protocol=="SERIAL"){
return bSerialConnected;
}
return false;
}
std::shared_ptr<SerialConnection> ConnectionSettingWidget::getConnection()
{
return serialConnection;
}
QMap<QString, QString> ConnectionSettingWidget::getParam()
{
QMap<QString,QString> paras;
paras["protocol"] = protocol;
if(protocol=="SERIAL"){
paras["serialCom"] = ui->cmbCom->currentText();
paras["baud"] = ui->cmbBaud->currentText();
}
return paras;
}
void ConnectionSettingWidget::on_cmbCom_clicked()
{
refreshComList();
}
void ConnectionSettingWidget::on_toolBtnConnectSerial_clicked()
{
if(!bSerialConnected){
if(serialConnection->isConnected()){
serialConnection->close();
}
QMap<QString,QString> paras;
paras["serialCom"] = ui->cmbCom->currentText();
paras["baud"] = ui->cmbBaud->currentText();
serialConnection->setting(paras);
bSerialConnected = serialConnection->open();
if(bSerialConnected){
ui->toolBtnConnectSerial->setText(tr("断开"));
ui->toolBtnConnectSerial->setStyleSheet("color:red");
ui->cmbCom->setEnabled(false);
ui->cmbBaud->setEnabled(false);
ConnectionManager::getInstance().insert(serialConnection);
}
}else{
serialConnection->close();
bSerialConnected = false;
QMap<QString,QString> paras;
paras["serialCom"] = "";
paras["baud"] = ui->cmbBaud->currentText();
serialConnection->setting(paras);
ConnectionManager::getInstance().insert(serialConnection);
ui->toolBtnConnectSerial->setText(tr("连接"));
ui->toolBtnConnectSerial->setStyleSheet("color:green");
ui->cmbCom->setEnabled(true);
ui->cmbBaud->setEnabled(true);
}
}
void ConnectionSettingWidget::refreshComList()
{
QString currentTxt = ui->cmbCom->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->cmbCom->clear();
ui->cmbCom->addItems(nameList);
if(!currentTxt.isEmpty()){
ui->cmbCom->setCurrentText(currentTxt);
}
}

38
connectionSettingWidget.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef CONNECTIONSETTINGWIDGET_H
#define CONNECTIONSETTINGWIDGET_H
#include <QWidget>
#include "serialConnection.h"
namespace Ui {
class ConnectionSettingWidget;
}
class ConnectionSettingWidget : public QWidget
{
Q_OBJECT
public:
explicit ConnectionSettingWidget(QWidget *parent = nullptr);
explicit ConnectionSettingWidget(const QString &proto,QWidget *parent = nullptr);
~ConnectionSettingWidget();
void setProtocol(const QString &proto);
void setParam(const QMap<QString,QString> &para);
bool isConnected();
std::shared_ptr<SerialConnection> getConnection();
QMap<QString,QString> getParam();
signals:
public slots:
void on_cmbCom_clicked();
void on_toolBtnConnectSerial_clicked();
private:
Ui::ConnectionSettingWidget *ui;
QString protocol;
void refreshComList();
bool bSerialConnected = false;
std::shared_ptr<SerialConnection> serialConnection;
};
#endif // CONNECTIONSETTINGWIDGET_H

218
connectionSettingWidget.ui Normal file
View File

@ -0,0 +1,218 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ConnectionSettingWidget</class>
<widget class="QWidget" name="ConnectionSettingWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>272</width>
<height>48</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="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetNoConstraint</enum>
</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>
<widget class="QGroupBox" name="gpbSerial">
<property name="styleSheet">
<string notr="true">QGroupBox{border:none}</string>
</property>
<property name="title">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<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 row="0" column="1">
<widget class="QComboBox" name="cmbBaud">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="editable">
<bool>true</bool>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<item>
<property name="text">
<string>9600</string>
</property>
</item>
<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>
<item row="0" column="0">
<widget class="MyComboBox" name="cmbCom">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QToolButton" name="toolBtnConnectSerial">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">color:green</string>
</property>
<property name="text">
<string>连接</string>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="checkable">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="gpbTcp">
<property name="styleSheet">
<string notr="true">QGroupBox{border:none}</string>
</property>
<property name="title">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<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 row="0" column="0">
<widget class="QLineEdit" name="leIP">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lePort">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QToolButton" name="toolBtnConnectTcp">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">color:green</string>
</property>
<property name="text">
<string>连接</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>MyComboBox</class>
<extends>QComboBox</extends>
<header location="global">myComboBox.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

BIN
images/add.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

BIN
images/add_hover.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

18
img.qrc
View File

@ -1,8 +1,10 @@
<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>
<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>
<file>images/add.png</file>
<file>images/add_hover.png</file>
</qresource>
</RCC>

View File

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

View File

@ -1,22 +1,25 @@

#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;
}

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

View File

@ -1,26 +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
#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

View File

@ -1,42 +1,41 @@
<?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>
<?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>456</width>
<height>393</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>
<resources/>
<connections/>
</ui>

152
mutilSerialConfigWidget.cpp Normal file
View File

@ -0,0 +1,152 @@
#include "mutilSerialConfigWidget.h"
#include "ui_mutilSerialConfigWidget.h"
#include "serialConnection.h"
#include <QDateTime>
#include <QMenu>
#include <QAction>
#include <QScrollBar>
#include <QDebug>
MutilSerialConfigWidget::MutilSerialConfigWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MutilSerialConfigWidget)
{
ui->setupUi(this);
on_toolBtnAddLeft_clicked();
on_toolBtnAddRight_clicked();
}
MutilSerialConfigWidget::~MutilSerialConfigWidget()
{
delete ui;
}
void MutilSerialConfigWidget::receiverDataHandle(const QByteArray &data)
{
QDateTime current = QDateTime::currentDateTime();
SerialConnection *conn = qobject_cast<SerialConnection *>(sender());
bool left=false,right=false;
for(ConnectionSettingWidget* wdg:wdgLeftMap.keys()){
if(wdg->getConnection().get()==conn){
left = true;
break;
}
}
if(!left){
for(ConnectionSettingWidget* wdg:wdgRightMap.keys()){
if(wdg->getConnection().get()==conn){
right = true;
break;
}
}
}
if(left){
for(ConnectionSettingWidget* wdg:wdgRightMap.keys()){
SerialConnection *serial = qobject_cast<SerialConnection *>(wdg->getConnection().get());
serial->sendData(data);
}
}else if(right){
for(ConnectionSettingWidget* wdg:wdgLeftMap.keys()){
SerialConnection *serial = qobject_cast<SerialConnection *>(wdg->getConnection().get());
serial->sendData(data);
}
}
if(bStop){
return;
}
QString txt;
if(!bdisplayHex){
txt = QString::fromLocal8Bit(data);
}else{
foreach (const unsigned char byte, data) {
txt += QString("%1").arg(byte,2,16,QChar('0')).toUpper() + " ";
}
}
ui->plainTextEdit->appendPlainText(QString("[%1]%2:%3").arg(current.toString("hh:mm:ss.zzz")).arg(conn->getConnectionName()).arg(txt));
}
void MutilSerialConfigWidget::displayHexHandle()
{
bdisplayHex = true;
}
void MutilSerialConfigWidget::displayAsciiHandle()
{
bdisplayHex = false;
}
void MutilSerialConfigWidget::displayClearHandle()
{
ui->plainTextEdit->clear();
}
void MutilSerialConfigWidget::displayStopHandle()
{
bStop = !bStop;
if(!bStop){
QScrollBar *vScrollBar = ui->plainTextEdit->verticalScrollBar();
vScrollBar->setValue(vScrollBar->maximum());
}
}
void MutilSerialConfigWidget::on_toolBtnAddLeft_clicked()
{
QString protocol = "SERIAL";;
ConnectionSettingWidget *wdg = new ConnectionSettingWidget(protocol);
int row = ui->gridLayoutSerialLeft->rowCount();
ui->gridLayoutSerialLeft->addWidget(wdg,row,0);
wdgLeftMap[wdg] = QPair<int,int>(row,0);
connect(wdg->getConnection().get(),SIGNAL(recvDataSignal(QByteArray)),this,SLOT(receiverDataHandle(QByteArray)),Qt::QueuedConnection);
}
void MutilSerialConfigWidget::on_toolBtnAddRight_clicked()
{
QString protocol = "SERIAL";;
ConnectionSettingWidget *wdg = new ConnectionSettingWidget(protocol);
int row = ui->gridLayoutSerialRight->rowCount();
ui->gridLayoutSerialRight->addWidget(wdg,row,0);
wdgRightMap[wdg] = QPair<int,int>(row,0);
connect(wdg->getConnection().get(),SIGNAL(recvDataSignal(QByteArray)),this,SLOT(receiverDataHandle(QByteArray)),Qt::QueuedConnection);
}
void MutilSerialConfigWidget::on_plainTextEdit_customContextMenuRequested(const QPoint &pos)
{
//Q_UNUSED(pos);
QMenu *menu = new QMenu(this);
QAction *displayHex = new QAction(tr("HEX显示"),this);
QAction *displayAscii = new QAction(tr("ASCII显示"),this);
QAction *displayStop = nullptr;
QAction *displayClear = new QAction(tr("清空"),this);
if(bdisplayHex){
displayHex->setCheckable(true);
displayHex->setChecked(true);
}else{
displayAscii->setCheckable(true);
displayAscii->setChecked(true);
}
if(bStop){
displayStop = new QAction(tr("继续"),this);
}else{
displayStop = new QAction(tr("暂停"),this);
}
menu->addAction(displayHex);
menu->addAction(displayAscii);
menu->addAction(displayStop);
menu->addAction(displayClear);
connect(displayHex,SIGNAL(triggered(bool)),this,SLOT(displayHexHandle()));
connect(displayAscii,SIGNAL(triggered(bool)),this,SLOT(displayAsciiHandle()));
connect(displayClear,SIGNAL(triggered(bool)),this,SLOT(displayClearHandle()));
connect(displayStop,SIGNAL(triggered(bool)),this,SLOT(displayStopHandle()));
//让菜单移动鼠标位置并显示
//menu->exec(QCursor::pos());
menu->exec(ui->plainTextEdit->mapToGlobal(pos));
}

38
mutilSerialConfigWidget.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef MUTILSERIALCONFIGWIDGET_H
#define MUTILSERIALCONFIGWIDGET_H
#include <QWidget>
#include <QMap>
#include "connectionSettingWidget.h"
namespace Ui {
class MutilSerialConfigWidget;
}
class MutilSerialConfigWidget : public QWidget
{
Q_OBJECT
public:
explicit MutilSerialConfigWidget(QWidget *parent = nullptr);
~MutilSerialConfigWidget();
private slots:
void receiverDataHandle(const QByteArray &data);
void displayHexHandle();
void displayAsciiHandle();
void displayClearHandle();
void displayStopHandle();
void on_toolBtnAddLeft_clicked();
void on_toolBtnAddRight_clicked();
void on_plainTextEdit_customContextMenuRequested(const QPoint &pos);
private:
Ui::MutilSerialConfigWidget *ui;
QMap<ConnectionSettingWidget*,QPair<int,int>> wdgLeftMap;
QMap<ConnectionSettingWidget*,QPair<int,int>> wdgRightMap;
bool bdisplayHex=false;
bool bStop=false;
};
#endif // MUTILSERIALCONFIGWIDGET_H

361
mutilSerialConfigWidget.ui Normal file
View File

@ -0,0 +1,361 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MutilSerialConfigWidget</class>
<widget class="QWidget" name="MutilSerialConfigWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>617</width>
<height>420</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout_5" rowstretch="0,1" columnstretch="1,0,1">
<item row="0" column="0">
<widget class="QGroupBox" name="gpbSerialLeft">
<property name="title">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0" rowspan="3">
<layout class="QGridLayout" name="gridLayoutSerialLeft"/>
</item>
<item row="3" column="0">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1" rowspan="4">
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">QGroupBox{border:none}</string>
</property>
<property name="title">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<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>
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QToolButton" name="toolBtnAddLeft">
<property name="cursor">
<cursorShape>ArrowCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">QToolButton{
border:none;
background:transparent;
image:url(:/images/images/add.png);
}
QToolButton::hover{
border:none;
background:transparent;
image:url(:/images/images/add_hover.png);
}</string>
</property>
<property name="text">
<string/>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonIconOnly</enum>
</property>
<property name="autoRaise">
<bool>false</bool>
</property>
<property name="arrowType">
<enum>Qt::NoArrow</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</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>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<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>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item row="0" column="2">
<widget class="QGroupBox" name="gpbSerialRight">
<property name="title">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0" rowspan="2">
<layout class="QGridLayout" name="gridLayoutSerialRight"/>
</item>
<item row="0" column="1" rowspan="3">
<widget class="QGroupBox" name="groupBox_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">QGroupBox{border:none}</string>
</property>
<property name="title">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<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>
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QToolButton" name="toolBtnAddRight">
<property name="cursor">
<cursorShape>ArrowCursor</cursorShape>
</property>
<property name="styleSheet">
<string notr="true">QToolButton{
border:none;
background:transparent;
image:url(:/images/images/add.png);
}
QToolButton::hover{
border:none;
background:transparent;
image:url(:/images/images/add_hover.png);
}</string>
</property>
<property name="text">
<string/>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonIconOnly</enum>
</property>
<property name="autoRaise">
<bool>false</bool>
</property>
<property name="arrowType">
<enum>Qt::NoArrow</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer_6">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="maximumBlockCount">
<number>100000</number>
</property>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="img.qrc"/>
</resources>
<connections/>
</ui>

20
myComboBox.cpp Normal file
View File

@ -0,0 +1,20 @@

#include "myComboBox.h"
#include <QMouseEvent>
MyComboBox::MyComboBox(QWidget *parent)
: QComboBox{parent}
{
}
void MyComboBox::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)//如果点击左键
{
emit clicked();//发出clicked信号
}
QComboBox::mousePressEvent(event);//将该事件传给父类处理,这句话很重要,如果没有,父类无法处理本来的点击事件
}

17
myComboBox.h Normal file
View File

@ -0,0 +1,17 @@

#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H
#include <QComboBox>
class MyComboBox : public QComboBox
{
Q_OBJECT
public:
explicit MyComboBox(QWidget *parent = nullptr);
void mousePressEvent(QMouseEvent *event) override;
signals:
void clicked();
};
#endif // MYCOMBOBOX_H

View File

@ -1,169 +1,168 @@
#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;
}
}
}
#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;
}
}
}

View File

@ -1,41 +1,42 @@
#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
#ifndef SERIALCONFIGWIDGET_H
#define SERIALCONFIGWIDGET_H
#include <QWidget>
#include <QMutex>
#include <cstring>
#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

View File

@ -1,284 +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>
<?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>706</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,0,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>

110
serialConnection.cpp Normal file
View File

@ -0,0 +1,110 @@

#include "serialConnection.h"
#include <QMessageBox>
#include <QDebug>
SerialConnection::SerialConnection()
{
m_SerialPort.connectReadEvent(this);
protocol = "SERIAL";
}
SerialConnection::~SerialConnection()
{
if(isConnected()){
close();
}
}
bool SerialConnection::open()
{
QString portName = paras.value("serialCom","");
QString baud = paras.value("baud","115200");
int databits = paras.value("databits",QString::number(itas109::DataBits::DataBits8)).toInt();
int stopbits = paras.value("stopbits",QString::number(itas109::StopBits::StopOne)).toInt();
int parity = paras.value("parity",QString::number(itas109::Parity::ParityNone)).toInt();
int flowControl = paras.value("flowControl",QString::number(itas109::FlowControl::FlowNone)).toInt();
if(portName.isEmpty()){
return false;
}
m_SerialPort.init(portName.toStdString().c_str(),baud.toInt(),
itas109::Parity(parity),itas109::DataBits(databits),
itas109::StopBits(stopbits),itas109::FlowControl(flowControl),1024*64);
m_SerialPort.setReadIntervalTimeout(readIntervalTimeoutMS);
if(m_SerialPort.open()){
emit connectionStatusChangeSignal(true);
return true;
}else{
QMessageBox::warning(NULL,tr("警告"),tr("串口打开错误") + QString("\n\ncode: %1\nmessage: %2").arg(
m_SerialPort.getLastError()).arg(m_SerialPort.getLastErrorMsg()));
emit connectionStatusChangeSignal(false);
return false;
}
}
bool SerialConnection::close()
{
m_SerialPort.close();
emit connectionStatusChangeSignal(false);
return true;
}
bool SerialConnection::isConnected()
{
return m_SerialPort.isOpen();
}
int SerialConnection::sendData(const QByteArray &data)
{
if(m_SerialPort.isOpen())
{
const char *s = data.constData();
// 支持中文并获取正确的长度
int len = m_SerialPort.writeData(s,data.length());
emit recvUserSendDataSignal(data);
return len;
}else{
return -1;
}
}
void SerialConnection::setting(QMap<QString, QString> &map)
{
if(isConnected()){
close();
}
paras = map;
connectionName = paras.value("serialCom","");
}
void SerialConnection::onReadEvent(const char *portName, unsigned int readBufferLen)
{
Q_UNUSED(portName);
if(readBufferLen > 0)
{
int recLen = 0;
char * str = NULL;
str = new char[readBufferLen];
if(str){
recLen = m_SerialPort.readData(str, readBufferLen);
}
if(recLen > 0)
{
// TODO: 中文需要由两个字符拼接,否则显示为空""
//QString m_str = QString::fromLocal8Bit(str,recLen);
//emitUpdateReceive(m_str);
QByteArray array(str,recLen);
emit recvDataSignal(array);
}
if(str)
{
delete[] str;
str = NULL;
}
}
}

31
serialConnection.h Normal file
View File

@ -0,0 +1,31 @@

#ifndef SERIALCONNECTION_H
#define SERIALCONNECTION_H
#include "baseConnection.h"
#include "CSerialPort/SerialPort.h"
using namespace itas109;
class SerialConnection :public BaseConnection, public CSerialPortListener
{
Q_OBJECT
public:
SerialConnection();
~SerialConnection();
bool open() override;
bool close() override;
bool isConnected() override;
int sendData(const QByteArray &data) override;
void setting(QMap<QString,QString> &map) override;
private slots:
void onReadEvent(const char *portName, unsigned int readBufferLen) override;
private:
CSerialPort m_SerialPort;
int readIntervalTimeoutMS = 20;
};
#endif // SERIALCONNECTION_H

View File

@ -16,16 +16,30 @@ msvc{
include($$PWD/3rdparty/CSerialPort/examples/CommQT/commqt.pri)
SOURCES += \
baseConnection.cpp \
connectionManager.cpp \
connectionSettingWidget.cpp \
main.cpp \
mainwindow.cpp \
serialConfigWidget.cpp
mutilSerialConfigWidget.cpp \
myComboBox.cpp \
serialConfigWidget.cpp \
serialConnection.cpp
HEADERS += \
baseConnection.h \
connectionManager.h \
connectionSettingWidget.h \
mainwindow.h \
serialConfigWidget.h
mutilSerialConfigWidget.h \
myComboBox.h \
serialConfigWidget.h \
serialConnection.h
FORMS += \
connectionSettingWidget.ui \
mainwindow.ui \
mutilSerialConfigWidget.ui \
serialConfigWidget.ui
# Default rules for deployment.