diff --git a/baseConnection.cpp b/baseConnection.cpp new file mode 100644 index 0000000..7014443 --- /dev/null +++ b/baseConnection.cpp @@ -0,0 +1,30 @@ + +#include "baseConnection.h" + +BaseConnection::BaseConnection(QObject *parent) + : QObject{parent} +{ + +} + +void BaseConnection::setting(QMap &map) +{ + paras = map; +} + +QString BaseConnection::getProtocol() +{ + return protocol; +} + +QMap BaseConnection::getParas() +{ + return paras; +} + +QString BaseConnection::getConnectionName() +{ + return connectionName; +} + + diff --git a/baseConnection.h b/baseConnection.h new file mode 100644 index 0000000..8254334 --- /dev/null +++ b/baseConnection.h @@ -0,0 +1,34 @@ + +#ifndef BASECONNECTION_H +#define BASECONNECTION_H + + +#include +#include + +class BaseConnection : public QObject +{ + Q_OBJECT +public: + explicit BaseConnection(QObject *parent = nullptr); + QString getProtocol(); + QMap getParas(); + QString getConnectionName(); + virtual void setting(QMap &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 paras; + QString connectionName; +}; + +#endif // BASECONNECTION_H diff --git a/connectionManager.cpp b/connectionManager.cpp new file mode 100644 index 0000000..e8f7a7f --- /dev/null +++ b/connectionManager.cpp @@ -0,0 +1,92 @@ + +#include "connectionManager.h" +#include + +ConnectionManager::ConnectionManager(QObject *parent): QObject{parent} +{ + +} + +ConnectionManager::~ConnectionManager() +{ + +} + +void ConnectionManager::insert(std::shared_ptr conn) +{ + if(conn){ + bool same = false; + for(int i=0;i=0 && idx<=connectionVec.length()){ + if(connectionVec[idx]->isConnected()){ + connectionVec[idx]->close(); + } + QString name = connectionVec[idx]->getConnectionName(); + connectionVec[idx]->disconnect(); + std::shared_ptr connPtr = connectionVec[idx]; + connectionVec.remove(idx); + emit connectionRemovedSignal(connPtr); + } +} + +void ConnectionManager::remove(std::shared_ptr conn) +{ + int idx = -1; + for(int i=0;iisConnected()){ + 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 ConnectionManager::getConnection(int idx) +{ + std::shared_ptr conn = nullptr; + if(idx>=0 && idx<=connectionVec.length()){ + conn = connectionVec[idx]; + } + return conn; +} + +std::shared_ptr ConnectionManager::getConnection(const QString &connectionName) +{ + std::shared_ptr conn = nullptr; + for(int i=0;igetConnectionName()){ + conn = connectionVec[i]; + } + } + return conn; +} diff --git a/connectionManager.h b/connectionManager.h new file mode 100644 index 0000000..d880599 --- /dev/null +++ b/connectionManager.h @@ -0,0 +1,37 @@ + +#ifndef CONNECTIONMANAGER_H +#define CONNECTIONMANAGER_H + +#include +#include +#include "baseConnection.h" +#include + +class ConnectionManager:public QObject +{ + Q_OBJECT +public: + static ConnectionManager& getInstance() + { + static ConnectionManager instance; + return instance; + } + void insert(std::shared_ptr conn); + void remove(int idx); + void remove(std::shared_ptr conn); + int getConnectionCounts(); + std::shared_ptr getConnection(int idx); + std::shared_ptr getConnection(const QString &connectionName); + +signals: + void connectionAddedSignal(std::shared_ptr connPtr); + void connectionRemovedSignal(std::shared_ptr connPtr); + void connectionChangeSignal(std::shared_ptr connPtr); + +private: + ConnectionManager(QObject *parent = nullptr); + ~ConnectionManager(); + QVector> connectionVec; +}; + +#endif // CONNECTIONMANAGER_H diff --git a/connectionSettingWidget.cpp b/connectionSettingWidget.cpp new file mode 100644 index 0000000..eb19972 --- /dev/null +++ b/connectionSettingWidget.cpp @@ -0,0 +1,157 @@ +#include "connectionSettingWidget.h" +#include "ui_connectionSettingWidget.h" +#include "connectionManager.h" + +#include +#include + +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(); +} + +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(); +} + +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 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 ConnectionSettingWidget::getConnection() +{ + return serialConnection; +} + +QMap ConnectionSettingWidget::getParam() +{ + QMap 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 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 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 s1cmbCom->clear(); + ui->cmbCom->addItems(nameList); + if(!currentTxt.isEmpty()){ + ui->cmbCom->setCurrentText(currentTxt); + } +} + diff --git a/connectionSettingWidget.h b/connectionSettingWidget.h new file mode 100644 index 0000000..472a213 --- /dev/null +++ b/connectionSettingWidget.h @@ -0,0 +1,38 @@ +#ifndef CONNECTIONSETTINGWIDGET_H +#define CONNECTIONSETTINGWIDGET_H + +#include +#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 ¶); + bool isConnected(); + std::shared_ptr getConnection(); + QMap 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; +}; + +#endif // CONNECTIONSETTINGWIDGET_H diff --git a/connectionSettingWidget.ui b/connectionSettingWidget.ui new file mode 100644 index 0000000..d5f6dcf --- /dev/null +++ b/connectionSettingWidget.ui @@ -0,0 +1,218 @@ + + + ConnectionSettingWidget + + + + 0 + 0 + 272 + 48 + + + + + 0 + 0 + + + + Form + + + + 0 + + + QLayout::SetNoConstraint + + + 0 + + + 0 + + + 0 + + + 0 + + + + + QGroupBox{border:none} + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 1 + 0 + + + + true + + + 1 + + + + 9600 + + + + + 115200 + + + + + 230400 + + + + + 460800 + + + + + 921600 + + + + + + + + + 1 + 0 + + + + + + + + + 0 + 0 + + + + + 0 + 0 + + + + color:green + + + 连接 + + + + 16 + 16 + + + + false + + + + + + + + + + QGroupBox{border:none} + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + + + + + 0 + 0 + + + + + + + + + 0 + 0 + + + + color:green + + + 连接 + + + + + + + + + + + MyComboBox + QComboBox +
myComboBox.h
+
+
+ + +
diff --git a/images/add.png b/images/add.png new file mode 100644 index 0000000..a0e8613 Binary files /dev/null and b/images/add.png differ diff --git a/images/add_hover.png b/images/add_hover.png new file mode 100644 index 0000000..196310a Binary files /dev/null and b/images/add_hover.png differ diff --git a/img.qrc b/img.qrc index 09f45bd..4278a53 100644 --- a/img.qrc +++ b/img.qrc @@ -1,8 +1,10 @@ - - - images/right_arrow_green.png - images/left_arrow_green.png - images/right_arrow_gray.png - images/left_arrow_gray.png - - + + + images/right_arrow_green.png + images/left_arrow_green.png + images/right_arrow_gray.png + images/left_arrow_gray.png + images/add.png + images/add_hover.png + + diff --git a/main.cpp b/main.cpp index d871ca9..b2de7ab 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,13 @@ - -#include "mainwindow.h" - -#include - - -int main(int argc, char *argv[]) -{ - QApplication a(argc, argv); - MainWindow w; - w.show(); - return a.exec(); -} + +#include "mainwindow.h" + +#include + + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp index b38b9ce..43faf12 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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; +} + + diff --git a/mainwindow.h b/mainwindow.h index 1cafe77..0d6ffbf 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -1,26 +1,26 @@ - -#ifndef MAINWINDOW_H -#define MAINWINDOW_H - -#include - - - -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 + + + +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 diff --git a/mainwindow.ui b/mainwindow.ui index 71b774f..18f9947 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -1,42 +1,41 @@ - - - MainWindow - - - - 0 - 0 - 555 - 500 - - - - MainWindow - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - - - - - - + + + MainWindow + + + + 0 + 0 + 456 + 393 + + + + MainWindow + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + + diff --git a/mutilSerialConfigWidget.cpp b/mutilSerialConfigWidget.cpp new file mode 100644 index 0000000..f7d4e45 --- /dev/null +++ b/mutilSerialConfigWidget.cpp @@ -0,0 +1,152 @@ +#include "mutilSerialConfigWidget.h" +#include "ui_mutilSerialConfigWidget.h" +#include "serialConnection.h" +#include +#include +#include +#include +#include + +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(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(wdg->getConnection().get()); + serial->sendData(data); + } + }else if(right){ + for(ConnectionSettingWidget* wdg:wdgLeftMap.keys()){ + SerialConnection *serial = qobject_cast(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(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(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)); +} + diff --git a/mutilSerialConfigWidget.h b/mutilSerialConfigWidget.h new file mode 100644 index 0000000..a7d9faf --- /dev/null +++ b/mutilSerialConfigWidget.h @@ -0,0 +1,38 @@ +#ifndef MUTILSERIALCONFIGWIDGET_H +#define MUTILSERIALCONFIGWIDGET_H + +#include +#include +#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> wdgLeftMap; + QMap> wdgRightMap; + bool bdisplayHex=false; + bool bStop=false; +}; + +#endif // MUTILSERIALCONFIGWIDGET_H diff --git a/mutilSerialConfigWidget.ui b/mutilSerialConfigWidget.ui new file mode 100644 index 0000000..3975824 --- /dev/null +++ b/mutilSerialConfigWidget.ui @@ -0,0 +1,361 @@ + + + MutilSerialConfigWidget + + + + 0 + 0 + 617 + 420 + + + + Form + + + + + + + + + + + + + + + Qt::Vertical + + + QSizePolicy::Expanding + + + + 20 + 0 + + + + + + + + + 0 + 0 + + + + QGroupBox{border:none} + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + ArrowCursor + + + QToolButton{ +border:none; +background:transparent; +image:url(:/images/images/add.png); +} +QToolButton::hover{ +border:none; +background:transparent; +image:url(:/images/images/add_hover.png); +} + + + + + + + 16 + 16 + + + + Qt::ToolButtonIconOnly + + + false + + + Qt::NoArrow + + + + + + + Qt::Vertical + + + + 20 + 0 + + + + + + + + + + + + + + QGroupBox{border:none} + + + + + + false + + + + 0 + + + + + Qt::Vertical + + + + 20 + 0 + + + + + + + + + 0 + 0 + + + + + 40 + 12 + + + + + 40 + 12 + + + + false + + + + + + :/images/images/left_arrow_gray.png + + + true + + + + + + + + 0 + 0 + + + + + 40 + 12 + + + + + 40 + 12 + + + + + + + :/images/images/right_arrow_gray.png + + + true + + + + + + + Qt::Vertical + + + + 20 + 0 + + + + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 0 + + + + + + + + + + + + 0 + 0 + + + + QGroupBox{border:none} + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + ArrowCursor + + + QToolButton{ +border:none; +background:transparent; +image:url(:/images/images/add.png); +} +QToolButton::hover{ +border:none; +background:transparent; +image:url(:/images/images/add_hover.png); +} + + + + + + + 16 + 16 + + + + Qt::ToolButtonIconOnly + + + false + + + Qt::NoArrow + + + + + + + Qt::Vertical + + + + 20 + 0 + + + + + + + + + + + + + + Qt::CustomContextMenu + + + 100000 + + + + + + + + + + diff --git a/myComboBox.cpp b/myComboBox.cpp new file mode 100644 index 0000000..3d2fc28 --- /dev/null +++ b/myComboBox.cpp @@ -0,0 +1,20 @@ + +#include "myComboBox.h" +#include + +MyComboBox::MyComboBox(QWidget *parent) + : QComboBox{parent} +{ + +} + +void MyComboBox::mousePressEvent(QMouseEvent *event) +{ + if(event->button() == Qt::LeftButton)//如果点击左键 + { + emit clicked();//发出clicked信号 + } + + QComboBox::mousePressEvent(event);//将该事件传给父类处理,这句话很重要,如果没有,父类无法处理本来的点击事件 +} + diff --git a/myComboBox.h b/myComboBox.h new file mode 100644 index 0000000..9dc27f9 --- /dev/null +++ b/myComboBox.h @@ -0,0 +1,17 @@ + +#ifndef MYCOMBOBOX_H +#define MYCOMBOBOX_H + +#include + +class MyComboBox : public QComboBox +{ + Q_OBJECT +public: + explicit MyComboBox(QWidget *parent = nullptr); + void mousePressEvent(QMouseEvent *event) override; +signals: + void clicked(); +}; + +#endif // MYCOMBOBOX_H diff --git a/serialConfigWidget.cpp b/serialConfigWidget.cpp index 46bc72f..501355f 100644 --- a/serialConfigWidget.cpp +++ b/serialConfigWidget.cpp @@ -1,169 +1,168 @@ -#include "serialConfigWidget.h" -#include "ui_serialConfigWidget.h" -#include -#include -#include -#include -#include -#include - -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 s1cmbCom1->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 +#include +#include +#include +#include +#include + +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 s1cmbCom1->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; + } + } +} + diff --git a/serialConfigWidget.h b/serialConfigWidget.h index 7e92db3..e958c35 100644 --- a/serialConfigWidget.h +++ b/serialConfigWidget.h @@ -1,41 +1,42 @@ -#ifndef SERIALCONFIGWIDGET_H -#define SERIALCONFIGWIDGET_H - -#include -#include -#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 +#include +#include +#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 diff --git a/serialConfigWidget.ui b/serialConfigWidget.ui index 3b5bc7f..b53175c 100644 --- a/serialConfigWidget.ui +++ b/serialConfigWidget.ui @@ -1,284 +1,284 @@ - - - SerialConfigWidget - - - - 0 - 0 - 702 - 374 - - - - - 0 - 0 - - - - Form - - - - 4 - - - 4 - - - 4 - - - 4 - - - 0 - - - - - QGroupBox{border:none} - - - - - - true - - - - - - 串口号: - - - - - - - - - - 波特率: - - - - - - - true - - - - 115200 - - - - - 230400 - - - - - 460800 - - - - - 921600 - - - - - - - - - - - QGroupBox{border:none} - - - - - - true - - - - - - 串口号: - - - - - - - - - - 波特率: - - - - - - - true - - - - 115200 - - - - - 230400 - - - - - 460800 - - - - - 921600 - - - - - - - - - - - QGroupBox{border:none} - - - - - - - - - 刷新 - - - - - - - 确定 - - - true - - - false - - - - - - - - - - QGroupBox{border:none} - - - - - - false - - - - 0 - - - - - - 0 - 0 - - - - - 40 - 12 - - - - - 40 - 12 - - - - false - - - - - - :/images/images/left_arrow_gray.png - - - true - - - - - - - - 0 - 0 - - - - - 40 - 12 - - - - - 40 - 12 - - - - - - - :/images/images/right_arrow_gray.png - - - true - - - - - - - - - - 10000 - - - - - - - - - - + + + SerialConfigWidget + + + + 0 + 0 + 706 + 374 + + + + + 0 + 0 + + + + Form + + + + 4 + + + 4 + + + 4 + + + 4 + + + 0 + + + + + QGroupBox{border:none} + + + + + + true + + + + + + 串口号: + + + + + + + + + + 波特率: + + + + + + + true + + + + 115200 + + + + + 230400 + + + + + 460800 + + + + + 921600 + + + + + + + + + + + QGroupBox{border:none} + + + + + + true + + + + + + 串口号: + + + + + + + + + + 波特率: + + + + + + + true + + + + 115200 + + + + + 230400 + + + + + 460800 + + + + + 921600 + + + + + + + + + + + QGroupBox{border:none} + + + + + + + + + 刷新 + + + + + + + 确定 + + + true + + + false + + + + + + + + + + QGroupBox{border:none} + + + + + + false + + + + 0 + + + + + + 0 + 0 + + + + + 40 + 12 + + + + + 40 + 12 + + + + false + + + + + + :/images/images/left_arrow_gray.png + + + true + + + + + + + + 0 + 0 + + + + + 40 + 12 + + + + + 40 + 12 + + + + + + + :/images/images/right_arrow_gray.png + + + true + + + + + + + + + + 10000 + + + + + + + + + + diff --git a/serialConnection.cpp b/serialConnection.cpp new file mode 100644 index 0000000..b8c320e --- /dev/null +++ b/serialConnection.cpp @@ -0,0 +1,110 @@ + +#include "serialConnection.h" +#include +#include + +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 &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; + } + } +} + diff --git a/serialConnection.h b/serialConnection.h new file mode 100644 index 0000000..fd4dd1f --- /dev/null +++ b/serialConnection.h @@ -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 &map) override; + +private slots: + void onReadEvent(const char *portName, unsigned int readBufferLen) override; + +private: + CSerialPort m_SerialPort; + int readIntervalTimeoutMS = 20; + +}; + +#endif // SERIALCONNECTION_H diff --git a/serialTransferTool.pro b/serialTransferTool.pro index 63f8011..e9cddae 100644 --- a/serialTransferTool.pro +++ b/serialTransferTool.pro @@ -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.