How can I communicate through Modbus protocol in Simulink?

How can I communicate through Modbus protocol in Simulink?

 Accepted Answer

As of R2024b, you can use the built-in blocks: 
For releases prior to R2024b, please consider the following:
Modbus communication over TCP/IP or Serial RTU is available in MATLAB as part of Industrial Communication Toolbox (in R2017a through R2021b, as part of Instrument Control Toolbox).
There are two workarounds for supporting Modbus communication in Simulink:
1) Write your own custom block to implement the Modbus functionality. This approach is the most flexible, and can support code generation. It is often done using S-functions. More information on S-functions can be found here: https://www.mathworks.com/help/simulink/blockset-authoring.html
The following guide on writing device drivers for Simulink may be a useful reference as well:https://www.mathworks.com/matlabcentral/fileexchange/39354-device-drivers
2) Use the Industrial Communication Toolbox 'modbus' object in Simulink using a MATLAB Function block. Note that this approach is limited in some ways, and does not support code generation.
There is an attached example model which shows how this approach might be implemented. Since MATLAB function blocks generate code, it is necessary to use "coder.extrinsic" on the MODBUS functions to instruct the code generator to skip these functions. More information on "coder.extrinsic" can be found here: https://www.mathworks.com/help/simulink/slref/coder.extrinsic.html
Also, note that we only create the Modbus object once by declaring it as a persistent variable. Because of this, you cannot use multiple of the Modbus read blocks within your model. It would be necessary to add extra logic to use multiple read blocks, so only one Modbus object is created for use by all blocks.

More Answers (0)

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!