Skip to content

Commit e4bd0a2

Browse files
committed
Adding modbus tcp connection address
1 parent 1d17bb9 commit e4bd0a2

7 files changed

Lines changed: 64 additions & 46 deletions

File tree

omodsim/connectiondetails.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
struct TcpConnectionParams
1515
{
1616
quint16 ServicePort = 502;
17-
const QString IPAddress = "0.0.0.0";
17+
QString IPAddress = "0.0.0.0";
1818

1919
void normalize()
2020
{
21+
const auto addr = QHostAddress(IPAddress);
22+
IPAddress = addr.isNull() ? "0.0.0.0" : addr.toString();
2123
ServicePort = qMax<quint16>(1, ServicePort);
2224
}
2325

2426
TcpConnectionParams& operator=(const TcpConnectionParams& params)
2527
{
28+
IPAddress = params.IPAddress;
2629
ServicePort = params.ServicePort;
2730
return *this;
2831
}
@@ -43,6 +46,7 @@ Q_DECLARE_METATYPE(TcpConnectionParams)
4346
inline QDataStream& operator <<(QDataStream& out, const TcpConnectionParams& params)
4447
{
4548
out << params.ServicePort;
49+
out << params.IPAddress;
4650
return out;
4751
}
4852

@@ -55,6 +59,7 @@ inline QDataStream& operator <<(QDataStream& out, const TcpConnectionParams& par
5559
inline QDataStream& operator >>(QDataStream& in, TcpConnectionParams& params)
5660
{
5761
in >> params.ServicePort;
62+
in >> params.IPAddress;
5863
params.normalize();
5964
return in;
6065
}

omodsim/controls/mainstatusbar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void MainStatusBar::updateConnectionInfo(QLabel* label, const ConnectionDetails&
107107
switch(cd.Type)
108108
{
109109
case ConnectionType::Tcp:
110-
label->setText(QString(tr("Modbus/TCP Srv: %1")).arg(cd.TcpParams.ServicePort));
110+
label->setText(QString(tr("TCP/IP %1:%2")).arg(cd.TcpParams.IPAddress, QString::number(cd.TcpParams.ServicePort)));
111111
break;
112112

113113
case ConnectionType::Serial:

omodsim/dialogs/dialogselectserviceport.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <QNetworkInterface>
12
#include "dialogselectserviceport.h"
23
#include "ui_dialogselectserviceport.h"
34

@@ -6,14 +7,37 @@
67
/// \param port
78
/// \param parent
89
///
9-
DialogSelectServicePort::DialogSelectServicePort(quint16& port, QWidget *parent)
10+
DialogSelectServicePort::DialogSelectServicePort(TcpConnectionParams& params, QWidget *parent)
1011
: QFixedSizeDialog(parent)
1112
, ui(new Ui::DialogSelectServicePort)
12-
,_port(port)
13+
,_params(params)
1314
{
1415
ui->setupUi(this);
1516
ui->lineEditPort->setInputRange(0, 65535);
16-
ui->lineEditPort->setValue(_port);
17+
ui->lineEditPort->setValue(params.ServicePort);
18+
19+
ui->comboBoxIp->addItem("0.0.0.0");
20+
for(auto&& nic : QNetworkInterface::allInterfaces())
21+
{
22+
if(!(nic.flags() & QNetworkInterface::IsRunning))
23+
{
24+
continue;
25+
}
26+
27+
for(auto&& entry : nic.addressEntries())
28+
{
29+
const auto addr = entry.ip();
30+
31+
if(addr.isNull() || addr.isMulticast() || addr.protocol() != QAbstractSocket::IPv4Protocol)
32+
{
33+
continue;
34+
}
35+
36+
ui->comboBoxIp->addItem(addr.toString());
37+
}
38+
}
39+
40+
ui->comboBoxIp->setCurrentText(_params.IPAddress);
1741
}
1842

1943
///
@@ -29,6 +53,7 @@ DialogSelectServicePort::~DialogSelectServicePort()
2953
///
3054
void DialogSelectServicePort::accept()
3155
{
32-
_port = ui->lineEditPort->value<int>();
56+
_params.ServicePort = ui->lineEditPort->value<int>();
57+
_params.IPAddress = ui->comboBoxIp->currentText();
3358
QFixedSizeDialog::accept();
3459
}

omodsim/dialogs/dialogselectserviceport.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define DIALOGSELECTSERVICEPORT_H
33

44
#include "qfixedsizedialog.h"
5+
#include "connectiondetails.h"
56

67
namespace Ui {
78
class DialogSelectServicePort;
@@ -12,14 +13,14 @@ class DialogSelectServicePort : public QFixedSizeDialog
1213
Q_OBJECT
1314

1415
public:
15-
explicit DialogSelectServicePort(quint16& port, QWidget *parent = nullptr);
16+
explicit DialogSelectServicePort(TcpConnectionParams& params, QWidget *parent = nullptr);
1617
~DialogSelectServicePort();
1718

1819
void accept() override;
1920

2021
private:
2122
Ui::DialogSelectServicePort *ui;
22-
quint16& _port;
23+
TcpConnectionParams& _params;
2324
};
2425

2526
#endif // DIALOGSELECTSERVICEPORT_H

omodsim/dialogs/dialogselectserviceport.ui

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,20 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>240</width>
9+
<width>328</width>
1010
<height>116</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
14-
<string>Select Service Port</string>
14+
<string>Select Service IP Address and Port</string>
1515
</property>
1616
<layout class="QVBoxLayout" name="verticalLayout">
1717
<item>
18-
<widget class="QLabel" name="label">
19-
<property name="text">
20-
<string>Modbus/TCP Service Port</string>
18+
<layout class="QFormLayout" name="formLayout">
19+
<property name="labelAlignment">
20+
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
2121
</property>
22-
<property name="alignment">
23-
<set>Qt::AlignCenter</set>
24-
</property>
25-
</widget>
26-
</item>
27-
<item>
28-
<layout class="QHBoxLayout" name="horizontalLayout">
29-
<item>
30-
<spacer name="horizontalSpacer">
31-
<property name="orientation">
32-
<enum>Qt::Horizontal</enum>
33-
</property>
34-
<property name="sizeHint" stdset="0">
35-
<size>
36-
<width>40</width>
37-
<height>20</height>
38-
</size>
39-
</property>
40-
</spacer>
41-
</item>
42-
<item>
22+
<item row="1" column="1">
4323
<widget class="NumericLineEdit" name="lineEditPort">
4424
<property name="sizePolicy">
4525
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
@@ -58,18 +38,25 @@
5838
</property>
5939
</widget>
6040
</item>
61-
<item>
62-
<spacer name="horizontalSpacer_2">
63-
<property name="orientation">
64-
<enum>Qt::Horizontal</enum>
41+
<item row="1" column="0">
42+
<widget class="QLabel" name="label">
43+
<property name="text">
44+
<string>Modbus/TCP Service Port</string>
6545
</property>
66-
<property name="sizeHint" stdset="0">
67-
<size>
68-
<width>40</width>
69-
<height>20</height>
70-
</size>
46+
<property name="alignment">
47+
<set>Qt::AlignCenter</set>
7148
</property>
72-
</spacer>
49+
</widget>
50+
</item>
51+
<item row="0" column="1">
52+
<widget class="QComboBox" name="comboBoxIp"/>
53+
</item>
54+
<item row="0" column="0">
55+
<widget class="QLabel" name="label_2">
56+
<property name="text">
57+
<string>Service IP Address</string>
58+
</property>
59+
</widget>
7360
</item>
7461
</layout>
7562
</item>

omodsim/mainwindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ void MainWindow::on_connectAction(ConnectionDetails& cd)
453453
{
454454
case ConnectionType::Tcp:
455455
{
456-
DialogSelectServicePort dlg(cd.TcpParams.ServicePort, this);
456+
DialogSelectServicePort dlg(cd.TcpParams, this);
457457
if(dlg.exec() == QDialog::Accepted) _mbMultiServer.connectDevice(cd);
458458
}
459459
break;

omodsim/menuconnect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool MenuConnect::canConnect(const ConnectionDetails& cd)
7979
{
8080
if(c.Type != cd.Type) continue;
8181
if(c.Type == ConnectionType::Tcp ||
82-
(c.Type == ConnectionType::Serial && c.SerialParams.PortName == cd.SerialParams.PortName))
82+
(c.Type == ConnectionType::Serial && c.SerialParams.PortName == cd.SerialParams.PortName))
8383
{
8484
return true;
8585
}

0 commit comments

Comments
 (0)