Skip to content

Commit

Permalink
Merge pull request #38 from NikolasK-source/main
Browse files Browse the repository at this point in the history
update to 1.6.0
  • Loading branch information
NikolasK-source committed Mar 31, 2024
2 parents a27b516 + db00e1f commit 7b239ac
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 63 deletions.
12 changes: 0 additions & 12 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,12 +0,0 @@
[submodule "libs/cxxopts"]
path = libs/cxxopts
url = https://github.com/NikolasK-source/cxxopts.git
[submodule "libs/libmodbus"]
path = libs/libmodbus
url = https://github.com/stephane/libmodbus
[submodule "libs/cxxshm"]
path = libs/cxxshm
url = https://github.com/NikolasK-source/cxxshm.git
[submodule "libs/cxxsemaphore"]
path = libs/cxxsemaphore
url = https://github.com/NikolasK-source/cxxsemaphore.git
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cmake_minimum_required(VERSION 3.22.1 FATAL_ERROR)
# ======================================================================================================================

# project
project(Modbus_TCP_client_shm LANGUAGES CXX VERSION 1.5.3)
project(Modbus_TCP_client_shm LANGUAGES CXX VERSION 1.6.0)

# settings
set(Target "modbus-tcp-client-shm") # Executable name (without file extension!)
Expand Down
83 changes: 47 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,67 @@

Modbus tcp client that stores its data (registers) in shared memory objects.

## Dependencies
- cxxopts by jarro2783 (https://github.com/jarro2783/cxxopts) (only required for building the application)
- libmodbus by Stéphane Raimbault (https://github.com/stephane/libmodbus)
- cxxshm (https://github.com/NikolasK-source/cxxshm)
- cxxsemaphore (https://github.com/NikolasK-source/cxxsemaphore)

On Arch linux they are available via the official repositories and the AUR:
- https://archlinux.org/packages/extra/any/cxxopts/
- https://aur.archlinux.org/packages/libmodbus
- https://aur.archlinux.org/packages/cxxshm
- https://aur.archlinux.org/packages/cxxsemaphore

## Build
```
git submodule init
git submodule update
mkdir build
cd build
cmake .. -DCMAKE_CXX_COMPILER=$(which clang++) -DCMAKE_BUILD_TYPE=Release -DCLANG_FORMAT=OFF -DCOMPILER_WARNINGS=OFF
cmake -B build -DCMAKE_CXX_COMPILER=$(which clang++) -DCMAKE_BUILD_TYPE=Release -DCLANG_FORMAT=OFF -DCLANG_TIDY=OFF -DCOMPILER_WARNINGS=OFF -DBUILD_DOC=OFF
cmake --build .
```

As an alternative to the ```git submodule``` commands, the ```--recursive``` option can be used with ```git clone```.

## Use
```
modbus-tcp-client-shm [OPTION...]
-i, --ip arg ip to listen for incoming connections (default: 0.0.0.0)
-p, --port arg port to listen for incoming connections (default: 502)
-n, --name-prefix arg shared memory name prefix (default: modbus_)
network options:
-i, --host arg host to listen for incoming connections (default: any)
-p, --service arg service or port to listen for incoming connections (default: 502)
-c, --connections arg number of allowed simultaneous Modbus Server connections. (default: 1)
-r, --reconnect do not terminate if no Modbus Server is connected anymore.
-t, --tcp-timeout arg tcp timeout in seconds. Set to 0 to use the system defaults (not recommended). (default: 5)
shared memory options:
-n, --name-prefix arg shared memory name prefix (default: modbus_)
--force Force the use of the shared memory even if it already exists. Do not use this option per default! It should only be used if the shared memory of an improperly terminated instance continues to exist as an orphan
and is no longer used.
-s, --separate arg Use a separate shared memory for requests with the specified client id. The client id (as hex value) is appended to the shared memory prefix (e.g. modbus_fc_DO). You can specify multiple client ids by
separating them with ','. Use --separate-all to generate separate shared memories for all possible client ids.
--separate-all like --separate, but for all client ids (creates 1028 shared memory files! check/set 'ulimit -n' before using this option.)
--semaphore arg protect the shared memory with a named semaphore against simultaneous access
--semaphore-force Force the use of the semaphore even if it already exists. Do not use this option per default! It should only be used if the semaphore of an improperly terminated instance continues to exist as an orphan and is
no longer used.
-b, --permissions arg permission bits that are applied when creating a shared memory. (default: 0640)
modbus options:
--do-registers arg number of digital output registers (default: 65536)
--di-registers arg number of digital input registers (default: 65536)
--ao-registers arg number of analog output registers (default: 65536)
--ai-registers arg number of analog input registers (default: 65536)
-m, --monitor output all incoming and outgoing packets to stdout
-r, --reconnect do not terminate if the Modbus Server disconnects.
--byte-timeout arg timeout interval in seconds between two consecutive bytes of the same message. In most
cases it is sufficient to set the response timeout. Fractional values are possible.
--response-timeout arg set the timeout interval in seconds used to wait for a response. When a byte timeout is
set, if the elapsed time for the first byte of response is longer than the given timeout,
a timeout is detected. When byte timeout is disabled, the full confirmation response must
be received before expiration of the response timeout. Fractional values are possible.
-t, --tcp-timeout arg tcp timeout in seconds. Set to 0 to use the system defaults (not recommended). (default:
5)
--force Force the use of the shared memory even if it already exists. Do not use this option per
default! It should only be used if the shared memory of an improperly terminated instance
continues to exist as an orphan and is no longer used.
-s, --separate arg Use a separate shared memory for requests with the specified client id. The client id
(as hex value) is appended to the shared memory prefix (e.g. modbus_fc_DO). You can
specify multiple client ids by separating them with ','. Use --separate-all to generate
separate shared memories for all possible client ids.
--separate-all like --separate, but for all client ids (creates 1028 shared memory files! check/set
'ulimit -n' before using this option.)
-h, --help print usage
--version print version information
--license show licences
--byte-timeout arg timeout interval in seconds between two consecutive bytes of the same message. In most cases it is sufficient to set the response timeout. Fractional values are possible.
--response-timeout arg set the timeout interval in seconds used to wait for a response. When a byte timeout is set, if the elapsed time for the first byte of response is longer than the given timeout, a timeout is detected. When
byte timeout is disabled, the full confirmation response must be received before expiration of the response timeout. Fractional values are possible.
other options:
-h, --help print usage
--license show licences (short)
--license-full show licences (full license text)
version information options:
--version print version and exit
--longversion print version (including compiler and system info) and exit
--shortversion print version (only version string) and exit
--git-hash print git hash
The modbus registers are mapped to shared memory objects:
Expand All @@ -66,8 +82,3 @@ The following example redirects packets from port 502 (standard modbus port) to
iptables -A PREROUTING -t nat -p tcp --dport 502 -j REDIRECT --to-port 5020
```
The modbus client must be called with the option ```-p 5020```

## Libraries
This application uses the following libraries:
- cxxopts by jarro2783 (https://github.com/jarro2783/cxxopts)
- libmodbus by Stéphane Raimbault (https://github.com/stephane/libmodbus)
20 changes: 11 additions & 9 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# ---------------------------------------- packages --------------------------------------------------------------------
# ======================================================================================================================
find_package(cxxshm REQUIRED)
find_package(cxxsemaphore REQUIRED)
find_package(cxxopts REQUIRED)

# ---------------------------------------- external libs ---------------------------------------------------------------
# ======================================================================================================================
find_library(modbus_library modbus)

# ---------------------------------------- subdirectories --------------------------------------------------------------
# ======================================================================================================================
include(../modbus.cmake)
target_link_libraries(${Target} PRIVATE modbus)

# ---------------------------------------- link libraries --------------------------------------------------------------
# ======================================================================================================================
target_link_libraries(${Target} PRIVATE ${modbus_library})
target_link_libraries(${Target} PRIVATE rt)

add_subdirectory(cxxopts EXCLUDE_FROM_ALL)
target_link_libraries(${Target} PRIVATE cxxopts)

add_subdirectory(cxxshm EXCLUDE_FROM_ALL)
target_link_libraries(${Target} PRIVATE INTERFACE cxxopts)
target_link_libraries(${Target} PRIVATE cxxshm)

add_subdirectory(cxxsemaphore EXCLUDE_FROM_ALL)
target_link_libraries(${Target} PRIVATE cxxsemaphore)
1 change: 0 additions & 1 deletion libs/cxxopts
Submodule cxxopts deleted from c74846
1 change: 0 additions & 1 deletion libs/cxxsemaphore
Submodule cxxsemaphore deleted from c5aec9
1 change: 0 additions & 1 deletion libs/cxxshm
Submodule cxxshm deleted from fc145c
1 change: 0 additions & 1 deletion libs/libmodbus
Submodule libmodbus deleted from 2cbafa
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ int main(int argc, char **argv) {
cxxopts::ParseResult args;
try {
args = options.parse(argc, argv);
} catch (cxxopts::OptionParseException &e) {
} catch (cxxopts::exceptions::parsing::exception &e) {
std::cerr << Print_Time::iso << " ERROR: Failed to parse arguments: " << e.what() << ".'\n";
return exit_usage();
}
Expand Down

0 comments on commit 7b239ac

Please sign in to comment.