title: "Modbus" url: doc/communication/modbus.html ---

# Modbus

## Installation Instructions

The Modbus com interface need the libmodbus library to work. Follow the steps below to set up your environment before compiling 4diac FORTE.

1. libmodbus files should be placed in the following folder structure:

   - `[libmodbushome]/include/modbus`: include files

   - `[libmodbushome]/lib`: `libmodbus.lib` (windows) or `libmodbus.so` (posix)

2. When configuring 4diac FORTE project with CMake choose `[libmodbushome]` as `FORTE_COM_MODBUS_LIB_DIR`

3. `forte.exe` must be able to find the dynamic libmodbus library after build. Therefore copy `libmodbus.dll/so` to folder with `forte.exe` or add `libmodbus.dll/so` to library include path.

## Parameters

### Modbus Client (TCP)

Usage of the Modbus Client (TCP):

    modbus[(protocol:)ip:port:(slaveId):pollFrequency:readAddresses:sendAddresses(:responseTimeout:byteTimeout)]

- protocol: tcp (tcp is default)

- ip: address of Modbus server, e.g. 127.0.0.1

- port: preferably above 1024, default is 502

- slaveId (optional): the slave id used by the modbus server (0xFF is standard); this is in principle an optional parameter but some modbus devices (e.g. Siemens SENTRON PAC power measurement devices) need this parameter in order to establish a connection. Also if you use a Modbus-RTU to Modbus-TCP Gateway like for Example EX9133C-2-MTCP then this slaveID does tell which Device on the RTU Side you want to connect to.

- pollFrequency: polling frequency in milliseconds

- readAddresses: addresses can be specified between 0-65535 more than one address (max 100) can be specified using

  - comma for separate addresses 0,2,65500

  - dots for interval 5..10

  - combination 0,5..10,2,65500

  - function is by default holding register, and can be changed with a prefixed letter

    - 'c' for coil

    - 'd' for discrete input

    - 'h' for holding register

    - 'i' for input register

    - -→ see below for a Detailed Table

- sendAddresses: addresses can be specified between 0-65535 if data is only read sendAddresses should be left empty more than one address (max 100) can be specified using

  - comma for separate addresses 0,2,65500

  - dots for interval 5..10

  - combination 0,5..10,2,65500

  - function can be selected like for readAddresses

- responseTimeout (optional): timeout in milliseconds to wait for a response (500ms is default)

- byteTimeout (optional): timeout in milliseconds between two consecutive bytes (500ms is default)

- to reuse a previous connection define only ip and port and leave everything up to slaveId empty

Example:

    modbus[tcp:127.0.0.1:502::20000:h0..3:]

### Modbus Client (RTU)

Usage of the Modbus Client (RTU):

    modbus[protocol:port:baudrate:parity:databits:stopbits:flow:(slaveId):pollFrequency:readAddresses:sendAddresses(:responseTimeout:byteTimeout)]

- protocol: rtu

- port: serial port, e.g., /dev/ttyS0 or COM1

- baudrate: serial port baudrate, e.g., 9600

- parity: N for none, E for even, O for odd

- databits: number of data bits, e.g., 8

- stopbits: number of stop bits, e.g., 1

- flow:

  - leave empty for none

  - arduino - disable DTR and wait for Arduino to boot just in case

  - delay - wait 2 seconds after connecting

  - longdelay - wait 3 seconds after connecting

- all other paramters are as for TCP

  - except the slaveId, while it is optional in some cases for Modbus-TCP, is is mandatory for Modbus-RTU, and in most cases the factory default of devices is 1

- to reuse a previous connection define only port and leave everything up to slaveId empty

Example:

    modbus[rtu:/dev/ttyUSB0:19200:N:8:1::1:2000:i6142,i6132,i6137:h64025..64028,h63995..63998]

## Table of Functions:

|  | read | write
--- | --- | --- | ---
c | coil | modbus_read_bits 0x01 (read coil status) | modbus_write_bits 0x0F (force multiple coils)
d | discrete input | modbus_read_input_bits 0x02 (read input status) |
h | holding register | modbus_read_registers 0x03 (read holding registers) | modbus_write_registers 0x10 (preset multiple registers)
i | input register | modbus_read_input_registers 0x04 (read input registers) |

## Example: Writing Outputs of a 8-Output Relay Card:

    modbus[rtu:/dev/uart/1:9600:N:8:1::1:2000:c0..7:c0..7]

## Where to go from here?

- Go back to Protocols index:
  Communication Index

- If you want to go back to the Start Here page, we leave you here a fast access
  Start Here page

Or Go to top