07Data and variables
Literals and constants: 16#FF, 2#1010 and T#5s
How to write a number, a duration or a bit pattern in ST, and why the compiler sometimes rejects a number that looks fine.
The bases
Form | Meaning | Example |
|---|---|---|
| decimal | integer |
| real | |
| binary | bit patterns |
| octal | rare |
| hexadecimal | masks, Modbus addresses |
| boolean | |
| duration | |
| text | |
The underscore is visual only: 1_000_000 is 1000000.
Typed literals
Sometimes you have to tell the compiler which type you mean:
INT#100 (* 100 as a 16-bit integer *)
REAL#100 (* 100.0 *)
DINT#100000You need this when a block expects a DINT and you want to pass a small number without ambiguity.
Why does the compiler reject PT := 5000?
Because PT is a TIME and 5000 is an integer. The standard allows no implicit conversion here:
HiTmr(IN := Level > 85.0, PT := T#5s); (* correct *)
HiTmr(IN := Level > 85.0, PT := 5000); (* error *)Named constants
Every number in your program wants a name if it repeats or carries engineering meaning:
VAR CONSTANT
LevelHi : REAL := 85.0; (* high alarm, LAH-101 *)
LevelHiHi : REAL := 95.0; (* pump trip *)
AlarmDelay : TIME := T#2s; (* ISA-18.2 de-chatter *)
END_VARThis is not cosmetic. A year from now, when operations ask for 88, you change one number instead of hunting 85.0 through a thousand lines.
Try it in the lab
The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.
What to try
- Run the program and turn S1 on: the lamps count in binary from 0 to 15, then start over.
- Watch Count, Limit and Ten in the watch table: 2#1111 is 15 and 16#A is 10.
- Change Limit to Ten and download again: counting now wraps at 10.
- Change T#500ms to T#200ms to count faster.
People find this lesson by searching
structured text literalshexadecimal plc 16#binary literal structured textTIME literal T#CONSTANT structured texttyped literal INT#plc number formats
Course outline
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers