Clear Filters
Clear Filters

How can I interface to PLCs with MATLAB or Simulink?

94 views (last 30 days)
I would like to know if MATLAB/Simulink comes with drivers to interface to PLCs like the Modicon or Allen Bradley PLC.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Jun 2023
Edited: MathWorks Support Team on 2 Jun 2023
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) Industrial Communication Toolbox (formerly OPC Toolbox)

The Industrial Communication Toolbox can be used with MATLAB code or Simulink models to communicate with an OPC Server connected to your PLC. The Industrial Communication Toolbox contains 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 Industrial Communication 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 block library to use live OPC data as inputs to your model and update the OPC server with your model outputs. The OPC 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 block library.
More information about Industrial Communication Toolbox is available here:

3) Embedded Coder

Some PLCs can be programmed using C code. 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 Embedded Coder is available here:

4) Simulink Real-Time 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 your hardware.A full list of supported protocols can be found here:
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 an RS-232 driver block that feeds 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 okay but OPC is too slow (or no server is available) you can 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: https://www.youtube.com/watch?v=fJpb1UHAZcsHowever, 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.

More Answers (0)

Categories

Find more on Simulink PLC Coder in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!