How can I interface to PLCs like the Modicon or Allen Bradley PLC with MATLAB or Simulink?
77 views (last 30 days)
MathWorks Support Team on 19 May 2021
Edited: MathWorks Support Team on 19 May 2021
There are several options for interfacing MATLAB and Simulink with PLCs.
1) Simulink PLC Coder:
Simulink PLC Coder generates hardware-independent IEC 61131 structured text from Simulink models, Stateflow charts, and Embedded MATLAB functions. The structured text is generated in PLCOpen XML and other file formats supported by widely used integrated development environments (IDEs). As a result, you can compile and deploy your application to numerous programmable logic controller (PLC) and programmable automation controller (PAC) devices.
Simulink PLC Coder generates test benches that help you verify the structured text using PLC and PAC IDEs and simulation tools. Support for industry standards is available through IEC Certification Kit (for IEC 61508 and IEC 61511).
More information about Simulink PLC Coder and IEC Certification Kit is available here:
2) OPC Toolbox
The OPC Toolbox can be used with MATLAB code or Simulink models to communicate with an OPC Server connected to your PLC. The OPC Toolbox is a collection of functions that extend the capability of the MATLAB numeric computing environment, and blocks that extend the Simulink dynamic system simulation environment. Using the OPC Toolbox, you can acquire live OPC data directly into MATLAB and Simulink, and write data directly to the OPC server from MATLAB and Simulink.
When working in the Simulink modeling and simulation environment, you can use blocks from the OPC Toolbox block library to use live OPC data as inputs to your model and update the OPC server with your model outputs. The OPC Toolbox block library includes the capability of running Simulink models in pseudo real time, by slowing the simulation to match the system clock. You can prototype control systems, provide plant simulators, and perform optimization and tuning tasks using Simulink and the OPC Toolbox block library.
More information about OPC Toolbox is available here:
3) Real-Time Workshop Embedded Coder
Some PLCs can be programmed using C code. Real-Time Workshop Embedded Coder generates C code from Simulink and Stateflow models that has the clarity and efficiency of professional handwritten code. The generated code is exceptionally compact and fast. Full support is provided for the integration of legacy applications, functions, and data. Support for industry standards is available through IEC Certification Kit (for IEC 61508 and ISO 26262) and DO Qualification Kit (for DO-178).
More information about Real-Time Workshop Embedded Coder is available here:
4) Simulink Real-time/Speedgoat to communicate with fieldbus protocols. MathWorks also support fieldbus protocols, such as Ethernet/IP and Modbus RTU through Simulink Real-time (timing of the fieldbus messages require a real-time machine). Almost all PLCs support one form of fieldbus protocol or another, so again it will depend on their hardware.
A full list of supported protocols can be found here:
An AE has personally used a Speedgoat to communicate to a Allen-Bradley PLC using Ethernet/IP. Using Simulink Real-time External mode, you stream this data back to Simulink during execution. In addition to streaming the data to Simulink, the Speedgoat system allows for real-time testing, as well.
5) Instrument Control toolbox with Serial communication. The Instrument control toolbox does have RS-232 driver block that feed directly into Simulink. However, you would have to configure this block to work with the DF1 protocol (a common industrial RS-232 protocol) which would require an understanding of the DF1 messaging. This option would not work "out of the box".
6) Desktop Simulation over TCP/IP or UDP. If Desktop simulation is OK but OPC is too slow (or no server available) I would also consider a solution over TCP/IP or UDP.This has been seen with Siemens, B&R, and Beckhoff PLCs. The idea is to have an S-function that pauses the simulation after each step, sends & receives parameters over TCP/IP or UDP to/from the PLC, and then continues with the next time step.Example:
However, this is not out of the box and requires solid knowledge of both MathWorks tools and the IDE (RsLogix), or support from our consulting group.
Important Questions to Ask:
1) Do they want to run a (hand written) controls program on the PLC against a plant model in Simulink?2) How fast does the data exchange have to be (minutes, seconds, milliseconds)?3) How much data is exchange in each steps (a couple of values or thousands of parameters)?- a) If this is slow and little data --> connection over OPC should be the easiest way (out of the box), also MathWorks supports the current OPC standard OPC UA (Unified Architecture) since R2015b.4) Is it enough to run (co-simulate) the PLC program (real-time) against a Desktop simulation (non-real-time) in Simlink? If so, then no SLRT needed.5) Or do they need both sides to be real-time? If so, then use SLRT on Speedgoat hardware and connect over an industrial fieldbus (e.g. Ethernet/IP).
6) Are they interested in generating the PLC program from a Simulink model? If so, then use PLC Coder for IEC 61131-3 Structured Text code generation.
More Answers (2)
dhwani shah on 19 May 2017
Is there need any hardware for using opc tool box for matlab and plc connection
Arsalan Shah on 7 Aug 2019
Where can I find more details on this. I would like to implement this
This has been seen with Siemens, B&R, and Beckhoff PLCs. The idea is to have an S-function that pauses the simulation after each step, sends & receives parameters over TCP/IP or UDP to/from the PLC, and then continues with the next time step.
But I can not find any guidance at all. Can you link me to any tutorial or literature?