IMDHMIControls LibraryShop main store

Troubleshooting

Why a Keyence scanner result shows only one byte in CODESYS

Industrial networks Published 5 min read

Short answer

One character arrives because the assembly generated from the scanner's EDS file reserves a single result element instead of the fixed 128-byte result block. Delete the Result Data entry and re-add it with Bit length = 8 and Count = 128, then bind a union of STRING(127) and ARRAY[0..127] OF BYTE to the regenerated start address to read the null-terminated result.

On this page
  1. Why only one byte can be mapped
  2. Confirm the fault before editing the assembly
  3. Rebuild the Result Data entry
  4. Overlay the block as a string
  5. Reading a 20-character code from the 128-byte block at %IB60
  6. Read the bytes in one radix
  7. Questions asked

You see one character because the input assembly generated from the scanner's EDS file reserves a single result element, while the scan itself is longer. Delete the Result Data entry in the CODESYS assembly configuration, add it again with Bit length = 8 and Count = 128, let the input mapping regenerate, then bind a union of STRING(127) and ARRAY[0..127] OF BYTE to the result block's start address. Nothing changes on the scanner: it triggers, it reports a 20-byte result, and its first character already reads correctly at %IB60. What is missing is allocated bytes in the assembly and a variable shaped to read them.

Why only one byte can be mapped #

"Result Data Size" is a live field inside the input data. Its value of 20 says the current scan produced 20 bytes; it says nothing about storage. The assembly generated from the EDS file reserved one result element, so the input mapping offers exactly one byte, %IB60, and the remaining bytes of the scan have no mapped variable to appear in. Enlarging a variable in application code cannot fix this: the EDS-derived assembly decides what may be mapped, and a device description sets limits the application cannot extend — the same reason why an EDS file will not add EtherNet/IP to a terminal server.

Direction matters when you check it. The PLC application is the EtherNet/IP scanner, the originator; the Keyence unit is the adapter, the target. Result data flows target to originator (T->O), the input direction. The trigger you force is output, O->T, so a working trigger alone proves nothing about the input assembly.

Confirm the fault before editing the assembly #

Run these checks on a fresh trigger, not on values left over from an earlier connection:

  • The Ethernet link is up at both ends and the I/O connection is established — you already have this if the scanner answers a trigger.
  • Result Data Size changes to 20 after the scan: the cyclic input path carries live data.
  • The byte at %IB60 tracks the code — 57 decimal when the first character is 9.
  • The input mapping lists a single result element.

All four together point at the assembly, not the network, the scanner or the string handling. If the I/O connection itself is down, fix that first; nothing below repairs a dead connection.

Rebuild the Result Data entry #

  1. Open the Keyence adapter under the EtherNet/IP scanner in the project tree and go to its assembly configuration.
  2. Delete the existing Result Data entry.
  3. Add Result Data again with Bit length = 8 and Count = 128. Read that as 128 elements of one byte each; it is not one element 128 bits wide.
  4. Apply the change and let CODESYS regenerate the input mapping.
  5. Read the result block's new start address in the regenerated mapping — the same channel assignment you follow when mapping physical I/O channels in CODESYS V3.5 — instead of carrying %IB60 forward by habit.
  6. Check any assembly fields that follow Result Data. Enlarging this entry shifts their offsets, and the total T->O size must still match what the scanner is configured to send.

Overlay the block as a string #

Declare the union at the block's exact size:

TYPE STRING_UNION_TYPE :
UNION
    _result : STRING(127);
    _byte   : ARRAY[0..127] OF BYTE;
END_UNION
END_TYPE

Bind one instance to the start address the regenerated mapping shows — here the observed %IB60:

ResultData AT %IB60: STRING_UNION_TYPE;

Read the whole code as ResultData._result, or single positions as ResultData._byte[0] through ResultData._byte[127]. A union performs no conversion; it is two views of the same storage. CODESYS reserves one extra byte for the terminating null, so STRING(127) occupies exactly 128 bytes and the two views align. The string view is valid only up to the null byte — if the scanner's output format does not write one, copy by length from the byte view, using the reported result length. Keep the union inside the result block and the block inside the total T->O input assembly; an overrun reads the following field as characters.

Reading a 20-character code from the 128-byte block at %IB60 #

Inputs:

  • Scan length: 20 bytes, reported by the scanner's result length field after a fresh trigger.
  • Result block capacity: 128 bytes, fixed by the scanner.
  • Assembly entry: Bit length = 8, Count = 128.
  • First byte: 57 decimal, observed at %IB60.
  • String capacity: STRING(127) plus one reserved null byte.

Rule in words: an assembly entry allocates its bit length times its count; eight bits make one byte; the byte at index n sits at the start address plus n.

The arithmetic:

  • 8 bits times 128 elements = 1024 bits; 1024 bits divided by 8 = 128 bytes allocated.
  • A 20-byte payload occupies indices 0 through 19, so its last byte sits at 60 + 19 = 79, at %IB79, and the block's last byte at 60 + 127 = 187, at %IB187.
  • STRING(127) needs 127 + 1 = 128 bytes — exactly the block, with nothing spare.
  • 57 decimal is 39 hexadecimal, the ASCII code for 9, so the first character checks out.

Result: with the rebuilt entry and the union bound at the block's start, %IB60 through %IB79 carry the code, and ResultData._result shows all 20 characters up to the terminator.

Decisions: 128 byte elements in the regenerated mapping means the transport is fixed and any remaining symptom is radix or termination. One element still listed means the entry was never rebuilt — return to the assembly configuration. And no code longer than 127 characters can be read through this block; the scanner's fixed result field caps it regardless of what the controller declares.

Read the bytes in one radix #

The readings from this fault mixed number bases: 57 was read as decimal and identified as 9, then 49 was called a hexadecimal code for 1. Both cannot hold in one radix:

Watch value Read as decimal Read as hexadecimal
57 57 = hex 39 = character 9 hex 57 = 87 = character W
49 49 = hex 31 = character 1 hex 49 = 73 = character I

Decimal 49 is the ASCII code for 1; hexadecimal 49 is the ASCII code for I. Fix the watch table to one display format and compare the byte view against the scanner's documented output. When the numbers match the expected ASCII sequence but the string still looks wrong, no network setting will help — the fault is termination or union size.

Questions asked

Why does Result Data Size show 20 when only one byte can be mapped?

Result Data Size is a live value the scanner writes into the input data: it reports the length of the current scan, 20 bytes for a 20-character code, and it changes when a code of a different length is read. The reserved assembly element is separate and stayed at one byte, which is why the mapping offered a single character regardless of the reported length.

Why STRING(127) and not STRING(128)?

CODESYS reserves one extra byte for the null terminator, so STRING(127) occupies 128 bytes, exactly the fixed size of the result block. STRING(128) would occupy 129 bytes and overrun the field that follows. A union smaller than the block is acceptable when only part of the block is mapped; a larger one never is.

The watch view shows 49 for the first byte. Is that a 1 or an I?

It depends on the radix. Decimal 49 is the ASCII code for 1, written 31 in hexadecimal; hexadecimal 49 is 73 decimal, the ASCII code for I. Pick one display format for the byte view in the watch table and compare it against the scanner's documented output before suspecting the mapping.

Does the scanner itself need reconfiguration?

No. Triggering works and the reported result length is correct, so the scanner side is done. The correction lives in the controller project: rebuild the Result Data assembly entry, regenerate the input mapping, add the union, then download the project so the changed device configuration actually reaches the controller.

Why does the string sometimes carry trailing garbage after a shorter code?

The string view reads to the first null byte. When the scanner does not write a terminator, bytes left in the block from an earlier, longer scan stay in place after the new result. Copy by length instead: take the number of bytes named by the result length field from the byte view into a buffer you manage yourself.

Keep reading

  • Troubleshooting

    Fixing a TwinCAT EtherNet/IP slave connection from an Omron PLC

    The CIP1 fault is a Forward Open mismatch: each Omron direction must carry the full serialized TwinCAT assembly, not the process variables alone. With 2 bytes in and 2 bytes out, the 4-byte ConnCtrl on outputs and ConnState on inputs make each direction 6 bytes, and the instances must follow producer and consumer roles. If a capture shows the request and a rejection, read the sizes and instances the originator actually serialized.

    Industrial networks

  • Troubleshooting

    Fix EtherNet/IP I/O between a ctrlX adapter and a Rockwell scanner

    Start at the Studio 5000 module status: a connection that never establishes points to the IP path or the assembly fields, while a connection with wrong or frozen data points to swapped input/output instances or the task interval. Match instance numbers and byte counts at both ends, instance 100 for consumed data with configuration instance 102 at length 0 in the documented example, and set the adapter I/O task interval equal to the scanner's RPI, 4 ms at priority 20 to 39.

    Industrial networks

  • Troubleshooting

    Why the CODESYS EtherNet/IP scanner runs in demo mode

    The scanner runs in demo mode because the installed Fieldbus communication IEC 61784 license covers the PROFINET controller stack, not the EtherNet/IP scanner. Activate the EtherNet/IP Scanner add-on license on the control running the CODESYS runtime and restart it; the 30-minute limit and the license message then clear.

    Industrial networks