Skip to content

Commit

Permalink
Merge pull request #20 from shiftee/tcp_reconnect
Browse files Browse the repository at this point in the history
Handle TCP disconnections
  • Loading branch information
tobydox authored Mar 6, 2018
2 parents 8c18311 + 03f27bf commit efd0a85
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 51 deletions.
29 changes: 1 addition & 28 deletions forms/tcpipsettingswidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>547</width>
<height>156</height>
<height>217</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -91,33 +91,6 @@
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="rightMargin">
<number>6</number>
</property>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnApply">
<property name="text">
<string>Apply</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
Expand Down
12 changes: 10 additions & 2 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private slots:
QLabel * m_statusText;
QTimer * m_pollTimer;
QTimer * m_statusTimer;
bool m_tcpActive;
bool m_poll;
};

Expand Down
23 changes: 5 additions & 18 deletions src/tcpipsettingswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

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)));
ui->edPort->setValidator(new QIntValidator(this));
enableGuiItems(false);
}
Expand All @@ -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();
}
}
Expand All @@ -55,26 +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()
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);
}
4 changes: 1 addition & 3 deletions src/tcpipsettingswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -26,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);
Expand Down

0 comments on commit efd0a85

Please sign in to comment.