Skip to main content

Modbus TCP

The Modbus TCP driver reads and writes registers and coils of Modbus TCP devices: PLCs, meters, drives and Modbus gateways. For what all drivers have in common, see How drivers work.

Protocol and role​

RoleClient (master). YTCC connects to the devices; it does not accept Modbus connections.
TransportModbus TCP.
Default port502
Devices per driverAny number. Devices with the same host and port, for example several units behind one Modbus gateway, share one TCP connection.
Driver typeModbus TCP (modbus_tcp)

Device parameters​

Set these on the device in Drivers → devices (see Add drivers and devices). An empty box uses the default shown in it.

Device form of a Modbus TCP device with Name, Description, Active, Gap tolerance regs, Host, Max block regs, Port, Set invalid on disconnect, Timeout (ms) and Unit ID.

ParameterDefaultRangeMeaning
Host— (required)IP address or host nameAddress of the device or of the Modbus gateway. A host name is resolved through DNS.
Port5021–65535TCP port of the device.
Unit ID10–255Modbus unit identifier (slave address). Devices usually use 1–247; behind a gateway it selects the device.
Timeout (ms)10001 and moreHow long the driver waits for each answer, for reading and for writing.
Gap tolerance regs100–65535How many unused registers may lie between two signals before the driver reads them in separate requests.
Max block regs1251–125Most registers in one read request (function codes 3 and 4).
Set invalid on disconnectonon / offWhen the connection is lost, mark all signals of the device as Bad · Not connected. When off, the signals keep their last value and quality.

Changing Host, Port, Unit ID, Timeout (ms) or Set invalid on disconnect reconnects that device. Changing Gap tolerance regs or Max block regs takes effect without dropping the connection.

When you import devices from a file, the host, port, unit ID and timeout come from the file.

Signal address​

Set the address on the Value source tab of the signal, under Address — Modbus TCP.

Value source tab of the signal Station.OutletFlow with Register Holding register, Address (0-based) 1, FC read 3, Quantity 1, Word order (device) and Data kind UInt16.

FieldValuesMeaning
RegisterHolding register, Input register, Coil, Discrete inputThe register table.
Address (0-based)0–65535The address as it is sent to the device. Holding register 40001 is address 0.
FC read3, 4, 1, 2Function code for reading: 3 holding registers, 4 input registers, 1 coils, 2 discrete inputs.
FC writeemptyLeave it empty. The value is written to the address that is read; see Writing.
Quantity1 and moreNumber of registers the value takes.
Word order(device), Big endian, Little endian, Word swappedOrder of the registers of a 32- or 64-bit value. See below.
Data kind(from signal type), Bit, Int16 … Float64, Text, Bit fieldHow the registers are read. See Data kinds.

Read now reads the address once from the device and shows the value or the error; nothing is written. Use it to check an address while you set it up.

Address syntax in import files​

When you import signals from a file (see Import and export with Excel or CSV), write the address in the address column in one of these forms:

FormExampleResult
Register word and 0-based addressholding 1, input 0, coil 0, discrete 5Holding register 1, input register 0, coil 0, discrete input 5
Short wordshr, ir, co, di, or 4x, 3x, 0x, 1xSame as the words above
1-based Modbus number, 5 or 6 digits40001, 400001, 30010, 00001, 10001First digit: 4 holding, 3 input, 0 coil, 1 discrete; the rest minus 1 is the address

A bare number such as 200 is rejected. The import sets the read function code and the quantity from the register and the data type (2 registers for 32-bit types, 4 for 64-bit types).

Word order​

Word orderByte order of a 32-bit value A B C D
Big endian (default)A B C D, the Modbus standard
Word swappedC D A B
Little endianD C B A

(device) uses big endian. The fourth order, byte swapped (B A D C), is set in Advanced: raw params (JSON) ("word_order": "BADC").

Data kinds​

Data kindRegistersValue
Bit1 coil or discrete inputOn or off
Int16, UInt161Signed or unsigned 16-bit integer
Int32, UInt32, Float32232-bit integer or floating-point number
Int64, UInt64, Float64464-bit integer or floating-point number
TextQuantityText, two characters per register
Bit field1Several bits of a register, as a number. Set the first bit and the number of bits in Advanced: raw params (JSON) (bit_offset, bit_count).

(from signal type) uses the data type of the signal. A scale and offset set on the signal are applied after reading (value = raw × scale + offset); the raw register value stays available as the raw value of the signal.

Under Filter and deadband you can suppress small changes (Deadband), set a Min interval (ms) between two values, smooth the value and reject spikes. These settings apply to every driver.

Reading​

The driver polls. It groups the signals of a device by function code and scan period, and reads neighbouring registers in one request: up to Max block regs registers (120 when a 32- or 64-bit value is in the block) and up to 2000 coils or discrete inputs, as long as no gap is larger than Gap tolerance regs. When the connection opens, it reads every block once; the device counts as connected after the first good answer.

The scan class of the signal sets how often each block is read; see How often values are read.

Writing​

A signal can be written when its Access is Read / Write or Write only. The driver chooses the function code from the value:

ValueFunction code
Coil (Bit)5 Write single coil
16-bit register6 Write single register
32- and 64-bit values, text16 Write multiple registers
  • The value is written to the address that is read, so give a writable signal a Register, an Address and an FC read.
  • A signal with data kind Bit is written as a coil (function code 5). Make it writable only when its Register is Coil.
  • Bit, Int16 … Float64 and Text values can be written.
  • If the device is not connected, the command is rejected at once. A command waiting for a device that goes offline is cancelled, not sent later.
  • Each device accepts up to 4 waiting commands; more are rejected as Previous command in progress.
  • If the device answers with an exception, the command is rejected with Device returned an error; the connection stays up.
  • If the signal is set to read back after writing, the driver reads the value again right after a successful write.

For how operators send and confirm commands, see Send commands.

Quality mapping​

SituationQuality of the signals
Read succeededGood
The answer is too short or cannot be convertedBad for that signal
Exception 02 (illegal data address)The driver splits the request until it finds the rejected address; that signal is Bad, the others are read normally
Other exceptions (01, 03, 04 …)Bad for the signals of that request; the connection stays up
Timeout, connection error, exception 05 or 06The connection is closed and opened again; with Set invalid on disconnect, all signals of the device are Bad · Not connected

Read the value together with its quality. Do not use a value whose quality is Bad or Bad · Not connected; see Read values and states.

Time stamps​

Modbus does not carry the time of a value. The time a value arrives in YTCC is used for both time stamps, and the signal details show Time source: protocol carries no time.

Diagnostics​

  • Device status. The device is shown as connected after the first good answer and as not connected when the connection is lost. A device with an invalid setting does not connect and shows the reason.
  • Communication alarm. One alarm per device after 5 seconds without connection; see How drivers work.
  • Read now. On the Value source tab, Read now shows the last read error of the signal, for example Modbus exception 02 — Illegal data address (holding 102, unit 3, FC3) or Timeout — no response from 10.0.0.30:502 (unit 1) within 1000 ms.
  • Signal details. On the runtime screen, the signal details show the driver, the device and the protocol address, for example holding 1. See Look at the details of a signal.
SymptomLikely cause
All signals of the device Bad · Not connectedWrong host or port, device off, firewall, or the unit does not answer within Timeout (ms)
One signal Bad, the others goodAddress outside the device's register map (exception 02), or wrong quantity
32-bit value looks wrong but changesWrong Word order
Value off by a registerThe device documentation counts from 1: use address minus 1
Command rejected with Signal not foundThe signal is not writable; set Access to rw on the signal

Supported function codes​

CodeFunctionSupported
1Read coilsYes
2Read discrete inputsYes
3Read holding registersYes
4Read input registersYes
5Write single coilYes
6Write single registerYes
15Write multiple coilsYes, not used for single values
16Write multiple registersYes

The driver recognises exception codes 01–08, 10 and 11 and shows their names in the error text.