SoftPLC

The integrated SoftPLC executes IEC 61131-3 PLC programs directly inside ProtoTwin. You can use it to develop and test control logic for your machines without a physical PLC, and then easily transfer the logic to a real PLC. The SoftPLC uses the same clock as the simulation, so it is completely deterministic and the simulation can run faster or slower than real-time.

The SoftPLC supports two languages:

  • Structured Text: A high-level textual language, similar to Pascal. The compiler supports IEC 61131-3 (Edition 4), including object-oriented programming, and the PLCopen Motion Control function blocks.
  • Function Block Diagram: A graphical language, where function blocks are connected together with lines.

Contents

Advantages

Developing control logic with the SoftPLC, before transferring it to a physical PLC, has several advantages:

  • Faster iterations: The programs, the model and the simulation are all in one place. There is no PLC to wire up, no network connection to configure and no program to download, so you can test a change moments after making it.
  • Developer experience: The script editor provides code completion, signature help, documentation on hover and go to definition. Syntax and semantic errors are shown as you type, before the program runs.
  • Determinism: The SoftPLC and the simulation share the same clock, so a simulation gives the same result every time it runs. A problem that occurs once can be reproduced and fixed, and a fix can be verified by running the simulation again.
  • Not limited to real-time: A physical PLC runs on its own clock, so the simulation must keep up with it in real-time. The SoftPLC runs at the speed of the simulation, so you can run at maximum speed to test long sequences quickly, or slow down, pause and step through the simulation to examine fast events.
  • Debugging: You can view and force variables while the simulation runs, and log messages to the console with the WRITE function. Runtime errors, such as division by zero or an array index out of bounds, stop the simulation and report the file and line where they occurred.
  • Standard languages: Programs are written in IEC 61131-3 languages and use the PLCopen Motion Control function blocks, so the logic can be quickly and easily transferred to a physical PLC.
  • Education: Students can learn PLC programming without a physical PLC or vendor-specific software, and see their programs control a machine immediately. The languages follow IEC 61131-3 standard, so the skills transfer to PLCs from any vendor. The SoftPLC is beginner friendly: the editor suggests code and explains errors as you type, and mistakes are safe to make, since they cannot damage a real machine.
  • AI Assistant: Our integrated assistant Torq can read and write Structured Text and axis definition files. Torq sees the same compiler errors as you see in the editor. You can ask Torq to implement control logic from a description, fix errors, or explain why a program does not behave as expected.

Creating Programs

SoftPLC programs are created in the script editor. Click the add button in the file explorer and enter a name for the file. In the template window, select SoftPLC and then choose one of the following items:

  • Structured Text: Creates a Structured Text file with the .st extension.
  • Function Block Diagram: Creates a Function Block Diagram file with the .fbd extension.
  • Axis: Creates an axis definition file with the .axis extension, which configures an axis for motion control.

The first Structured Text file you create is automatically populated with an empty program, which is named after the file:

PROGRAM Main
VAR
END_VAR

END_PROGRAM

All Structured Text and axis files are compiled together as a single project. This means that you can declare functions, function blocks and data types in separate files and use them from any other file. Syntax and semantic errors are immediately shown in the editor as you type. If the project fails to compile when the simulation starts, the SoftPLC runs the last version that compiled successfully and displays a message in the console.

Scan Cycle

A physical PLC executes its programs repeatedly in a scan cycle. Similarly, the SoftPLC executes one scan cycle for each simulation step:

  1. The values of the input tags are read from the model.
  2. The programs are executed.
  3. The values of the output tags are written to the model.
  4. The simulation is advanced by one timestep.

The inputs therefore reflect the state of the model at the end of the previous step, just like the input image of a physical PLC. The timestep is set in the physics settings, and is 10 ms by default. Without a configuration, every Structured Text program is executed once per scan. A configuration allows you to run programs in tasks with different intervals.

Connecting to the Model

The SoftPLC is connected to the model through the IO Browser, in the same way as a physical PLC:

  1. Open the IO Browser and click the Add Server button.
  2. Select the SoftPLC protocol, enter a name for the server and click Save.
  3. Select the created SoftPLC server from the list and click the Import button to automatically import all its addressed variables as tags.
  4. Bind the tags to the signals of the components in your model, such as the state of a sensor or the target position of a motor.

You will need to import the tags again after adding or renaming variables.

Tags

The variables of a Structured Text program that become tags are:

  • Variables with an explicit address, such as Start AT %IX0.0 : BOOL. The area of the address determines the area of the tag. Addresses starting with %I are inputs, addresses starting with %Q are outputs, and addresses starting with %M are memory.
  • Global variables that are not constant. Global variables without an address are memory tags.
  • The properties of each motion axis.

Local variables without an address are internal to the program and do not become tags. Each tag is named by its path in the program:

Variable Tag
Start declared in program Main Main.Start
Lamp declared in function block instance Station of program Main Main.Station.Lamp
Speed declared in a file-level VAR_GLOBAL GVL.Speed
Start declared in program instance Controller of configuration Cell Cell.Controller.Start
Rate declared in a VAR_GLOBAL of configuration Cell Cell.Rate
ActualPosition property of axis Carriage Carriage.ActualPosition

The data type of each tag is determined by the type of the variable:

IEC Type Tag Type
BOOL Bit
SINT Int8
INT Int16
DINT Int32
USINT, BYTE UInt8
UINT, WORD UInt16
UDINT, DWORD UInt32
REAL Float
LREAL Double

Variables of other types, such as LINT, TIME, STRING, arrays and structures, cannot be tags. Located variables of these types are skipped.

Debugging

Variables

Click the Show Variables button in the toolbar displayed at the top right corner of the script editor to show the variables of the SoftPLC. The values are updated as the simulation runs. Select a value to write a new value to the variable. You can also force a variable, which holds its value before and after every scan until it is released.

Console

The WRITE function writes a message to the console, which is useful for tracing the execution of a program.

WRITE('Station ready');
WRITE(Counter.CV);

You can pass any variable or object. It will be automatically converted to a string before being written to the console.

Runtime Errors

The SoftPLC checks for errors as the program runs. These include division by zero, arithmetic overflow, array indices that are out of bounds, values outside of a subrange, dereferencing a NULL reference, and a failed ASSERT. A runtime error stops the simulation and is reported in the console, along with the file and line where it occurred.