ถ้าคุณเคยเขียน PLC แบบ Ladder Diagram มาตลอด แล้วเจอโปรแกรมที่เขียนด้วย Structured Text (ST) ครั้งแรก อาจรู้สึกว่ามันคล้าย Code Python หรือ C มากกว่า Ladder สัญลักษณ์ที่คุ้นเคย บทความนี้อธิบาย Structured Text ตั้งแต่พื้นฐาน เหมาะกับงานไหน และต่างจาก Ladder อย่างไร สำหรับวิศวกรที่ทำงานกับ PLC Mitsubishi และ Siemens ในโรงงานชลบุรีและระยอง
Structured Text (ST) คืออะไร?
Structured Text เป็น 1 ใน 5 ภาษา PLC มาตรฐาน IEC 61131-3 มีรูปแบบคล้าย High-Level Programming Language เช่น Pascal หรือ C PLC ที่รองรับ ST ได้แก่ Siemens S7-1200/1500 (TIA Portal), Mitsubishi iQ-R, iQ-F (GX Works3), Omron NJ/NX (Sysmac Studio), Beckhoff TwinCAT
Structured Text ต่างจาก Ladder Diagram อย่างไร?
| Ladder Diagram (LAD) | Structured Text (ST) | |
|---|---|---|
| รูปแบบ | Graphical — Rung, Contact, Coil | Text-based — IF/THEN, FOR, WHILE |
| เรียนรู้ง่ายถ้ามีพื้นฐาน | Electrical / Relay Logic | Programming / IT Background |
| เหมาะกับ | Sequential Logic, Basic I/O | Calculation, Algorithm, Complex Logic |
| อ่านง่ายเมื่อ | Logic ไม่ซับซ้อน | Logic ซับซ้อน มีการคำนวณ |
| Code ยาวแค่ไหน | 10 Condition = 10 Rung | 10 Condition = 10 บรรทัด IF |
| Debugging | เห็น State ทุก Contact Real-time | ต้องใช้ Watch Variable |
Syntax พื้นฐานของ Structured Text
IF/THEN/ELSIF/ELSE
(* ตัวอย่าง: ควบคุม Motor ตามอุณหภูมิ *)
IF Temperature > 80.0 THEN
Motor_Cool := TRUE;
Motor_Run := FALSE;
ELSIF Temperature > 60.0 THEN
Motor_Cool := FALSE;
Motor_Run := TRUE;
ELSE
Motor_Cool := FALSE;
Motor_Run := FALSE;
END_IF;
FOR Loop
(* คำนวณผลรวม Array 10 ตัว *)
Total := 0;
FOR i := 0 TO 9 DO
Total := Total + DataArray[i];
END_FOR;
Average := Total / 10;
CASE Statement
(* State Machine สำหรับ Recipe Selection *)
CASE MachineState OF
0: (* IDLE *)
Output_Enable := FALSE;
1: (* RUNNING *)
Output_Enable := TRUE;
ProcessTimer(IN:=TRUE, PT:=T#5S);
2: (* FAULT *)
Output_Enable := FALSE;
AlarmBit := TRUE;
END_CASE;
งานที่ Structured Text เหมาะมากกว่า Ladder
- การคำนวณทางคณิตศาสตร์ — สูตรซับซ้อน PID Manual, Scaling, Unit Conversion เขียนใน ST สั้นและอ่านง่ายกว่า Ladder มาก
- State Machine (Machine Sequence) — CASE Statement ทำให้เห็น State ทั้งหมดในที่เดียว ง่ายต่อการ Modify
- Array Processing — วนซ้ำ Process ข้อมูล Recipe, Quality Data ด้วย FOR Loop
- String Manipulation — จัดการ Serial Number, Batch Code, Barcode ที่เป็น String
- Algorithm ซับซ้อน — Interpolation, Moving Average, Statistical Analysis
ข้อควรระวังเมื่อใช้ Structured Text ใน PLC
- FOR/WHILE Loop ที่ Iterate มากใน 1 Scan — ทำให้ Scan Time ยาว อาจ Trigger Watchdog
- ไม่ตรวจสอบ Array Boundary — Access Index เกิน Array Size ทำให้ Data เสียหาย
- Floating Point ใน Safety Critical — ต้องระวัง NaN และ Infinity
- WHILE Loop ไม่มี Exit Condition ที่ชัดเจน — เสี่ยง Infinite Loop
ST กับ Ladder ใช้ร่วมกันได้ไหม?
ได้! PLC รุ่นใหม่อย่าง Mitsubishi iQ-R, Siemens S7-1500 และ Omron NJ สามารถใช้ทั้ง Ladder และ ST ในโปรเจกต์เดียวกันได้ Best Practice คือ:
- Ladder สำหรับ Basic I/O Control, Interlock, Safety Circuit
- Structured Text สำหรับ Calculation, State Machine, Algorithm
- Function Block (FB) เขียนด้วย ST แล้วเรียกใช้จาก Ladder
ควรเรียกผู้เชี่ยวชาญเมื่อไหร่?
- ต้องการ Debug โปรแกรม ST ที่ซับซ้อน
- ต้องการ Migrate โปรแกรม Ladder เก่าเป็น ST เพื่อ Maintainability
- ต้องการ Review โปรแกรม ST เพื่อความปลอดภัย
สรุป
Structured Text เป็นภาษา PLC ที่ทรงพลังมากสำหรับงานคำนวณและ Algorithm ที่ซับซ้อน ทำงานร่วมกับ Ladder ได้ดีเยี่ยม ทีม Xeniel Automation รับเขียนโปรแกรม PLC ทั้ง Ladder และ Structured Text สำหรับโรงงานในชลบุรีและระยอง
ต้องการวิศวกร PLC ที่เขียน Structured Text ได้?
Xeniel Automation รับเขียน PLC ทั้ง Ladder และ Structured Text ทุก Platform Mitsubishi Siemens Omron Beckhoff บริการนิคมชลบุรีและระยอง
ปรึกษาวิศวกรฟรี ทันที!โทร 098-965-3845 | LINE: @xeniel.24
ตัวอย่างโค้ด Structured Text ที่ใช้จริงในโรงงาน
ตัวอย่าง 1: PID Temperature Control
(* Temperature PID Control *)
IF Enable THEN
Error := Setpoint - ActualTemp;
Integral := Integral + (Error * ScanTime);
Derivative := (Error - PrevError) / ScanTime;
Output := (Kp * Error) + (Ki * Integral) + (Kd * Derivative);
(* Output Limiting *)
IF Output > MaxOutput THEN Output := MaxOutput; END_IF;
IF Output < 0.0 THEN Output := 0.0; END_IF;
PrevError := Error;
END_IF;
ตัวอย่าง 2: Recipe Management
(* Recipe Selection *)
CASE RecipeNo OF
1: Setpoint := 80.0; Speed := 150; MixTime := 120;
2: Setpoint := 95.0; Speed := 200; MixTime := 90;
3: Setpoint := 70.0; Speed := 100; MixTime := 180;
ELSE: Setpoint := 0.0;
END_CASE;
ตาราง: Ladder vs ST เลือกอะไรสำหรับงานแบบไหน
| ลักษณะงาน | แนะนำ Ladder | แนะนำ ST |
|---|---|---|
| Digital I/O Control ง่ายๆ | ✅ | |
| Sequence Control ทั่วไป | ✅ | |
| PID Control / Math | ✅ | |
| Recipe Management | ✅ | |
| String Processing | ✅ | |
| Array / Data Table | ✅ | |
| Function Block Reuse | ✅ | |
| Maintenance ทีมช่างทั่วไป | ✅ |
ควรเรียกผู้เชี่ยวชาญเมื่อไหร่?
- ต้องการ Rewrite โปรแกรม Ladder เก่าเป็น Structured โดยเพิ่ม Feature ใหม่
- ต้องการ PID Tuning หรือ Math-heavy Logic ที่ทำได้ยากใน Ladder
- ต้องการ Function Block Library ที่ Reuse ได้ทั้งโรงงาน
สรุป
Structured Text ไม่ใช่มาแทน Ladder แต่เป็นเครื่องมือเพิ่มเติมสำหรับงานที่ Logic ซับซ้อน โดยเฉพาะ PID, Recipe และ Math Calculation Xeniel Automation รับเขียนโปรแกรม PLC ทั้ง Ladder และ ST สำหรับโรงงานในชลบุรีและระยอง
ต้องการเขียนโปรแกรม PLC ด้วย Structured Text? ปรึกษา Xeniel
Xeniel Automation รับเขียนโปรแกรม PLC ทั้ง Ladder, ST, FBD สำหรับงาน PID, Recipe, Motion ออนไซต์ชลบุรีและระยอง
ปรึกษาฟรีทาง LINE โทร 098-965-3845