Skip to content

Read ​

Purpose: read registers or coils, compare them with the expected values, and optionally store what was read in variables.

Read compare settings

Compare has four choices:

ChoiceMeaning
No checkRead only, no compare. PASS if the device responds
= equalsPASS only if equal to Expected value
≠ not equalPASS only if not equal to Expected value
In rangePASS only if between the Pass when limits (inclusive)

Read store settings

Save to variable stores the values read in a variable, and Save as decides how:

ChoiceJSON (store_mode)Result
First valuefirst (default)Stores only the first value
Each valueeachStores each value separately: r_0, r_1, r_2…, and r itself equals r_0
32-bit, high firstu32_hi_firstCombines 2 registers into one 32-bit value, the first being the high word
32-bit, low firstu32_lo_firstSame, with the first being the low word

JSON ​

json
{ "read": { "function": 3, "address": "0x0100", "expected": 100 } }
{ "read": { "address": "0x0101", "compare_op": "!=", "expected": 7 } }
{ "read": { "address": "0x0100", "range": [90, 110] } }
{ "read": { "address": "0x0110", "count": 3, "store": "r", "store_mode": "each" } }
FieldRequiredMeaning
addressYesStart address. Expression allowed
end_address / countNoThe range to read, as in Write. Up to 123
functionNo1 coils, 2 discrete inputs, 3 holding registers (default), 4 input registers
formatNoDefault unsigned
expectedNoExpected value. A single value must match at every position; a list is compared value by value. Expression allowed
compare_opNo"==" (default) or "!=". "eq" and "ne" are also accepted
rangeNoRange: [low, high], {"min": low, "max": high}, or a string like "0x10-0x20" for Unsigned
toleranceNoAllowed difference (0–65535): equal if |actual − expected| ≤ tolerance
compareNoDefaults to true when expected or range is given. false reads without comparing
storeNoName of the variable to store into
store_modeNoSee the table above. Needs store
channel, max_msNoSee Common fields

Result ​

ResultWhen
PASSThe compare matches, or there is no compare and the device responds
FAILThe compare does not match, the device returns an exception code, or the response is slower than max_ms
DISCONNECTThe device does not respond

Example ​

CaseWhat it doesResult
Prepare dataWrites the data the later cases readPASS
Equal and not equal0x0100 equals 100; 0x0101 (value 0) is not 7PASS
Range and tolerance0x0100 (100) is within 90–110; equals 98 ± 5PASS
Several registers0x0110..0x0112 equal 10, 20, 30 in orderPASS
Save to variablesStores into v, r_0..r_2 and big, then prints them with LogPASS
Other function codesReads with FC04 and FC02PASS
Fails on purpose0x0102 is 5, expected 6FAIL

Read example result

The Log prints v = 100, r_0..r_2 = 10, 20, 30, big = 65538. big is built from 0x0001, 0x0002 with the high word first: 1 × 65536 + 2 = 65538. Overall the run is Release BLOCKED because the last case FAILs.

Notes ​

  • The screen has no separate tolerance field: JSON with "expected": 98, "tolerance": 5 opens on screen as In range 0x005D–0x0067 (93–103). Both give the same result.
  • With store_mode: "each", the variable name can be at most 27 characters (leaving room for _0 up to _122).
  • u32_hi_first/u32_lo_first only work with the Unsigned format, FC03 or FC04, and exactly 2 registers. Other formats (such as long_abcd) already combine the registers into one value, so just store it.
  • When the start address is an expression, count covers the whole range. The same is true of Write and Wait Until. end_address still works, for example "${addr} + 2".

Download the sample script ​

Sample script

Before running it, start a Slave window on 127.0.0.1:502 and click Listen, then open the script with Open JSON in Script mode and click Run. See How to use the examples.

Roach Modbus Tool by Mr.Roach