Skip to main content

6.2.1 Introduction to ST Language

6.1.2.1 Introduction to ST

  • Structured text (ST) is a high-level text language that can be used to describe the behavior of functions, function blocks and programs. It can also describe the behavior of steps, actions and transitions in sequential functional flow charts. In complex control systems, structured text can greatly reduce the amount of code and make complex system problems simpler. Compared with graphical programming languages, its disadvantage is that debugging is not intuitive
  • ST language program execution sequence, the program execution sequence using structured text is executed sequentially from top to bottom according to the "line number". At the beginning of each cycle, the program line with the smaller line number is executed first
warning
  • Syntax and Structure
    Keywords: ST uses keywords (such as IF, FOR, WHILE, CASE, FUNCTION, etc.) to control the program flow, ensure that the keywords are spelled correctly, and avoid spelling errors that lead to grammatical errors
    Semicolon: A semicolon (;) must be added after each statement to mark the end of the statement
    Indentation and comments: Proper indentation and adding comments can help improve the readability of the code. Use (comment content) for multi-line comments
  • Data type
    Choose the appropriate data type: ST supports multiple data types (such as BOOL, INT, REAL, STRING, etc.). When defining variables, user should choose the most suitable data type to optimize memory usage and program efficiency
    Arrays and structures: Proper use of arrays and structures can improve code organization and readability. Ensure that subscript access to the array is legal and avoid out-of-bounds access
  • Control structure
    Conditional statements: When using IF statements, make sure the logic is clear and avoid nesting too deeply to increase readability. Consider using CASE statements instead of multiple levels of IF statements to improve code readability
    Loop control: When using FOR and WHILE loops, ensure that the loop condition terminates at the appropriate time to prevent infinite loops
  • Function and Program Organization
    Modular programming: Divide code into small, reusable functions and procedures to improve code maintainability and readability
    Local variables: Use local variables as much as possible to reduce dependence on global variables, thereby reducing program complexity
  • Error handling
    Exception handling: The ST language itself does not provide a built-in exception handling mechanism, so status checking and error handling logic need to be added to the program to ensure the robustness of the program
    Debugging information: During debugging, add debugging information, such as status output or logs, to help track program execution
  • Performance Optimization
    Avoid unnecessary calculations: Avoid repeatedly calculating the same expression in a loop, and try to keep constants and unchanging values outside the loop
    Optimize algorithms: Choose more efficient algorithms and data structures to improve execution efficiency
  • Hardware Limitations
    Understand PLC hardware: When writing a program, consider the limitations of the PLC hardware used (such as memory, processing power, etc.) to ensure that the program can run smoothly
  • Coding Style
    Consistency: Maintain consistency in code style (such as naming rules, indentation style, etc.) to facilitate team collaboration and code maintenance
  • Documentation and Comments
    Detailed documentation: When writing code, make sure to add detailed documentation and comments so that other developers understand the function and intent of the code