Troubleshooting
Why a WORD bit reference fails in ladder logic and how to fix it
Short answer
On an internal WORD, read a bit directly as variable_name.bit_number in ladder. When the WORD is an I/O variable that rejects bit access, MOVE the whole WORD into an internal variable at the start of the scan, use internal_variable.bit_number in the rungs, and MOVE it back at the end of the scan only if the ladder writes to it.
On this page
A bit reference like status_word.3 works on an internal WORD in ladder, but the same form on some I/O variables fails to compile, and a bit exposed through a union can be rejected outright with the message C0230: Type name not expected in this place. The fix is to give the rungs a WORD they are allowed to touch: use variable_name.bit_number on an internal variable, and when the source WORD will not accept bit access, MOVE the whole WORD into an internal variable at the start of the scan, use its bits in the rungs, and MOVE it back at the end of the scan only when the ladder writes to it.
What the compiler is telling you #
C0230 is documented as a type name not expected in this place, with a possible cause of a data type name used at an invalid position. The message shows the data type name it found. In a union-bit case it means the compiler is reading your operand as a type where it expects a variable or expression, so the rung will not compile until that operand is replaced.
The error does not tell you why the reference was rejected. The evidence in a failing project rarely identifies the CODESYS version, the declaration syntax, or whether the WORD is mapped to physical I/O, and those distinctions decide the fix. Check which of the three cases below you are in before editing anything.
The three cases and the fix for each #
- Internal WORD. Reference the bit directly as
variable_name.bit_number, for examplemotor_status.6. This is the documented, supported form and it belongs in the rung as-is. - I/O variable that rejects bit access. Some I/O variables do not allow it. Add an internal WORD, MOVE the I/O value into it at scan begin, use
internal_variable.bit_numberin the rungs, and MOVE it back at scan end only if the ladder changes bits. - Bit exposed through a union, reported as C0230. Remove the union-derived bit operand from the rung and replace it with a bit on an internal WORD populated by MOVE at scan begin. The compiler error points at the operand, so replacing the operand is what clears the error.
Why the copy method is safe #
Ladder executes top to bottom in one scan, so the placement of the MOVEs is what makes the copy correct. The scan-begin MOVE must be in a rung above every rung that reads the internal WORD, and the scan-end MOVE must be below every rung that writes to it. Get the order wrong and a rung reads last scan's value, or two rungs write the same bits and the last one wins.
That last-rung-wins behavior is not unique to this workaround; it applies to any internal tag written from more than one place, and the pitfalls of copy instructions inside branched rungs follow the same scan-order rule.
Worked example: pulling a fault flag out of a status WORD #
Inputs. A WORD named io_status_word mapped to I/O, holding a device status in which bit 3 is the fault flag. An example value of 16#0008, which is a WORD with only bit 3 set. Bit 3 means the fourth bit from the least significant end, numbered 0 through 15.
Rule. A WORD value with exactly one bit set equals 2 raised to that bit number, so the fault bit is set when the WORD ANDed with 2^3 is not zero.
Arithmetic. 16#0008 in decimal is 8, and 2^3 = 8. The mask test is 16#0008 AND 16#0008 = 16#0008, which is nonzero, so the fault flag is TRUE. With the copy method, the MOVE puts 16#0008 into the internal WORD status_copy before the rungs execute, and the contact status_copy.3 in the ladder evaluates TRUE for that scan.
Result and decision. The rung compiles and the fault contact closes on any io_status_word value that has bit 3 set: 16#0008, 16#0009, 16#0018 and so on. The threshold is the mask itself, 2^3 = 16#0008; shift it for a different bit. Because this WORD is a status input the ladder only reads, skip the scan-end MOVE back; a return copy could overwrite incoming device data with a stale value.
Placing the MOVEs in the program #
Use one internal WORD as the ladder-facing copy so no rung ever references the rejected operand:
- At the top of the program, MOVE
io_status_wordintostatus_copy. - In the ladder rungs, reference the bit as
status_copy.3(or whichever bit you need). - At the bottom of the program, MOVE
status_copyback toio_status_wordonly when the ladder writes bits and the source must receive the changes.
After editing, compile and confirm the C0230 message no longer points at the ladder operand. Then go online and watch three things during execution: the source WORD, the internal copy, and the Boolean you extracted. The copy must receive the source value before any rung reads it, and any bit the ladder changes must reach the source only after the final MOVE. If the copy or the Boolean does not track the source, the MOVE placement is wrong, not the bit logic.
One safety note #
If the bit you are extracting feeds an interlock, an e-stop chain or a drive enable, treat the whole path as a safety function and validate it as such, including the scan-order dependency of the copy. A software bit copy is not a substitute for a wired safety circuit.
When the compiler still refuses #
If a direct bit reference on an internal WORD also fails, check that the operand you typed is the variable and not the type name, because that is exactly the condition C0230 describes. As a last resort on any variable, test the bit arithmetically: AND the WORD with the mask for that bit, or use SHL/SHR to shift the bit into a known position, and branch on the result being nonzero. It is uglier than status_copy.3, but it works where bit-access syntax is not accepted at all.
Questions asked
What does error C0230 actually mean?
C0230 is reported as Type name not expected in this place. The compiler is reading the operand you supplied as a data type name where it expects a variable or expression, so the rung never compiles. When the operand is a union-derived bit name or a bit reference on a variable that does not support it, replacing that operand with a bit on an internal WORD clears the error.
Do I need to move the internal WORD back at the end of the scan?
Only when the ladder writes bits and the source WORD must receive those changes. If the ladder only reads the bit, for example a status or fault flag, leave the data one-way and skip the return copy. A needless return copy on an input can overwrite incoming process data with a stale value.
Can I test bits without the dot syntax?
Yes. Apply a mask to the WORD with an AND, or shift the target bit into position with SHL or SHR, then compare the result against zero. This works on variables where bit access is not accepted, but it makes the rung harder to read, so the internal-copy method is usually the better first choice.