|
| 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. |
0 commit comments