Writing PLC Programs That Maintenance Engineers Will Thank You For a Lifetime (Standardized Programming Guide)

In non-standard automation projects, the delivery of equipment is just the beginning; the real test lies in subsequent maintenance. A standardized PLC program not only allows maintenance engineers to quickly get up to speed but also significantly reduces equipment failure rates and maintenance costs.

Naming Conventions: Let the Program “Speak for Itself”

The Golden Rules of Variable Naming

Input Signal Naming Convention:

I_EquipmentLocation_Function_Status
Example:
I_Cylinder_A_Extend (Cylinder A extended to position)
I_Motor_Main_Run (Main motor running feedback)
I_Sensor_Height_Detect (Height detection sensor)

Output Signal Naming Convention:

O_EquipmentLocation_Function_Action
Example:
O_Cylinder_A_Extend (Cylinder A extending)
O_Motor_Main_Start (Main motor starting)
O_Valve_Vacuum_On (Vacuum solenoid valve on)

Internal Variable Naming Convention:

M_FunctionModule_SpecificFunction
T_FunctionModule_TimerPurpose
C_FunctionModule_CounterPurpose

Example:
M_Auto_Running (Automatic running mode)
T_Cylinder_Timeout (Cylinder action timeout)
C_Product_Count (Product count)

The benefits of such naming conventions are obvious: even engineers encountering the equipment for the first time can quickly understand the program logic through variable names.

Program Structure: Modularity is Key

Standard Program Architecture

Main Program (Main) Structure:

  1. System Initialization
  2. Safety Interlock Check
  3. Mode Selection
  4. Call Various Function Modules
  5. Fault Handling
  6. HMI Data Update

Principles for Function Module Division:

  • Divided by Mechanical Action: Feeding Module, Processing Module, Discharging Module
  • Divided by Control Logic: Safety Module, Communication Module, Diagnostic Module
  • Each module should control within 50-100 lines of code

Practical Program Template

//============ Safety Interlock Module ============
IF NOT (I_EmergencyStop AND I_SafetyDoor AND I_AirPressure) THEN
    M_Safety_OK := FALSE;
    O_MainPower_Cut := TRUE;
    RETURN;
ELSE
    M_Safety_OK := TRUE;
END_IF;

//============ Main Process of Automatic Mode ============
CASE Auto_Step OF
    0: // Initialization
        IF M_Safety_OK AND I_StartButton THEN
            Auto_Step := 10;
        END_IF;
    
    10: // Feeding Action
        CALL FB_Feeding();
        IF M_Feeding_Complete THEN
            Auto_Step := 20;
        END_IF;
    
    20: // Processing Action
        CALL FB_Processing();
        IF M_Processing_Complete THEN
            Auto_Step := 30;
        END_IF;
        
    30: // Discharging Action
        CALL FB_Discharging();
        IF M_Discharging_Complete THEN
            Auto_Step := 0;
            C_Product_Count := C_Product_Count + 1;
        END_IF;
END_CASE;

Comment Standards: Making Logic Clear

Three Levels of Comments

1. Program Header Comment

(*
Program Name: Spindle Control Module
Function Description: Control the start, stop, and speed of the spindle
Creation Date: 2024-08-22
Created By: Engineer Zhang
Modification Records:
2024-08-25 - Engineer Li - Added overload protection logic
2024-08-30 - Engineer Wang - Optimized speed control algorithm
*)

2. Function Block Comment

//========== Cylinder Control Function Block ==========// Input: Cylinder extend command, cylinder retract command// Output: Cylinder extend, cylinder retract, action complete signal// Function: Control cylinder action and monitor action completion//======================================

3. Key Logic Comment

// Interlock logic to prevent simultaneous extension and retraction of the cylinder
IF I_Cylinder_ExtendCmd AND NOT I_Cylinder_RetractCmd THEN
    O_Cylinder_Extend := TRUE;
    O_Cylinder_Retract := FALSE;
END_IF;

Fault Handling: Prevention is Better than Cure

Comprehensive Alarm Mechanism

Alarm Level Definitions:

  • Severe Alarm: Immediate shutdown, manual reset required
  • General Alarm: Provides a prompt, can continue running
  • Reminder Information: Status prompt, no action required

Standard Alarm Handling Process:

//========== Alarm Handling Module ==========// Cylinder action timeout alarm
IF O_Cylinder_Extend AND NOT I_Cylinder_Extended THEN
    T_Cylinder_Timeout(IN := TRUE, PT := T#5S);
    IF T_Cylinder_Timeout.Q THEN
        Alarm_Array[1].Active := TRUE;
        Alarm_Array[1].Message := 'Cylinder A extension timeout';
        Alarm_Array[1].Level := 2; // Severe Alarm
        M_Auto_Stop := TRUE;
    END_IF;
ELSE
    T_Cylinder_Timeout(IN := FALSE);
END_IF;

Fault Diagnosis Function

Device Status Monitoring:

  • Real-time monitoring of key sensor status
  • Recording of actuator action times
  • Trend analysis of system operating parameters

Historical Data Recording:

// Fault record structure
TYPE Fault_Record:
STRUCT
    DateTime: DATE_AND_TIME;     // Fault time
    FaultCode: INT;              // Fault code
    FaultDesc: STRING[50];       // Fault description
    Duration: TIME;              // Fault duration
END_STRUCT
END_TYPE

Performance Optimization: Details Determine Success or Failure

Scan Cycle Optimization

High-Frequency Tasks (Fast Scan):

  • Safety interlock detection
  • Emergency stop signal processing
  • Key position feedback

Low-Frequency Tasks (Slow Scan):

  • HMI data updates
  • Historical data recording
  • Communication status checks

Memory Usage Optimization

Choosing Appropriate Data Types:

  • Use BOOL for boolean values, not INT
  • Use UINT for counting to save memory
  • Use REAL for analog values like temperature and pressure

Avoiding Redundant Calculations:

// Incorrect Writing
IF (Sensor_Value * 0.1 + 273.15) > 350.0 THEN
    Alarm_HighTemp := TRUE;
END_IF;

// Correct Writing
Temperature := Sensor_Value * 0.1 + 273.15;
IF Temperature > 350.0 THEN
    Alarm_HighTemp := TRUE;
END_IF;

Maintenance-Friendly Design Details

HMI Interface Coordination

Standardization of Status Display:

  • Green: Normal operation
  • Yellow: Warning status
  • Red: Fault shutdown
  • Gray: Shutdown status

Operation Button Layout:

  • Start button: Green, on the right
  • Stop button: Red, on the left
  • Reset button: Yellow, in the middle

Debugging Interface Reservation

//========== Debug Mode ============
IF M_Debug_Mode THEN
    // Step execution function
    IF I_Debug_Step THEN
        Auto_Step := Auto_Step + 1;
    END_IF;
    
    // Forced output function (only in debug mode)
    IF M_Force_Enable THEN
        O_Debug_Output := I_Force_Value;
    END_IF;
END_IF;

Version Management and Documentation

Program Version Control

Every time the program is modified, it should:

  1. Update the version number (V1.0 → V1.1)
  2. Record modification content and reasons
  3. Backup the previous version of the program
  4. Update technical documentation

Delivery Document Checklist

  • PLC program source files and compiled files
  • Variable address allocation table
  • Fault code reference table
  • User manual
  • Maintenance manual

In Conclusion

Standardized PLC programming is not just a technical issue; it is also a reflection of professional quality. When maintenance engineers are at a fault site in the middle of the night, being able to quickly locate problems through clear program logic brings them gratitude beyond your imagination.

A good program speaks for itself, and excellent programmers convey warmth through code. Every standardized variable name, every detailed comment, and every thoughtful debugging interface is the best respect for peers.

This is not only a technical legacy but also a manifestation of the engineer’s spirit.

Leave a Comment