Skip to content

Commit acdb4e0

Browse files
committed
Finish ip-connection
2 parents 1d17bb9 + 149dbb2 commit acdb4e0

8 files changed

Lines changed: 121 additions & 70 deletions

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: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ MainStatusBar::MainStatusBar(const ModbusMultiServer& server, QWidget* parent)
4545
auto label = new QLabel(this);
4646
label->setFrameShadow(QFrame::Sunken);
4747
label->setFrameShape(QFrame::Panel);
48-
label->setMinimumWidth(120);
48+
label->setMinimumWidth(80);
4949
label->setProperty("ConnectionDetails", QVariant::fromValue(cd));
5050

5151
updateConnectionInfo(label, cd);
@@ -60,9 +60,8 @@ MainStatusBar::MainStatusBar(const ModbusMultiServer& server, QWidget* parent)
6060
{
6161
if(cd == label->property("ConnectionDetails").value<ConnectionDetails>())
6262
{
63-
_labels.removeOne(label);
6463
removeWidget(label);
65-
delete label;
64+
_labels.removeOne(label);
6665

6766
break;
6867
}
@@ -107,15 +106,15 @@ void MainStatusBar::updateConnectionInfo(QLabel* label, const ConnectionDetails&
107106
switch(cd.Type)
108107
{
109108
case ConnectionType::Tcp:
110-
label->setText(QString(tr("Modbus/TCP Srv: %1")).arg(cd.TcpParams.ServicePort));
109+
label->setText(QString(tr("Modbus/TCP Srv %1:%2")).arg(cd.TcpParams.IPAddress, QString::number(cd.TcpParams.ServicePort)));
111110
break;
112111

113112
case ConnectionType::Serial:
114-
label->setText(QString(tr("Port %1:%2:%3:%4:%5 ")).arg(cd.SerialParams.PortName,
115-
QString::number(cd.SerialParams.BaudRate),
116-
QString::number(cd.SerialParams.WordLength),
117-
Parity_toString(cd.SerialParams.Parity),
118-
QString::number(cd.SerialParams.StopBits)));
113+
label->setText(QString(tr("Port %1:%2:%3:%4:%5")).arg(cd.SerialParams.PortName,
114+
QString::number(cd.SerialParams.BaudRate),
115+
QString::number(cd.SerialParams.WordLength),
116+
Parity_toString(cd.SerialParams.Parity),
117+
QString::number(cd.SerialParams.StopBits)));
119118
break;
120119
}
121120
}

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: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,66 @@ MenuConnect::MenuConnect(MenuType type, ModbusMultiServer& server, QWidget *pare
1313
,_menuType(type)
1414
,_mbMultiServer(server)
1515
{
16-
addAction(tr("Modbus/TCP Srv"), ConnectionType::Tcp, QString());
17-
18-
for(auto&& port: getAvailableSerialPorts())
16+
if(type == MenuType::ConnectMenu)
1917
{
20-
const auto text = QString(tr("Port %1")).arg(port);
21-
addAction(text, ConnectionType::Serial, port);
18+
addAction(tr("Modbus/TCP Srv"), ConnectionType::Tcp, QString());
19+
for(auto&& port: getAvailableSerialPorts())
20+
{
21+
const auto text = QString(tr("Port %1")).arg(port);
22+
addAction(text, ConnectionType::Serial, port);
23+
}
2224
}
2325

24-
connect(&_mbMultiServer, &ModbusMultiServer::connected, this, [&](const ConnectionDetails&)
26+
connect(&_mbMultiServer, &ModbusMultiServer::connected, this, [&](const ConnectionDetails& cd)
2527
{
26-
for(auto&& a : actions())
28+
if(_menuType == MenuType::DisconnectMenu)
2729
{
28-
const auto data = a->data().value<QPair<ConnectionType, QString>>();
29-
const bool isConnected = _mbMultiServer.isConnected(data.first, data.second);
30-
a->setEnabled(_menuType == ConnectMenu ? !isConnected : isConnected);
30+
switch(cd.Type)
31+
{
32+
case ConnectionType::Tcp:
33+
{
34+
const auto port = QString("%1:%2").arg(cd.TcpParams.IPAddress, QString::number(cd.TcpParams.ServicePort));
35+
addAction(QString(tr("Modbus/TCP Srv %1")).arg(port), ConnectionType::Tcp, port);
36+
}
37+
break;
38+
39+
case ConnectionType::Serial:
40+
addAction(QString(tr("Port %1")).arg(cd.SerialParams.PortName), ConnectionType::Serial, cd.SerialParams.PortName);
41+
break;
42+
}
43+
}
44+
else
45+
{
46+
for(auto&& a : actions())
47+
{
48+
const auto data = a->data().value<QPair<ConnectionType, QString>>();
49+
const bool isConnected = _mbMultiServer.isConnected(data.first, data.second);
50+
a->setEnabled(_menuType == ConnectMenu ? !isConnected : isConnected);
51+
}
3152
}
3253
});
3354

34-
connect(&_mbMultiServer, &ModbusMultiServer::disconnected, this, [&](const ConnectionDetails&)
55+
connect(&_mbMultiServer, &ModbusMultiServer::disconnected, this, [&](const ConnectionDetails& cd)
3556
{
3657
for(auto&& a : actions())
3758
{
3859
const auto data = a->data().value<QPair<ConnectionType, QString>>();
39-
const bool isConnected = _mbMultiServer.isConnected(data.first, data.second);
40-
a->setEnabled(_menuType == ConnectMenu ? !isConnected : isConnected);
60+
if(_menuType == MenuType::DisconnectMenu)
61+
{
62+
const auto port = (data.first == ConnectionType::Tcp) ?
63+
QString("%1:%2").arg(cd.TcpParams.IPAddress, QString::number(cd.TcpParams.ServicePort)) :
64+
cd.SerialParams.PortName;
65+
66+
if(data.first == cd.Type && data.second == port)
67+
{
68+
removeAction(a);
69+
}
70+
}
71+
else
72+
{
73+
const bool isConnected = _mbMultiServer.isConnected(data.first, data.second);
74+
a->setEnabled(_menuType == ConnectMenu ? !isConnected : isConnected);
75+
}
4176
}
4277
});
4378
}
@@ -79,7 +114,7 @@ bool MenuConnect::canConnect(const ConnectionDetails& cd)
79114
{
80115
if(c.Type != cd.Type) continue;
81116
if(c.Type == ConnectionType::Tcp ||
82-
(c.Type == ConnectionType::Serial && c.SerialParams.PortName == cd.SerialParams.PortName))
117+
(c.Type == ConnectionType::Serial && c.SerialParams.PortName == cd.SerialParams.PortName))
83118
{
84119
return true;
85120
}
@@ -140,5 +175,4 @@ void MenuConnect::addAction(const QString& text, ConnectionType type, const QStr
140175

141176
const auto data = QPair<ConnectionType, QString>(type, port);
142177
action->setData(QVariant::fromValue(data));
143-
action->setEnabled(_menuType == ConnectMenu);
144178
}

omodsim/modbusmultiserver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ QSharedPointer<QModbusServer> ModbusMultiServer::findModbusServer(ConnectionType
9494
for(auto&& s : _modbusServerList)
9595
{
9696
const auto cd = s->property("ConnectionDetails").value<ConnectionDetails>();
97-
if((cd.Type == ConnectionType::Tcp && type == ConnectionType::Tcp) ||
98-
(cd.Type == ConnectionType::Serial && type == ConnectionType::Serial && cd.SerialParams.PortName == port))
97+
if((cd.Type == ConnectionType::Tcp && type == ConnectionType::Tcp && port == QString("%1:%2").arg(cd.TcpParams.IPAddress, QString::number(cd.TcpParams.ServicePort))) ||
98+
(cd.Type == ConnectionType::Serial && type == ConnectionType::Serial && port == cd.SerialParams.PortName))
9999
{
100100
return s;
101101
}

0 commit comments

Comments
 (0)