Troubleshooting
Fix Modbus TCP FC02 discrete inputs returning no data
Short answer
FC02 finishing with no error while the client BOOLs stay FALSE is a mapping fault, not a network fault: the server block's discrete-input buffer is BYTE storage, and unpacked bytes read back as zero bits. Declare the buffer in BYTE, pack each state into its bit, and match the requested range. The FC03 reads that already work prove the connection.
On this page
An FC02 read that finishes with no error while every client BOOL stays FALSE is a data-mapping fault, not a communication fault. In the CXA_ModbusTCP library, version 3.4.0.0, the server's type-02 block publishes its discrete inputs through a BYTE buffer called InputData, while the client block IL_ModBusTCPReadDiscreteInputs hands you BOOL values. If nothing in the server application packs states into those bytes, the table holds 16#00 and the client correctly reads all-zero bits: transaction complete, data absent. Working FC03 holding-register reads on the same IL_ModBusTCPClient connection already prove the Ethernet path, the addressing and the port, so leave them alone. Declare InputData in the BYTE form the block requires, pack each state into its bit position, and align the requested range with the populated bits.
Why FC03 works while FC02 returns nothing #
Modbus keeps four separate data tables in the server: coils read by FC01, discrete inputs read by FC02, input registers read by FC04, and holding registers read by FC03. The tables do not share storage. Writing 47 to a holding register does nothing to the discrete input carrying the same displayed number, so a working FC03 exchange proves the connection and says nothing about the discrete-input table's contents. That is precisely the isolation you need.
The client connection, the server address and port, the trigger logic and the transaction sequence are shared between the two functions. When FC03 returns a value you deliberately changed in the server, all of that is proven. The only things left unproven are the content of the discrete-input table and the path from response bytes to client BOOLs, and that is where this fault lives.
A wrong address range announces itself differently: a request outside the configured table returns exception 02 instead of a clean all-zero response, which is what happens when a Modbus read crosses a register map gap. Done with no error and all FALSE is the signature of a table nobody loaded.
The server stores discrete inputs as packed bytes #
The server's type-02 block, IL_ModBusTCPSeverType02, exposes discrete inputs as InputData, and the library requires that buffer in BYTE form; the client read block unpacks the same logical data into BOOL values. One server BYTE carries eight discrete-input states. These are two representations of one table, not interchangeable array layouts: pointing the server application at a BOOL array does not load the table, and the read still returns zeros.
The correction is a usage fix, not a workaround: define InputData exactly as the installed library's declaration requires, and write the application states into it bit by bit. The failure family is recognizable elsewhere — the same transport-proven, representation-broken pattern shows up when reading a float over Modbus TCP in an HMI. On Siemens servers the equivalent mapping question is covered in troubleshooting S7 MB_SERVER Modbus TCP FC02 FC03 and FC05 mapping.
How FC02 packs the bits #
An FC02 request carries a 0-based start offset and a quantity; the response carries a byte count followed by the data bytes, one byte per eight inputs, rounded up. Inside each data byte the packing is least-significant-bit first: the first requested input is bit 0 of the first byte, input 8 is bit 0 of the second byte, and in general input n lands in byte n DIV 8 at bit n MOD 8. Multi-byte fields in the request itself, such as the start offset and the quantity, travel high byte first; that byte order and the LSB-first bit packing share one message and are not in conflict.
Worked example, ten states at offsets 0 through 9, displayed as 10001 through 10010 in five-digit notation:
| Item | Value | Arithmetic |
|---|---|---|
| Buffer size | 2 bytes | 10 inputs / 8 = 1.25, rounded up to 2 |
| States TRUE | offsets 0, 2, 8 | application choice |
| Byte 0 | 16#05 | 2^0 + 2^2 = 1 + 4 = 5 |
| Byte 1 | 16#01 | 2^0 = 1, because offset 8 is bit 0 of byte 1 |
| Response | byte count 2, data 05 01 | packing rule above |
| Client destination | elements 0, 2 and 8 TRUE, other seven FALSE | one BOOL per requested input |
Before the fix, the same exchange returns 00 00 and all ten BOOLs read FALSE with the block done and no error — the exact symptom.
Packing code, illustrative; the names are mine, not library identifiers:
VAR
aStates : ARRAY[0..9] OF BOOL; (* application states, offsets 0..9, default FALSE *)
abInputData : ARRAY[0..1] OF BYTE := [16#00, 16#00]; (* server buffer, 2 bytes *)
n : UINT := 0;
END_VAR
FOR n := 0 TO 9 DO
IF aStates[n] THEN
abInputData[n/8] := abInputData[n/8] OR SHL(BYTE#16#01, n MOD 8);
ELSE
abInputData[n/8] := abInputData[n/8] AND (NOT SHL(BYTE#16#01, n MOD 8));
END_IF
END_FOR
Test: set aStates[0], aStates[2] and aStates[8] TRUE and run the loop once; abInputData must read 16#05 and 16#01, matching the table. The set-and-clear form leaves the other seven bits of each byte untouched.
The fix, step by step #
- Open the server block's declaration in the installed CXA_ModbusTCP 3.4.0.0 library and read the required InputData type and bounds. Declare it BYTE-based and size it to the configured discrete-input range: bytes needed equals quantity divided by 8, rounded up.
- Keep the application signals as BOOLs wherever the logic uses them; convert at the buffer, not throughout the program.
- Map state n to byte n DIV 8, bit n MOD 8, and write every state every scan.
- Set and clear single bits with a mask, as in the loop above, so one state change never wipes its seven neighbors, and scan the program for any later whole-byte assignment to the buffer that overwrites your bits.
- On the client, point the read's start offset and quantity at the populated range — offset 0, quantity 10 in the example — and give the destination exactly that many BOOL elements.
- Trigger the read, wait for the done indication, then consume the destination; keep the FC03 exchange running as the baseline and change one mapping element at a time.
Prove it before closing the ticket #
Watch the server's InputData bytes online. Force the state at offset 3, displayed 10004: byte 0 moves from 16#05 to 16#0D, because 5 + 2^3 = 13, and nothing else changes. Bytes that never move while the application states are TRUE mean the packing code is missing or overwritten.
Run the client read against the same bytes: elements 0, 2 and 8 of the BOOL array go TRUE with data 05 01. If the bytes move but the BOOLs do not, the fault is in the read block's destination wiring or in where the program consumes it — re-check the quantity, the destination size, and that the copy happens after done, not on the trigger scan.
Then force each state once, offsets 0 through 9, and confirm one client BOOL per state, in order. If any of these inputs feeds an interlock, finish that end-to-end check before the machine returns to service: a bit landing on the wrong input reports a closed guard that is open.
Match the symptom to the decision #
| Symptom | Where the fault is | Decision |
|---|---|---|
| FC03 works; FC02 done, no error, all BOOLs FALSE | Server table empty, bytes never packed | Pack InputData; the network is proven |
| FC02 returns exception 02 | Request range outside the configured table | Align start offset and quantity with the buffer |
| FC01 coil reads also wrong | Coil table has its own mapping and storage | Apply the same packing discipline to the coil table separately |
| Bytes change online, client BOOLs stale | Client destination or consumption | Check quantity, destination size, done-gated copy |
| Transactions fail or time out | Connection layer | Trace interfaces, addresses and port — the one case FC03 success rules out |
When the discrete inputs are healthy and you move on to writing coils, note that servers differ in mask-write support: what to do when a Modbus TCP server lacks FC22 support covers the fallback.
Questions asked
The read block shows done with no error. Does that prove the discrete inputs are correct?
No. Done reports the transaction: the request went out, a well-formed response came back, and no exception was raised. It says nothing about what the table holds. On this stack an error-free read of all-zero bits is the signature of a server table nobody packed, so check the InputData bytes online before touching the connection.
How many data bytes does an FC02 response carry for 20 inputs?
Three. The byte count is the quantity rounded up to whole bytes: 20 divided by 8 is 2.5, so the response carries 3 data bytes holding 24 bit positions, and the 4 positions past input 20 are padding sent as zero. The client destination still needs exactly 20 BOOLs, not 3.
Can I keep the application signals as BOOLs and still fix this?
Yes. Keep the BOOLs wherever the logic uses them and pack at the boundary: one move per state into its bit of the BYTE buffer, executed every scan before the server block runs. The failure comes from leaving the bytes unwritten, not from the application's choice of data type.
Will this correction also fix FC01 coil reads?
No. Coils are a separate table with its own server mapping and its own backing storage, so a coil fault needs the same discipline applied separately. What this fix does establish is that the connection and the client read path work, which narrows a coil problem to its table.
Why does address 40001 work while 10001 does not?
Those five-digit numbers are documentation conventions for the tables: 1xxxx denotes discrete inputs and 4xxxx holding registers, while the request itself carries a 0-based offset. 40001 and 10001 therefore address different tables and share nothing but the digits; success on one never populates the other.