AI Code Generation
Use natural language to generate Structured Text code
AI Code Generation
PLC Studio AI's code generation feature lets you describe what you want in plain English, and the AI writes the Structured Text code for you.
Using the AI Chat
The AI chat is your primary interface for code generation. You'll find it in the right panel of the PLC Studio AI interface.
Basic Usage
- Select a POU in the project explorer (or create a new one)
- Type your request in the chat
- Review the generated code in the diff view
- Accept or modify the changes
Example Prompts
Here are some effective prompts to get you started:
Creating a new function block:
Create a function block called FB_MotorControl with:
- Input: bStart (BOOL), bStop (BOOL), rSpeed (REAL)
- Output: bRunning (BOOL), bError (BOOL)
- Logic: Start sets running, stop clears it, error if speed > 100Adding functionality:
Add a timer that delays the motor start by 2 seconds after bStart goes TRUEImplementing algorithms:
Implement a PID controller with configurable Kp, Ki, and Kd parametersGenerating boilerplate:
Create the state machine structure for a 5-step sequence controlUnderstanding AI Responses
When you send a request, the AI will:
- Analyze your request - Understanding what you want to achieve
- Consider context - Looking at existing code and project structure
- Generate code - Writing Structured Text that meets your requirements
- Explain changes - Providing a summary of what was generated
The generated code appears in the diff view, where you can review it before accepting.
Tips for Better Results
Be Specific About Data Types
// Less effective
"Create a variable for temperature"
// More effective
"Create a REAL variable called rTemperature with initial value 20.0"Reference Existing Code
// The AI knows your project context
"Add error handling to the FB_Pump function block similar to FB_Motor"Break Down Complex Requests
// Instead of one large request, break it down:
1. "Create the basic structure for FB_ConveyorControl"
2. "Add the speed control logic with ramping"
3. "Add the emergency stop handling"Ask for Explanations
"Explain what this code does: [paste code]"
"Why did you use a TON timer instead of TOF?"Code Generation Modes
Generate New Code
Create entirely new POUs, variables, or code sections:
"Create a new function block FB_ValveControl for a 3-way valve"Modify Existing Code
Update or enhance existing code:
"Add input validation to the current function"
"Refactor this to use a CASE statement instead of IF-ELSE"Complete Code
Let AI finish what you started:
"Complete this function block based on the interface I've defined"Limitations
The AI code generation has some limitations:
- Structured Text only - Other IEC 61131-3 languages are view-only
- No hardware access - AI cannot directly interact with your PLC
- Context window - Very large projects may not fully fit in context
- No real-time data - AI doesn't have access to live PLC values
Best Practices
- Always review generated code - AI can make mistakes
- Test thoroughly - Generated code should be tested like any other code
- Start simple - Begin with small requests and build up
- Provide context - The more context you give, the better the results
- Iterate - Don't expect perfection on the first try