Introduction to CoDeSys Joining Long Strings
Working with strings in PLC programming sounds simple until it suddenly isn’t. Many developers hit problems when codesys joining long strings becomes part of logging, communication, or HMI work. Errors appear out of nowhere. Data gets cut. Sometimes the PLC even stops.
If you have faced these issues, you are not alone. String handling in CoDeSys requires planning, especially when dealing with large or dynamic text. This guide walks through practical, real-world ways to handle long strings safely, without crashes or surprises.
Understanding Strings in CoDeSys
What Is a STRING Data Type in CoDeSys
In CoDeSys, a STRING is an array of characters stored in memory. By default, it is not unlimited. It has a fixed maximum size defined at declaration time.
That detail is where most problems start.
Default STRING Length and Its Limitations
By default, a STRING holds 80 characters. Anything beyond that is either cut or causes unexpected behavior.
STRING vs STRING(n)
- STRING → Default length (usually 80)
- STRING(255) → Allows up to 255 characters
- STRING(1024) → Suitable for long messages
When codesys joining long strings, using the default STRING is one of the most common mistakes.
Why Errors Occur When Joining Long Strings in CoDeSys
Memory Limits in PLC Systems
PLCs are not PCs. Memory is limited and tightly controlled. Long strings consume memory fast, especially when temporary variables are created during concatenation.
Buffer Overflow and Truncation Issues
If the destination string is shorter than the result, CoDeSys may:
- Truncate silently
- Throw runtime warnings
- Cause unstable behavior
Runtime Errors vs Compile-Time Errors
Some errors appear only when the program runs with real data. This makes codesys joining long strings harder to debug if not planned properly.
Common Error Messages Seen During String Joining
String Too Long Errors
This appears when concatenation exceeds the declared string size.
Unexpected Truncation
No error message. Just missing characters. This is dangerous because it looks like the program works.
Application Crash or Watchdog Timeout
Heavy string operations inside loops can slow the PLC and trigger watchdog faults.
Basic Methods for CoDeSys Joining Long Strings
Using CONCAT Function
Result := CONCAT(Str1, Str2);
This works for small strings. It becomes risky for long ones.
Using the + Operator
Result := Str1 + Str2;
This creates temporary copies in memory, increasing risk.
Why These Methods Fail for Long Strings
Both methods assume the destination string is large enough. When codesys joining long strings, that assumption often breaks.
Using STRING(n) to Safely Join Long Strings
Defining Proper String Length
Always calculate the maximum possible length:
- Static text
- Variable data
- Separators
- Safety margin
Example:
VAR
LogMessage : STRING(1024);
END_VAR
Best Practices for Length Calculation
If unsure, overestimate slightly. Memory is cheaper than crashes.
Step-by-Step Safe String Joining Approach
Initializing Strings Correctly
Always clear strings before reuse:
LogMessage := ”;
Appending in Small Chunks
Instead of joining everything at once, append step by step.
Chunk-Based String Joining Example
LogMessage := ”;
LogMessage := CONCAT(LogMessage, ‘Temp: ‘);
LogMessage := CONCAT(LogMessage, TempValue);
LogMessage := CONCAT(LogMessage, ‘ C’);
This approach reduces sudden memory spikes and helps codesys joining long strings safely.
Using CONCAT_EXT and Alternative Functions
When CONCAT_EXT Is Safer
Some CoDeSys versions offer extended string functions that manage memory better. These are safer for repeated appends.
Performance Considerations
Extended functions are slightly slower but far more stable for long operations.
Handling Strings in Loops Without Errors
FOR Loop String Appending
Appending inside loops is risky.
FOR i := 1 TO 100 DO
LogMessage := CONCAT(LogMessage, DataArray[i]);
END_FOR
This can overflow quickly.
Avoiding Infinite Loops and Overflows
Always:
- Check current string length
- Break when approaching limits
- Log partial data if needed
This is critical when codesys joining long strings dynamically.
Memory Optimization Tips for Long Strings
Reducing Temporary Variables
Avoid unnecessary intermediate strings. Reuse one main variable.
Clearing Strings After Use
After sending or logging:
LogMessage := ”;
This frees memory and improves stability.
Debugging CoDeSys Joining Long Strings
Monitoring String Length at Runtime
Use LEN() to monitor size:
CurrentLength := LEN(LogMessage);
This helps catch problems early.
Using Watch Windows and Logging
Watch string variables live. If length grows unexpectedly, stop and analyze.
Real-World Use Cases of Long String Joining
Logging and Data Export
System logs often require timestamps, values, and messages joined together.
Building JSON or CSV Strings
Communication with servers often needs structured text. This is a common case where codesys joining long strings becomes critical.
HMI and Communication Messages
Operator messages and alarms often exceed default string sizes.
Best Practices to Avoid Errors in the Future
Design-Level String Planning
Plan string sizes during design, not after errors appear.
Testing With Maximum Data Size
Always test with worst-case values, not average ones.
Final Thoughts on CoDeSys Joining Long Strings Without Errors
Handling strings in CoDeSys is not difficult, but it demands discipline. Most issues with codesys joining long strings come from underestimating size and memory behavior. With proper string declaration, safe appending methods, and runtime monitoring, long strings become stable and predictable.
Conclusion
Errors while joining long strings in CoDeSys are avoidable. The key is understanding limits, planning ahead, and writing defensive code. Treat strings like memory-sensitive data, not simple text. When done right, even complex logging and communication tasks run smoothly without crashes or data loss.
FAQs
1. Why does CoDeSys truncate long strings without warning?
Because the destination STRING length is smaller than the result.
2. What is the safest way to join long strings in CoDeSys?
Use STRING(n), append in steps, and monitor length at runtime.
3. Is CONCAT better than the + operator for long strings?
Both have limits, but step-by-step CONCAT is safer.
4. Can long string operations slow down a PLC?
Yes. Heavy string handling can trigger watchdog timeouts.
5. What string length should I use for logs or JSON data?
Usually STRING(512) or STRING(1024), depending on data size.

