Skip to content

Commit 2077f47

Browse files
Merge branch 'main' into release
Release 1.0.3
2 parents c8f288d + b8ba536 commit 2077f47

15 files changed

+739
-31
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# general
22
build*
3-
docs/
3+
docs/*
4+
5+
!docs/index.md
6+
!docs/_config.yml
47

58
# editor files
69

@@ -462,3 +465,5 @@ modules.order
462465
Module.symvers
463466
Mkfile.old
464467
dkms.conf
468+
469+
!network.koesling.modbus_tcp_client_shm.yml

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.13.4 FATAL_ERROR)
44
# ======================================================================================================================
55

66
# project
7-
project(Modbus_TCP_client_shm LANGUAGES CXX VERSION 1.0.2)
7+
project(Modbus_TCP_client_shm LANGUAGES CXX VERSION 1.0.3)
88

99
# settings
1010
set(Target "Modbus_TCP_client_shm") # Executable name (without file extension!)

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Nikolas Koesling
3+
Copyright (c) 2021-2022 Nikolas Koesling
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ The modbus registers are mapped to shared memory objects:
3939
AI | Discrete Input Registers | read-only | <name-prefix>AI
4040
```
4141

42+
### Use privileged ports
43+
The standard modbus port (502) can be used only by the root user under linux by default.
44+
To circumvent this, you can create an entry in the iptables that redirects packets on the standard modbus port to a higher port.
45+
The following example redirects packets from port 502 (standard modbus port) to port 5020
46+
```
47+
iptables -A PREROUTING -t nat -p tcp --dport 502 -j REDIRECT --to-port 5020
48+
```
49+
The modbus client must be called with the option ```-p 5020```
50+
4251
## Libraries
4352
This application uses the following libraries:
4453
- cxxopts by jarro2783 (https://github.com/jarro2783/cxxopts)

docs/_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
theme: jekyll-theme-cayman

docs/index.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Shared Memory Modbus TCP Client
2+
3+
This project is a simple command line based Modbus TCP client for POXIX compatible operating systems that stores the contents of its registers in shared memory.
4+
5+
## Basic operating principle
6+
7+
The client creates four shared memories.
8+
One for each register type:
9+
- Discrete Output Coils (DO)
10+
- Discrete Input Coils (DI)
11+
- Discrete Output Registers (AO)
12+
- Discrete Input Registers (AI)
13+
14+
All registers are initialized with 0 at the beginning.
15+
The Modbus master reads and writes directly the values from these shared memories.
16+
17+
The actual functionality of the client is realized by applications that read data from or write data to the shared memory.
18+
19+
20+
## Use the Application
21+
The application can be started completely without command line arguments.
22+
In this case the client listens for connections on all IPs on port 502 (the default modbus port).
23+
The application terminates if the master disconnects.
24+
25+
The arguments ```--port``` and ```--ip``` can be used to specify port and ip to listen to.
26+
27+
By using the command line argument ```--monitor``` all incoming and outgoing packets are printed on stdout.
28+
This option should be used carefully, as it generates large amounts of output depending on the masters polling cycle and the number of used registers.
29+
30+
The ```--reconnect``` option can be used to specify that the application is not terminated when the master disconnects, but waits for a new connection.
31+
32+
### Use privileged ports
33+
Ports below 1024 cannot be used by standard users.
34+
Therefore, the default modbus port (502) cannot be used without further action.
35+
36+
Here are two ways to use the port anyway:
37+
#### iptables (recommended)
38+
An entry can be added to the iptables that forwards the packets on the actual port to a higher port.
39+
40+
The following example redirects all tcp packets on port 502 to port 5020:
41+
```
42+
iptables -A PREROUTING -t nat -p tcp --dport 502 -j REDIRECT --to-port 5020
43+
```
44+
45+
#### setcap
46+
The command ```setcap``` can be used to allow the application to access privileged ports.
47+
However, this option gives the application significantly more rights than it actually needs and should therefore be avoided.
48+
49+
This option cannot be used with flatpaks.
50+
51+
```
52+
setcap 'cap_net_bind_service=+ep' /path/to/binary
53+
```
54+
55+
56+
57+
## Build from Source
58+
59+
The following packages are required for building the application:
60+
- cmake
61+
- clang or gcc
62+
63+
Additionally, the following packages are required to build the modbus library:
64+
- autoconf
65+
- automake
66+
- libtool
67+
68+
69+
Use the following commands to build the application:
70+
```
71+
git clone --recursive https://github.com/NikolasK-source/modbus_tcp_client_shm.git
72+
cd modbus_tcp_client_shm
73+
mkdir build
74+
cmake -B build . -DCMAKE_BUILD_TYPE=Release -DCLANG_FORMAT=OFF -DCOMPILER_WARNINGS=OFF
75+
cmake --build build
76+
```
77+
78+
The binary is located in the build directory.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
id: network.koesling.modbus_tcp_client_shm
2+
runtime: org.freedesktop.Platform
3+
runtime-version: '21.08'
4+
sdk: org.freedesktop.Sdk
5+
command: Modbus_TCP_client_shm
6+
finish-args:
7+
- --device=shm
8+
- --share=network
9+
modules:
10+
- name: Modbus_TCP_client_shm
11+
buildsystem: simple
12+
build-commands:
13+
# build
14+
- mkdir build
15+
- cmake -B build . -DCMAKE_BUILD_TYPE=Release -DCLANG_FORMAT=OFF -DCOMPILER_WARNINGS=OFF
16+
- cmake --build build
17+
18+
# install
19+
- mkdir -p "${FLATPAK_DEST}/bin"
20+
- cp build/Modbus_TCP_client_shm ${FLATPAK_DEST}/bin
21+
- ls -lah ${FLATPAK_DEST}
22+
sources:
23+
- type: git
24+
branch: release
25+
url: https://github.com/NikolasK-source/modbus_tcp_client_shm.git
26+

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
target_sources(${Target} PRIVATE main.cpp)
55
target_sources(${Target} PRIVATE modbus_shm.cpp)
66
target_sources(${Target} PRIVATE Modbus_TCP_Slave.cpp)
7+
target_sources(${Target} PRIVATE license.cpp)
78

89

910
# ---------------------------------------- header files (*.jpp, *.h, ...) ----------------------------------------------
1011
# ======================================================================================================================
1112
target_sources(${Target} PRIVATE modbus_shm.hpp)
1213
target_sources(${Target} PRIVATE Modbus_TCP_Slave.hpp)
14+
target_sources(${Target} PRIVATE license.hpp)
1315

1416

1517

src/Modbus_TCP_Slave.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright (C) 2021-2022 Nikolas Koesling <nikolas@koesling.info>.
3+
* This program is free software. You can redistribute it and/or modify it under the terms of the MIT License.
4+
*/
5+
16
#include "Modbus_TCP_Slave.hpp"
27

38
#include <stdexcept>

src/Modbus_TCP_Slave.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright (C) 2021-2022 Nikolas Koesling <nikolas@koesling.info>.
3+
* This program is free software. You can redistribute it and/or modify it under the terms of the MIT License.
4+
*/
5+
16
#pragma once
27

38
#include <modbus/modbus.h>
@@ -9,7 +14,7 @@ namespace TCP {
914
//! Modbus TCP slave
1015
class Slave {
1116
private:
12-
modbus_t * modbus; //!< modbus object (see libmodbus library)
17+
modbus_t *modbus; //!< modbus object (see libmodbus library)
1318
modbus_mapping_t *mapping; //!< modbus data object (see libmodbus library)
1419
bool delete_mapping; //!< indicates whether the mapping object was created by this instance
1520
int socket = -1; //!< socket of the modbus connection
@@ -23,7 +28,7 @@ class Slave {
2328
*/
2429
explicit Slave(const std::string &ip = "0.0.0.0",
2530
short unsigned int port = 502,
26-
modbus_mapping_t * mapping = nullptr);
31+
modbus_mapping_t *mapping = nullptr);
2732

2833
/*! \brief destroy the modbus slave
2934
*

0 commit comments

Comments
 (0)