Skip to main content

Example programs

The quickest way to see the library work is to run its example programs: a client and a server in each language, which you drive interactively from the console.

Run a server and a client​

Clone the repository, check out the tag for this version (1.6.0-RC1), and from its root start a server and a client on the same machine. In Rust:

cargo run -p example-server -- tcp 127.0.0.1:10502
cargo run -p example-client -- tcp 127.0.0.1:10502

Then type help in either one to list the commands that change the server's values or send requests from the client.

The C, C++, Java and .NET examples build with each language's usual tools (CMake, Maven, and the .NET SDK) against the published library for the tag's version, so you don't need to build the library from source.

Transports and arguments​

Each language's client and server behave the same way and accept the same commands. Run them from the root of the repository, so that they find the TLS certificates in ./certs. Both take the transport as their first argument, tcp, rtu, tls-ca or tls-self-signed, and an optional second argument: the endpoint to listen on or connect to, or the serial port. Defaults such as the unit ID are constants at the top of each program.

Without an endpoint, the servers listen on the standard Modbus ports, 502 for TCP and 802 for TLS, which on Linux need elevated privileges; that's why the commands above pass another one.

For RTU, the client opens /dev/ttySIM0 and the server /dev/ttySIM1; pass the paths of any two connected serial ports instead, such as a pair of virtual ports.

Source​

The examples for each language, for this version:

The code snippets throughout this guide are pulled from these programs when the guide is built, so they always compile and stay up to date.