Introduction
PLCSim Advanced is one of the best PLC simulators in the world and the PLCSim Advanced API is one of the reasons.
In this article I will first introduce what is PLCSim Advanced API and provide a simple demonstration of it.
What is PLCSim Advanced API
PLCSim Advanced API (application programming interface) enables the interaction between PLCSim Advanced and external software.
The API supports C++ and C# hence it supports cross platform external software including but not limited to Windows and Linux.
What Difference Does the API Make
PLCSim advanced has most of the software verification needs covered until the simulation needs to take real world scenarios into consideration. When these things need to be considered, the PLCSim Advanced API becomes something indispensable.
With PLCSim Advanced API, you can do the below things.
- Simulate hardware behavior and signals.
- Synchronize PLCSim Advanced with the external software - cosimulation.
In terms of hardware behaviour and signal simulation. With the OPC communication, the PLC IO signals can be partially simulated by the external software writing to the PLC’s interface DB and inside the PLC some simulation service program to map the DB’s values to the IO tags. However, this requires extra programs in the PLC to support the simulation and the programs will likely be brought to production which is not ideal. Furthermore, it is not possible to simulate hardware behaviors like hardware interrupts without the PLCSim Advanced API.
In terms of cosimulation, since PLCSim Advanced runs in realtime by default and external simulation software may not (for example, Matlab/Simulink can take minutes to simulate a few seconds of the model runtime), PLCSim Advanced needs to synchronize with the external simulation software to get the simulation going. This is possible with the PLCSim Advanced API.
Below I’ll provide a simple demonstration of how PLCSim Advanced API can simulate a hardware interrupt.
A Simple Demonstration
Preparation
In this demonstration, I’m using Visual Studio and code in C#. The .Net framework version should be 4.8 to get the application going. After installing the PLCSim Advanced package, you need to add the API as a reference to your project. The .dll file by default installation is stored in C:\Program Files (x86)\Common Files\Siemens\PLCSIMADV\API.
The API offers 4 interfaces:
- IBaseRuntimeManager.
- ISimulationRuntimeManager.
- IInstances.
- IRemoteRuntimeManager.
In this demonstration, I will be using IInstances only.
Below is a brief introduction to the 4 interfaces.
IBaseRuntimeManager
The base interface of the PLCSim Advanced runtime manager. It is used to manage runtime instances (create, search, receive runtime instances). One runtime manager can manage up to 16 runtime instances.
ISimulationRuntimeManager and IRemoteRuntimeManager
They are derived from IBaseRuntimeManager and are used to handle events, manage connections with other runtime managers.
IInstances
This is the interface of a runtime instance. It is used to handle the runtime instances’ events, states, IO values, etc.
Get the PLCSim Advanced Instance Going
The API is extremely easy to use. Simply create the following module, class and methods.
namespace PLCSimAdv_API_Test.Module
{
public class PLCSimAdvInstance
{
public IInstance instance { get; set; }
public void PLCInstance(string instanceName)
{
instance = SimulationRuntimeManager.RegisterInstance(instanceName);
}
public void PLCInstance_ConfigIP(SIPSuite4 instanceIP)
{
if (instance.ControllerIP.ToString() != instanceIP.ToString())
{
instance.SetIPSuite(0, instanceIP, false);
}
}
}
}
Then in the application’s program I created a simple control interface to register the instance, configure its IP address and trigger the hardware interrupt.
namespace PLCSimAdv_API_Test
{
internal class Program
{
static void Main(string[] args)
{
ushort seqNum;
PLCSimAdvInstance module = new PLCSimAdvInstance();
Console.WriteLine("Press any key to initialize the PLC simulation instance");
Console.ReadKey(true);
module.PLCInstance("API_Test");
Console.WriteLine("Press any key to start the PLC simulation instance");
Console.ReadKey(true);
module.instance.PowerOn(60000);
Console.WriteLine("Press any key to set the PLC simulation instance X1 IP address");
Console.ReadKey(true);
SIPSuite4 instanceIP = new SIPSuite4("192.168.0.101", "255.255.255.0", "0.0.0.0");
module.PLCInstance_ConfigIP(instanceIP);
Console.WriteLine("Press any key to trigger DI hardware interrupt");
Console.ReadKey(true);
module.instance.ProcessEvent(258, 0, EProcessEventType.RisingEdge, out seqNum);
}
}
}
Afterward, simply run the console application and it can register the PLCSim Advanced instance and configure it.
Download PLC Project to the Simulation Instance
In this demonstration, I have created a simple PLC project with a S7-1511C. On the PLC’s 1st digital input, I have configured a rising edge hardware interrupt that will trigger the CPU to run OB40 Hardware Interrupt.
The hardware interrupt OB40 will write “Yes, it has been triggered” to a global DB if the hardware interrupt reaches the PLC.
After downloading this PLC project to the PLCSim Advanced instance, I can go online to the instance and see that the DB’s string element currently is empty.
Trigger Hardware Interrupt
In the C# console application, I have programmed the below line to trigger the hardware interrupt.
module.instance.ProcessEvent(258, 0, EProcessEventType.RisingEdge, out seqNum);
258 is the hardware ID of the digital input module. The 2nd input parameter is 0 because I want the hardware interrupt to be sent to the digital input module’s channel 0 which is the channel that I have configured for the rising edge hardware interrupt. EProcessEventType.RisingEdge specifies that the interrupt is a rising edge interrupt and the out seqNum is an event number which is not further used in this case.
After I triggered the hardware interrupt from the API side, my PLCSim Advanced instance received the hardware interrupt and OB40 was triggered. The program inside OB40 writes the string to the DB. This hardware behaviour and response is not possible without the PLCSim Advanced API.
Conclusion
The PLCSim Advanced API is an advanced software interface for the already fantastic PLC simulation software and provides amazing features like hardware events simulations and simulation synchronization. It allows us to integrate our external simulations to the PLCSim Advanced hence we can achieve more than just PLC software validations.