From c25a9bd552c47a38256e2e05c2c2573f458e8eba Mon Sep 17 00:00:00 2001 From: Mark O'Donovan Date: Tue, 6 Mar 2018 13:49:06 +0000 Subject: [PATCH 1/2] Open new TCP connection for each request This allows us to handle broken TCP connections which occur often when testing ModbusTCP systems. Up to now when a TCP connection breaks, the user must modify either the ip address or port to re-enable the Apply button which can then be used to open a new connection. --- src/mainwindow.cpp | 12 ++++++++++-- src/mainwindow.h | 1 + src/tcpipsettingswidget.cpp | 10 ++++++++-- src/tcpipsettingswidget.h | 1 + 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index ebba32b..99575b4 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -47,6 +47,7 @@ MainWindow::MainWindow( QWidget * _parent ) : QMainWindow( _parent ), ui( new Ui::MainWindowClass ), m_modbus( NULL ), + m_tcpActive(false), m_poll(false) { ui->setupUi(this); @@ -381,8 +382,12 @@ void MainWindow::enableHexView( void ) void MainWindow::sendModbusRequest( void ) { + if( m_tcpActive ) + ui->tcpSettingsWidget->tcpConnect(); + if( m_modbus == NULL ) { + setStatusError( tr("Not configured!") ); return; } @@ -539,8 +544,7 @@ void MainWindow::sendModbusRequest( void ) err += tr( "Protocol error" ); err += ": "; err += tr( "Number of registers returned does not " - "match number of registers " - "requested!" ); + "match number of registers requested!" ); } if( err.size() > 0 ) @@ -582,6 +586,7 @@ void MainWindow::onRtuPortActive(bool active) modbus_register_monitor_add_item_fnc(m_modbus, MainWindow::stBusMonitorAddItem); modbus_register_monitor_raw_data_fnc(m_modbus, MainWindow::stBusMonitorRawData); } + m_tcpActive = false; } else { m_modbus = NULL; @@ -596,6 +601,7 @@ void MainWindow::onAsciiPortActive(bool active) modbus_register_monitor_add_item_fnc(m_modbus, MainWindow::stBusMonitorAddItem); modbus_register_monitor_raw_data_fnc(m_modbus, MainWindow::stBusMonitorRawData); } + m_tcpActive = false; } else { m_modbus = NULL; @@ -604,6 +610,8 @@ void MainWindow::onAsciiPortActive(bool active) void MainWindow::onTcpPortActive(bool active) { + m_tcpActive = active; + if (active) { m_modbus = ui->tcpSettingsWidget->modbus(); if (m_modbus) { diff --git a/src/mainwindow.h b/src/mainwindow.h index 3c8746a..91168ac 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -100,6 +100,7 @@ private slots: QLabel * m_statusText; QTimer * m_pollTimer; QTimer * m_statusTimer; + bool m_tcpActive; bool m_poll; }; diff --git a/src/tcpipsettingswidget.cpp b/src/tcpipsettingswidget.cpp index 20eeea2..40a5a3d 100644 --- a/src/tcpipsettingswidget.cpp +++ b/src/tcpipsettingswidget.cpp @@ -6,8 +6,8 @@ TcpIpSettingsWidget::TcpIpSettingsWidget(QWidget *parent) : QWidget(parent), - ui(new Ui::TcpIpSettingsWidget) -, m_tcpModbus(0) + ui(new Ui::TcpIpSettingsWidget), + m_tcpModbus(0) { ui->setupUi(this); connect(ui->edNetworkAddress, SIGNAL(textChanged(QString)), this, SLOT(onEdNetworkAddressTextChanged(QString))); @@ -59,9 +59,15 @@ void TcpIpSettingsWidget::enableGuiItems(bool checked) void TcpIpSettingsWidget::on_cbEnabled_clicked(bool checked) { enableGuiItems(checked); + emit tcpPortActive(checked); } void TcpIpSettingsWidget::on_btnApply_clicked() +{ + tcpConnect(); +} + +void TcpIpSettingsWidget::tcpConnect() { int portNbr = ui->edPort->text().toInt(); ui->btnApply->setEnabled(false); diff --git a/src/tcpipsettingswidget.h b/src/tcpipsettingswidget.h index 6310dd5..094c89a 100644 --- a/src/tcpipsettingswidget.h +++ b/src/tcpipsettingswidget.h @@ -18,6 +18,7 @@ class TcpIpSettingsWidget : public QWidget, public IModbus // IModbus interface virtual modbus_t *modbus() { return m_tcpModbus; } virtual int setupModbusPort(); + void tcpConnect(); protected: void changeModbusInterface(const QString& address, int portNbr); From 03f27bf66a983e7bfc8a6d60a60c2e0b1f60daaf Mon Sep 17 00:00:00 2001 From: Mark O'Donovan Date: Tue, 6 Mar 2018 14:17:09 +0000 Subject: [PATCH 2/2] Remove Apply button from TCP/IP tab It is no longer needed --- forms/tcpipsettingswidget.ui | 29 +---------------------------- src/tcpipsettingswidget.cpp | 21 +-------------------- src/tcpipsettingswidget.h | 3 --- 3 files changed, 2 insertions(+), 51 deletions(-) diff --git a/forms/tcpipsettingswidget.ui b/forms/tcpipsettingswidget.ui index e77081b..9720a3b 100644 --- a/forms/tcpipsettingswidget.ui +++ b/forms/tcpipsettingswidget.ui @@ -7,7 +7,7 @@ 0 0 547 - 156 + 217 @@ -91,33 +91,6 @@ - - - - 6 - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Apply - - - - - diff --git a/src/tcpipsettingswidget.cpp b/src/tcpipsettingswidget.cpp index 40a5a3d..ed66891 100644 --- a/src/tcpipsettingswidget.cpp +++ b/src/tcpipsettingswidget.cpp @@ -10,7 +10,6 @@ TcpIpSettingsWidget::TcpIpSettingsWidget(QWidget *parent) : m_tcpModbus(0) { ui->setupUi(this); - connect(ui->edNetworkAddress, SIGNAL(textChanged(QString)), this, SLOT(onEdNetworkAddressTextChanged(QString))); ui->edPort->setValidator(new QIntValidator(this)); enableGuiItems(false); } @@ -32,9 +31,8 @@ void TcpIpSettingsWidget::changeModbusInterface(const QString &address, int port m_tcpModbus = modbus_new_tcp( address.toLatin1().constData(), portNbr ); if( modbus_connect( m_tcpModbus ) == -1 ) { - emit connectionError( tr( "Could not connect tcp/ip port!" ) ); + emit connectionError( tr( "Could not connect to TCP/IP port!" ) ); - ui->btnApply->setEnabled(true); releaseTcpModbus(); } } @@ -55,32 +53,15 @@ void TcpIpSettingsWidget::enableGuiItems(bool checked) ui->edNetworkAddress->setEnabled(checked); } - void TcpIpSettingsWidget::on_cbEnabled_clicked(bool checked) { enableGuiItems(checked); emit tcpPortActive(checked); } -void TcpIpSettingsWidget::on_btnApply_clicked() -{ - tcpConnect(); -} - void TcpIpSettingsWidget::tcpConnect() { int portNbr = ui->edPort->text().toInt(); - ui->btnApply->setEnabled(false); changeModbusInterface(ui->edNetworkAddress->text(), portNbr); emit tcpPortActive(ui->cbEnabled->isChecked()); } - -void TcpIpSettingsWidget::onEdNetworkAddressTextChanged(const QString &) -{ - ui->btnApply->setEnabled(true); -} - -void TcpIpSettingsWidget::on_edPort_textChanged(const QString &) -{ - ui->btnApply->setEnabled(true); -} diff --git a/src/tcpipsettingswidget.h b/src/tcpipsettingswidget.h index 094c89a..7f10ac8 100644 --- a/src/tcpipsettingswidget.h +++ b/src/tcpipsettingswidget.h @@ -27,9 +27,6 @@ class TcpIpSettingsWidget : public QWidget, public IModbus private slots: void on_cbEnabled_clicked(bool checked); - void on_btnApply_clicked(); - void onEdNetworkAddressTextChanged(const QString &arg1); - void on_edPort_textChanged(const QString &arg1); signals: void tcpPortActive(bool val);