El texto estructurado (del inglés, Structured Text), abreviado como ST o STX, es uno de los cinco lenguajes soportados por el estándar IEC 61131-3, y fue diseñado para la programación de controladores lógicos programables (PLC).[1][2] Es un lenguaje de alto nivel estructurado en bloques y se basa en el Pascal, por lo que su sintaxis es muy parecida.[3] Todos los lenguajes de programación comparten elementos comunes de la IEC61131. Las variables y llamadas a funciones están definidas por elementos comunes, por lo que se pueden utilizar diferentes lenguajes dentro del estándar IEC 61131-3 en el mismo programa.
Se admiten declaraciones complejas e instrucciones anidadas:
Una máquina de estado simple (PASCAL)
(* simple state machine *) TxtState := STATES[StateMachine]; CASE StateMachine OF 1: ClosingValve(); StateMachine := 2; 2: OpeningValve(); ELSE BadCase(); END_CASE;
Por el contrario de otros lenguajes de programación, no hay ninguna solución alternativa para el CASE: al detectar la primera condición coincidente, salta al código de su afirmación y sale de todo el CASE; en caso negativo, seguirá revisando los demás CASE hasta el ELSE, instrucciones a cumplir por defecto en caso de no encontrar ninguna coincidencia anterior; terminando el bloque de código en el END_CASE.
Configuración de la PLC (PASCAL)
// PLC configuration CONFIGURATION DefaultCfg VAR_GLOBAL b_Start_Stop : BOOL; // Global variable to represent a boolean. b_ON_OFF : BOOL; // Global variable to represent a boolean. Start_Stop AT %IX0.0:BOOL; // Digital input of the PLC (Address 0.0) ON_OFF AT %QX0.0:BOOL; // Digital output of the PLC (Address 0.0). (Coil) END_VAR // Schedule the main program to be executed every 20 ms TASK Tick(INTERVAL := t#20ms); PROGRAM Main WITH Tick : Monitor_Start_Stop; END_CONFIGURATION PROGRAM Monitor_Start_Stop // Actual Program VAR_EXTERNAL Start_Stop : BOOL; ON_OFF : BOOL; END_VAR VAR // Temporary variables for logic handling ONS_Trig : BOOL; Rising_ONS : BOOL; END_VAR // Start of Logic // Catch the Rising Edge One Shot of the Start_Stop input ONS_Trig := Start_Stop AND NOT Rising_ONS; // Main Logic for Run_Contact -- Toggle ON / Toggle OFF --- ON_OFF := (ONS_Trig AND NOT ON_OFF) OR (ON_OFF AND NOT ONS_Trig); // Rising One Shot logic Rising_ONS := Start_Stop; END_PROGRAM
Bloque de Función en PASCAL para una PLC
//======================================================================= // Function Block Timed Counter : Incremental count of the timed interval //======================================================================= FUNCTION_BLOCK FB_Timed_Counter VAR_INPUT Execute : BOOL := FALSE; // Trigger signal to begin Timed Counting Time_Increment : REAL := 1.25; // Enter Cycle Time (Seconds) between counts Count_Cycles : INT := 20; // Number of Desired Count Cycles END_VAR VAR_OUTPUT Timer_Done_Bit : BOOL := FALSE; // One Shot Bit indicating Timer Cycle Done Count_Complete : BOOL := FALSE; // Output Bit indicating the Count is complete Current_Count : INT := 0; // Accumulating Value of Counter END_VAR VAR CycleTimer : TON; // Timer FB from Command Library CycleCounter : CTU; // Counter FB from Command Library TimerPreset : TIME; // Converted Time_Increment in Seconds to MS END_VAR // Start of Function Block programming TimerPreset := REAL_TO_TIME(in := Time_Increment) * 1000; CycleTimer( in := Execute AND NOT CycleTimer.Q, pt := TimerPreset); Timer_Done_Bit := CycleTimer.Q; CycleCounter( cu := CycleTimer.Q, r := NOT Execute, pv := Count_Cycles); Current_Count := CycleCounter.cv; Count_Complete := CycleCounter.q; END_FUNCTION_BLOCK