IMDHMIControls LibraryShop main store

How-to

Read and write Logix tags from CODESYS with CIP GenericService

Industrial networks Published 7 min read

Short answer

Read a Logix tag from CODESYS with CIP service 16#4C against the tag's Symbol Object instance at class 16#6B: send a two-byte element-count body, expect a two-byte type word first (16#C4 is DINT), and take the DINT value from bytes 2-5. Write with service 16#4D, sending type word, element count, then data. Rebuild the name-to-instance table after every download or online edit, because the controller reassigns instance numbers.

On this page
  1. What the first two bytes of the reply mean
  2. Build the Read Tag call
  3. Resolve the tag instance through the Symbol Object
  4. Write the tag with service 16#4D
  5. Worked example: one DINT, start to finish
  6. Symptoms and first checks
  7. Decide where explicit messaging fits
  8. Questions asked

Read a Logix tag from CODESYS by sending CIP service 16#4C to the tag's Symbol Object instance at class 16#6B, with a request body of exactly two bytes holding the element count. The reply starts with a two-byte data-type word, 16#C4 for DINT, so the value sits at bytes 2-5, not at byte 0. Writes use service 16#4D with type word, element count and data in one body. Resolve instance IDs by walking the class 16#6B name list at startup, and rebuild that list after every project download or online edit, because the controller reassigns the numbers. Rockwell documents the services, object model and reply layouts in 1756-PM020, Logix 5000 Controllers Data Access; every code and layout below comes from that document.

What the first two bytes of the reply mean #

A Read Tag reply for one atomic element is not bare data. Bytes 0-1 are a little-endian UINT data-type word, and the value follows it. If your program samples byte 0 and reads a constant 196 whatever the tag holds, you are reading the type word: 196 = 12 x 16 + 4 = 16#C4, the elementary code for DINT. No buffer resize moves that byte, and no tag value ever will.

The response service echo is a different field again: a successful reply carries 16#4C OR 16#80 = 16#CC, which is 204 decimal. Seeing 204 there means the controller answered your request; it is not your data either.

Reply layout for one element:

Byte offset Content One-element DINT read
0-1 Data-type word, little-endian 16#C4, 16#00
2-5 Value, least significant byte first the tag's DINT

Type words you will meet:

Type word Meaning
16#C1 BOOL
16#C2 SINT
16#C3 INT
16#C4 DINT
16#CA REAL

The data length follows the type: one byte for SINT, two for INT, four each for DINT and REAL. Check udiReceivedDataSize before decoding anything, because for one DINT element it reads 6, which is 2 type bytes plus 4 data bytes.

Build the Read Tag call #

  1. Add the Logix controller under the EtherNet/IP scanner in the CODESYS device tree and note its device reference; the call below uses it.
  2. Declare your own buffers and instance variable. Every name in the VAR block below is illustrative; the block's field names are the library's own, as its help lists them.
  3. Fill the request body with exactly the element count, two bytes little-endian, and pass udiWriteDataSize := 2. Passing SIZEOF of a larger buffer sends a zero count followed by padding, and the controller does not answer that with your value.
  4. Call the block with class 16#6B, the instance from your name table, attribute 0 and service 16#4C. Assign every field; Read Tag addresses the instance rather than an attribute, and the library help documents how the block treats the attribute field for instance-level services.
  5. Decode on xDone only, after evaluating xError and eError.
VAR
    xReadTag       : BOOL := FALSE;         // rising edge starts the call
    udiTagInstance : UDINT := 0;            // resolved from the name table
    awWriteData    : ARRAY[0..7] OF BYTE := [8(16#00)];
    awReadData     : ARRAY[0..63] OF BYTE := [64(16#00)];
    udiRx          : UDINT := 0;
    diValue        : DINT := 0;
END_VAR

awWriteData[0] := 16#01;                    // element count, low byte
awWriteData[1] := 16#00;                    // element count, high byte

genericService(
    xExecute            := xReadTag,
    itfEtherNetIPDevice := _1769_L33ER_LOGIX5333ER,
    eClass              := 16#6B,
    dwInstance          := udiTagInstance,
    wAttribute          := 0,
    eService            := 16#4C,
    pWriteData          := ADR(awWriteData),
    udiWriteDataSize    := 2,
    pReadData           := ADR(awReadData),
    udiReadDataSize     := SIZEOF(awReadData),
    xDone               => xDone,
    xBusy               => xBusy,
    xError              => xError,
    eError              => eError,
    udiReceivedDataSize => udiRx);

xDone, xBusy, xError and eError are the block's status outputs; udiRx is the receive-size copy. Decode least significant byte first:

IF xDone AND NOT xError THEN
    IF (udiRx = 6) AND (awReadData[0] = 16#C4) AND (awReadData[1] = 16#00) THEN
        diValue := awReadData[2]
                 + 16#100 * awReadData[3]
                 + 16#10000 * awReadData[4]
                 + 16#1000000 * awReadData[5];
    END_IF
END_IF

pReadData and udiReadDataSize set the receive capacity; udiReceivedDataSize reports what actually arrived.

Resolve the tag instance through the Symbol Object #

Class 16#6B is the Symbol Object: every controller and program tag is one instance of it. Instance numbers are assigned by the controller and are not a stable address. A download or an online edit renumbers them, the list includes system and program-scoped symbols, and the numbering is not contiguous. An instance that returned your tag this morning returning a different symbol this afternoon is the controller behaving normally, not a fault in your call.

Discovery works with getAttributeAll on successive instances: the reply carries the symbol name as ASCII bytes mixed with binary fields in the same buffer, which is why a raw dump of it reads like a page of decimal character codes. On one small test project every sample tag sat below instance 18; that tells you nothing about the next project.

Work this way:

  1. At startup, walk the instances with getAttributeAll, decode each name, and build a name-to-instance table in the PLC.
  2. Address every read and write through that table by tag name.
  3. Before each access, and on any xError, re-read the name at the cached instance and compare it with the expected string.
  4. On a mismatch or an error, discard the table and rebuild it.
  5. Force a rebuild whenever you know the Logix project was downloaded or edited online. Do not wait for the comparison to catch it.

Write the tag with service 16#4D #

The Write Tag request body is the type word, the element count, then the data: eight bytes for one DINT. Take the type word from a Read Tag reply on the same tag and never guess it, because a mismatched type word fails the write.

To write 1000000, note that 1000000 = 16#000F4240, since 15 x 65536 + 66 x 256 + 64 = 983040 + 16896 + 64 = 1000000. The value bytes, least significant first, are 16#40, 16#42, 16#0F, 16#00. The full body:

awWriteData[0] := 16#C4;   // type word, low byte
awWriteData[1] := 16#00;   // type word, high byte
awWriteData[2] := 16#01;   // element count, low byte
awWriteData[3] := 16#00;   // element count, high byte
awWriteData[4] := 16#40;   // value, least significant byte first
awWriteData[5] := 16#42;
awWriteData[6] := 16#0F;
awWriteData[7] := 16#00;
// eService := 16#4D, udiWriteDataSize := 8

Judge a write by xDone and eError, not by reply content. And treat a written tag as a command to running equipment: interlock its effect in the Logix project so a stale or mis-decoded value cannot move anything unsafe.

Worked example: one DINT, start to finish #

  1. In the Logix project, create a controller-scope DINT tag and set it to 305419896, which is 16#12345678.
  2. Resolve its instance from the name table and confirm the decoded name matches exactly.
  3. Send the Read Tag call from the section above: service 16#4C, two-byte count of 1.
  4. Expected reply: udiRx = 6, byte 0 = 196 (16#C4), byte 1 = 0, bytes 2-5 = 16#78, 16#56, 16#34, 16#12.
  5. Reconstruct: 16#78 = 120; 16#56 = 86, and 86 x 256 = 22016; 16#34 = 52, and 52 x 65536 = 3407872; 16#12 = 18, and 18 x 16777216 = 301989888. The sum 120 + 22016 + 3407872 + 301989888 = 305419896, the tag value, proven.
  6. Set the tag to 0 and repeat: bytes 2-5 clear to zero and byte 0 stays 196. When the decode tracks the tag and byte 0 does not move, the decode is proven.

Symptoms and first checks #

Symptom Likely cause First check
Constant 196 in byte 0 Type word read as data udiReceivedDataSize = 6, then read bytes 2-5
Right value earlier, wrong tag now Instance renumbered after a download or online edit Re-read the symbol name at the cached instance
xError set, eError populated Controller rejected the service, instance or body Body length, instance still exists, service code
Read works, write fails Type word or element count mismatch Compare against the type word Read Tag returned
Readable text mixed with junk in discovery ASCII name and binary fields in one buffer Parse by the reply structure, not as one string

None of these rows is a comms-loss symptom: when the controller stops answering at the network level, this block sets xError instead of serving stale data, and the operator-facing version of that failure is PLC comms timeouts that turn HMI values into question marks.

Decide where explicit messaging fits #

Four routes connect CODESYS to a Logix controller, and the choice comes down to which project you are allowed to edit:

  • The EDS import route, with the Logix controller as adapter, gives cyclic reads only: Studio 5000 exposes a Producer tag and no Consumer, so the data flow is one-way into CODESYS.
  • Logix as scanner with the CODESYS device as adapter gives cyclic I/O in both directions and needs a module added in the Logix project.
  • CODESYS as producer with the Logix controller as consumer also requires a change on the Logix side; check your CODESYS release notes for scanner-and-adapter combination support before designing around it.
  • Generic service on class, instance and attribute needs no Logix-side change, and you own the symbol table and its invalidation.

The decision: for a handful of atomic tags, about ten or fewer, on a Logix project you must not touch, use the generic service route exactly as described above. For arrays, structures, or a tag list that keeps growing, put the mapping in the Logix project and use cyclic I/O instead.

Questions asked

Why does the first byte of my read buffer stay at 196?

Because 196 is 16#C4, the data-type word for DINT that leads every Read Tag reply, and it stays 196 no matter what the tag holds. The same reading identifies other types: 195 is 16#C3 (INT) and 202 is 16#CA (REAL). Use it as a runtime type check before decoding, then take the process value from the bytes after it, bytes 2 to 5 for a DINT.

Why did one instance return my tag this morning and a different tag later?

Instance numbers under class 16#6B are assigned by the controller, so a download or an online edit moves them, and the class also holds system and program-scoped symbols with non-contiguous numbering. Never persist the table in retentive memory and trust it after a restart: validate the cached names or rebuild outright, and always force a rebuild when you know the Logix project changed.

Can I pass the tag name straight to the generic service block?

No. The block takes class, instance and attribute only; there is no symbolic path segment to carry a name, so the instance number is the only handle it accepts. Keep names on your side: store the name-to-instance table, treat the name as the address and the instance as a cache entry you refresh after every download or online edit.

How large a reply should I expect for an array of DINTs?

For DINT elements the reply is two type bytes plus four bytes per element, so an eight-element DINT array returns 2 + 8 x 4 = 34 bytes; check udiReceivedDataSize against that before decoding. Larger arrays and structures need the fragmented read service and, for UDTs, template handling, both documented in 1756-PM020, so size your buffers from the type word first.

Why does my discovery dump show numbers like 77 and 121?

Those are ASCII bytes read as decimals: getAttributeAll returns the symbol name as ASCII, so the name MyTag arrives as bytes 77, 121, 84, 97 and 103 (16#4D, 16#79, 16#54, 16#61, 16#67). Decode the name field as text and everything else by the documented reply structure; treating the whole buffer as one string is what mixed readable characters into junk.

Is the EDS import route good enough for writes?

No. With the EDS imported and the Logix controller as adapter, Studio 5000 exposes a Producer tag, so CODESYS reads cyclically but there is no Consumer tag to write to. If you cannot edit the Studio 5000 project, generic service against class 16#6B is your only write path from CODESYS; if you can edit it, cyclic I/O in both directions is the cleaner design.

Keep reading

  • How-to

    CompactLogix MSG to SLC 5/04 through a DH+ gateway

    The CIP path in the CompactLogix MSG ends at the 1756-DHRIO module in the remote chassis. On the MSG configuration tab, change the communication method from CIP to DH+, then enter Channel A and the SLC 5/04 node address in the DH+ fields; the SLC is never part of the path itself.

    Industrial networks

  • How-to

    Reach a CompactLogix through a ControlNet bridge in RSLinx

    When autobrowse through a 1784-U2CN ControlNet path lists the bridge's EtherNet/IP module but no CompactLogix or third-party EtherNet/IP devices, add the target IP address manually under that module's Ethernet port in RSWho or the FactoryTalk Linx browse tree, then select the resulting device as the online path in the Logix programming software. The devices are reachable; only discovery fails, and a manual entry needs no discovery.

    Industrial networks

  • How-to

    Control a PowerFlex 525 drive from two PLC controllers

    Yes — add the PowerFlex 525 to the Logix controller's Ethernet tree and its command tags appear, but a cyclic drive connection has exactly one owner. Give the Logix controller that ownership, read the MicroLogix machine's request into it, and let one rung select the command the drive follows. Never let two controllers write the speed reference.

    Drives and motion