Message assembly, alarm-text building, batch-number generation — all string work. ST's string toolkit is short: CONCAT to join, LEFT/RIGHT/MID to extract, LEN for length, DELETE/INSERT to cut and splice — but one trap awaits every newcomer: string length versus buffer size. The sections below give the function table, joining, extraction and the pits.
| Function | Does | Example |
|---|---|---|
| LEN(S) | String length | LEN('AB12') = 4 |
| CONCAT(S1,S2) | Joins two strings | CONCAT('AB','12') = 'AB12' |
| LEFT(S,N) | Left N characters | LEFT('AB12',2) = 'AB' |
| RIGHT(S,N) | Right N characters | RIGHT('AB12',2) = '12' |
| MID(S,Pos,N) | N characters from position Pos | MID('AB12',2,2) = 'B1' |
| DELETE(S,N,Pos) | Deletes N from position Pos | DELETE('AB12',2,2) = 'A2' |
| INSERT(S1,S2,Pos) | Inserts S2 at Pos | INSERT('A2','B',1) = 'AB2' |
Mind the origin convention: most IEC libraries count Pos from 1 (MID('AB12',2,2) starts at the second character). Confirm in your platform's library documentation — a one-position slip skews every extraction.
CONCAT joins two at a time; longer builds nest or step:
Engineering advice: beyond three parts, build step by step into temporary strings (one CONCAT per step) — readable and debuggable; a five-deep nested one-liner lives on bracket-matching and dies at the first edit. Numbers become strings through INT_TO_STRING / DINT_TO_STRING conversions — concatenating a raw number is a compile error or mojibake.
| Scene | Method | Points |
|---|---|---|
| Fixed-length frames | MID by fixed offsets per field | Offset constants + comments = the field table |
| Variable-length text | LEN first, FIND for delimiters, then cut | Without FIND, a small loop finds it |
| Suffixes / extensions | RIGHT(S, N) | Confirm length ≥ N first |
| Trimming spaces | LEN-driven shrink loop | Or the platform's TRIM-type function |
Variable-length parsing must verify length first: an over-short string makes MID return empty or skew — "verify length, then extract" is the parsing iron law.
| Trap | Mechanism | Defence |
|---|---|---|
| Default 80 characters | STRING declared without a length defaults to 80 bytes; CONCAT results beyond it are silently truncated | Declare with length: s : STRING(255); |
| Result overflow | Two parts joined exceed the buffer; the tail vanishes without alarm | Compute LEN before joining; branch on overlength |
| Chinese takes two bytes | One CJK character occupies 2 bytes; 80 characters hold only 40 Chinese characters | Count bytes; double the budget for Chinese fields |
| PLC and HMI lengths differ | The HMI variable shorter than the PLC's truncates the display | Unify lengths and write the convention down |
"Silent truncation" is the nastiest: no error, function looks fine, only the text's tail is missing — batch numbers mismatch, alarm descriptions lose their endings. The only defence: declare every string with an explicit length, verify length before every join.
| Symptom | Cause | Fix |
|---|---|---|
| Joined string only half there | 80-character buffer truncated | Declare with explicit length |
| Extraction skewed one position | Pos origin misunderstood (0/1-based) | Check the platform docs, test one case |
| Chinese turns to mojibake | Encoding mismatch (PLC/HMI) | Unify encoding, test a Chinese sample |
| Numbers join as odd characters | No TO_STRING conversion | Use the conversion functions |
| Parsing occasionally returns empty | Extract before verifying length | LEN check first |
ST string work is not about knowing many functions but two disciplines: length management (declared lengths, verified joins) and origin confirmation (checked once per platform). Lose the functions and the documentation has them; lose the disciplines and string problems return forever as "occasionally missing tails, occasionally skewed fields".
String types and standard functions follow IEC 61131-3 and the platform library documents (CODESYS/TwinCAT String libraries, TIA CONCAT/S_CONV etc.); origin and default-length conventions follow the platform manuals; Chinese character byte counts follow the encoding (GB2312/UTF-8 two or three bytes) and the HMI documentation.
Write the string conventions into the project standard at kickoff: lengths, encoding, origin, field table — one agreement, and the whole line stops fighting strings.