Agents4PLC: Automating Closed-loop PLC Code Generation and Verification in Industrial Control Systems using LLM-based Agents

Zihan Liu*

Zhejiang University

Email: zihanliu@zju.edu.cn

Ruinan Zeng*

Zhejiang University

Email: zengruinan@zju.edu.cn

Dongxia Wang

Zhejiang University

Email: dxwang@zju.edu.cn

Gengyun Peng

Zhejiang University

Email: pgengyun@zju.edu.cn

Jingyi Wang

Zhejiang University

Email: wangjyee@zju.edu.cn

Qiang Liu

Zhejiang University

Email: 22460396@zju.edu.cn

Peiyu Liu

Zhejiang University

Email: liupeiyu@zju.edu.cn

Wenhai Wang

UWin Tech & Zhejiang University

Email: zdzzlab@zju.edu.cn


*Indicates Equal Contribution

Corresponding Author
MY ALT TEXT

Workflow show.

Abstract

In industrial control systems, the generation and verification of Programmable Logic Controller (PLC) code are critical for ensuring operational efficiency and safety. While Large Language Models (LLMs) have made strides in automated code generation, they often fall short in providing correctness guarantees and specialized support for PLC programming. To address these challenges, this paper introduces Agents4PLC, a novel framework that not only automates PLC code generation but also includes code-level verification through an LLM-based multi-agent system. We first establish a comprehensive benchmark for verifiable PLC code generation area, transitioning from natural language requirements to human-written-verified formal specifications and reference PLC code. We further enhance our 'agents' specifically for industrial control systems by incorporating Retrieval-Augmented Generation (RAG), advanced prompt engineering techniques, and Chain-of-Thought strategies. Evaluation against the benchmark demonstrates that Agents4PLC significantly outperforms previous methods, achieving superior results across a series of increasingly rigorous metrics. This research not only addresses the critical challenges in PLC programming but also highlights the potential of our framework to generate verifiable code applicable to real-world industrial applications.

Problem Definition
Information Retrieval
Planning Phase

Problem Definition: The task begins with analyzing user input, such as the ST coding requirements.

Information Retrieval: The retrieval agent gathers relevant information from various sources.

Planning Phase: The planning agent generates and ranks actionable plans based on the retrieved data.

Coding Agent
Validation Phase
Final Output

Coding Agent: The coding agent generates the ST code based on the selected plan and checks for syntax errors.

Validation Phase: The validation agent checks the functional correctness of the generated code.

Final Output: If validation is successful, the final formally verified ST code is produced.

Overview of Control Interfaces

We conduct case studies utilizing the UWinTech Control Engineering Application Software Platform to test the potential of our code generation framework to be applied on practical PLC production environments.

Our experiment covers the key processes of LED control, motor management, and temperature monitoring, with experiment title, requirements and control results below, showcasing how the control system reacts to different inputs and conditions.

LED Control Interface
Tank Monitoring Interface

LED Control Interface

The LED control task uses two input variables (PB1, PB2) to determine whether the LED should be on or off, based on a logical AND operation between PB1 and the negation of PB2.

Tank Monitoring Interface

The motor control task checks if the motor should stop or start by comparing the pressure value to a set threshold, turning off the motor if the pressure is too low.

Motor Status Interface

Motor Status Interface

The temperature and relay task monitors pressure and temperature, adjusting relays and triggering alarms when the values exceed preset limits, also providing error codes for specific abnormal conditions.