Troubleshooting
Why TwinCAT FB_MBReadRegs fails with ADS error 1794
Short answer
ADS error 1794 means the read block tried to write received data at an address range the runtime refuses. Use FB_MBReadInputRegs for input-register data, declare the buffer in %M with explicit byte addresses, keep nQuantity within the buffer's word capacity, pass cbLength := SIZEOF(buffer), pulse bExecute once per transaction, and power-cycle the controller if the error persists.
On this page
- What does ADS error 1794 mean in TwinCAT?
- Is FB_MBReadRegs the right block for Modbus input registers?
- Why can't the receive buffer sit in the input image?
- How many words fit in the destination array?
- What value goes in cbLength?
- How should bExecute be triggered?
- How do you verify the read after the fix?
- What clears error 1794 when the code is already correct?
- Questions asked
ADS error 1794 from a TwinCAT Modbus TCP read block is the documented invalid-index-group code: the block tried to place received data at an address range the runtime refuses, so the fault sits in the destination buffer and the call's parameters, not on the Modbus wire. The fix, in the order it matters: use FB_MBReadInputRegs when the server exposes the values as input registers; declare the buffer in %M at explicit byte addresses; keep nQuantity inside the buffer's 16-bit word capacity; pass cbLength := SIZEOF(buffer) instead of a typed 100; and pulse bExecute once per transaction. Each step below is worked with the numbers of a six-word register map, and the last section covers the power cycle that cleared the one stubborn case.
What does ADS error 1794 mean in TwinCAT? #
ADS error 1794 is the documented ADS return code for an invalid index group, hex 0x703: a read or write named an index group the target does not accept, and on a Modbus read block that points at the destination buffer access rather than the Modbus transaction. Its neighbors on the same documented list are 1795, invalid index offset, and 1796, reading or writing not permitted. When the block raises 1794 at its error outputs, four call faults produce it, checked in this order:
- the destination declared in the input image at %I* instead of writable %M memory;
- nQuantity larger than the destination's capacity in 16-bit words;
- cbLength typed as a constant instead of the destination's real size;
- bExecute retriggered on every scan.
Each has its own section below, and the persistent case after all four corrections ends at the power cycle in the last section.
Is FB_MBReadRegs the right block for Modbus input registers? #
No: FB_MBReadRegs issues Modbus function 3 and reads holding registers, while position data published as input registers needs FB_MBReadInputRegs, which issues function 4 and reads 1 to 128 sixteen-bit registers per call. The three read blocks of the TwinCAT Modbus TCP client library split by register area and quantity unit:
| Function block | Modbus function | Register area | nQuantity unit |
|---|---|---|---|
| FB_MBReadInputs | 2 | discrete inputs | bits |
| FB_MBReadRegs | 3 | holding registers | 16-bit words |
| FB_MBReadInputRegs | 4 | input registers | 16-bit words |
A function 3 request against a map that exists only in the input-register area brings back no usable data, which matches the reported symptom: first the error, then silence and no coordinates. Changing the element type from DINT to WORD cannot move a request into another register area; only the choice of function block does that. Decision: when the server documents its positions as input registers, the call is FB_MBReadInputRegs.
Why can't the receive buffer sit in the input image? #
A read block copies the received bytes to pDestAddr, and the runtime refuses that write into the input image, so the buffer belongs in %M with explicit byte addresses, for example %MB0 for the received registers and %MB32 for the registers written back. In the reported setup the first attempt declared the buffer as AT %I*, which placed the write target where the copy is refused, matching the invalid index group. Two buffers both bound to %M* without addresses then shared the same bytes, and the coordinates climbed into the thousands until the addresses were separated. The declaration that worked:
VAR_GLOBAL
GAB_FactoryIO_RegsIN AT %MB0 : ARRAY [0..5] OF WORD;
GAB_FactoryIO_RegsOUT AT %MB32 : ARRAY [0..5] OF WORD;
END_VAR
The arithmetic keeps the two ranges apart: each array is 6 words × 2 bytes = 12 bytes, so the first occupies %MB0 through %MB11, and %MB32 leaves 32 − 12 = 20 bytes of gap before the second begins. Decision: every buffer a function block writes lives in %M at an address you assign, never at %I*.
How many words fit in the destination array? #
nQuantity must stay within the destination's capacity in 16-bit words: ARRAY [0..5] OF WORD holds 6 words, so the request is capped at 6, while the failing ARRAY [0..5] OF DINT held only 12 words against a request for 16. The failing combination, with the arithmetic:
- ARRAY [0..5] OF DINT is 6 elements × 4 bytes = 24 bytes, which is 12 words of 2 bytes.
- nQuantity := 16 asks for 16 words × 2 bytes = 32 bytes of received data.
- 32 − 24 = 8 bytes land past the end of the destination, and 1794 appeared with exactly this call.
The documented ceiling for both register blocks is 128 registers per call, which needs 128 × 2 = 256 bytes of destination; a larger map is split across calls. An oversized multi-register request fails differently on other masters, as in why MB_CLIENT returns error 8383 on multi-register reads. Decision: enlarge the array first if the map needs more registers, then derive the request as nQuantity ≤ SIZEOF(destination) / 2.
What value goes in cbLength? #
cbLength carries the destination's size in bytes and comes from the compiler as SIZEOF(destination); in the reported case the error stayed until that call replaced the typed constant 100. A constant tells the block it may hand the runtime more bytes than the variable owns: cbLength := 100 against a 24-byte array describes 76 bytes that do not exist, and even with the register area and the memory already corrected, 1794 persisted until SIZEOF took over. Three rules keep the parameters honest:
- cbLength := SIZEOF(buffer), in bytes;
- nQuantity at or below cbLength / 2, in words;
- both taken from the same declaration, so an array change updates them at compile time.
Decision: never type cbLength by hand; the destination's declaration is the single source for both numbers.
How should bExecute be triggered? #
bExecute is pulsed once per transaction: call the instance with bExecute := TRUE a single time, then keep calling it with bExecute := FALSE and wait until bBusy is FALSE before the next request, never retriggering it every scan. Two rules make that work. First, declare an instance in VAR and call that instance; using the type name FB_MBReadRegs where an instance is required makes the compiler refuse with the message that the function block must be instantiated to be accessed. The same instance rule applies when calling a ladder function block from SFC in CODESYS so it executes. Second, in a state machine, fire the request in one state and service it in the following states:
VAR
LFB_MBReadInputRegs : FB_MBReadInputRegs; // instance, declared once
END_VAR
// state 10: fire once; the address, port and unit ID are example values
LFB_MBReadInputRegs(
sIPAddr := '192.168.0.109',
nTCPPort := 505,
nUnitID := 255,
nQuantity := 6, // matches ARRAY [0..5] OF WORD
nMBAddr := 0, // first register of the example map
cbLength := SIZEOF(IO.GAB_FactoryIO_RegsIN),
pDestAddr := ADR(IO.GAB_FactoryIO_RegsIN),
bExecute := TRUE,
tTimeout := T#1S
);
// later states: keep the instance called with the trigger low until bBusy drops
LFB_MBReadInputRegs(bExecute := FALSE);
Retriggering on every scan restarts the transaction before any response arrives, so the block churns without delivering. Decision: one rising edge per request, and the next edge only after bBusy has gone low.
How do you verify the read after the fix? #
A read is proven by cbRead, bError and the data: for six words cbRead must report 12 bytes, bError stays FALSE, nErrId stays 0, and the words in the buffer follow the moving coordinates. Check in this order:
- bBusy goes FALSE at the end of each transaction, so the trigger logic is cycling;
- bError is FALSE and nErrId is 0, so no ADS or Modbus fault is pending;
- cbRead equals nQuantity × 2 bytes per word, here 6 × 2 = 12 bytes;
- the values at the buffer change as the simulated axis moves.
When a coordinate arrives with its sign bit set, the word is still an unsigned pattern at this stage, and converting an unsigned 16-bit Modbus word to a signed integer is the step that turns it into a usable position. Decision: call the fault fixed only when all four checks pass on consecutive transactions.
What clears error 1794 when the code is already correct? #
Cycle the controller's power: the reported case where 1794 persisted after every correction cleared only when PLC power was removed and restored, while a software reboot left the condition in place. Before cutting power, save the project and put the machine into its safe state with the outputs de-energized, because everything the controller holds drops when the supply goes. Then remove and restore power at the controller's switch, download the corrected project, and watch the first transactions against the four checks above. Decision: the power cycle is the last step, taken only after the register area, memory location, quantity and cbLength are all right, because on a call that is still wrong the error returns with the first request.
Questions asked
How do I read more than 128 registers with FB_MBReadInputRegs?
Each call of the register blocks is documented for 1 to 128 registers, so a larger map needs several calls. Split the map into consecutive windows, give each call its own nMBAddr start and its own destination array, and run the calls one after another on one instance by waiting for bBusy to drop between them. Two instances can overlap requests only where the server accepts more than one open transaction.
What is the difference between ADS errors 1794, 1795 and 1796?
They sit side by side on the documented ADS return-code list: 1794 is invalid index group, 1795 is invalid index offset, and 1796 is reading or writing not permitted. All three describe an access to an address range the target refuses, so the same checks apply to each: destination in writable %M memory, nQuantity within the buffer's word capacity, and cbLength equal to the buffer's real size.
Does nQuantity count bits or words on FB_MBReadInputs?
FB_MBReadInputs issues Modbus function 2 and nQuantity counts bits there, while both register blocks count sixteen-bit words. A working discrete-input call read 64 bits into ARRAY [0..15] OF BYTE: 64 bits is 8 bytes, and the 16-byte array holds that with 8 bytes to spare. Match the unit to the block before comparing nQuantity against the destination size.
Can a software restart clear error 1794 instead of a power cycle?
Not in the reported case: a software reboot left the error in place, and only removing and restoring power at the controller's switch cleared it, which points at a stale runtime memory condition rather than at the project. After the cycle the block opens a fresh connection to the server on its first request, so judge the result on the checks of the first few transactions, and expect the error back if any call fault remains.