How to run Modbus TCP in real time using Simulink?

3 ビュー (過去 30 日間)
Bartosz Soltowski
Bartosz Soltowski 2021 年 8 月 30 日
編集済み: Van Tu Duong 2024 年 5 月 9 日
Hi,
I have a Matlab script to communicate with my relay using Modbus TCP. The script was generated using Modbus Explorer in Matlab.
The code works well. It turns on/off my relay according to specified conditions as well as reads its status.
I am now trying to execute Matlab code from my Simulink model. I imported Matlab Function block which includes Matlab code (generated before) to control the relay. When I click Run button, I am getting error below:
Cannot write 'double' data to the buffer. The input data have to be 'uint8' type. Function 'MATLAB Function' (#24.1132.1168), line 31, column 1: "write(m, 'coils', 1000, 1, serverId)"
I tried to change names of my variables (as someone suggested that this could be the issue), I also changed all data types but I could not fix it.
I can't understand why am I getting this error if the same code executed in Matlab works fine?
I would be really grateful for your help!

回答 (4 件)

Bazarov
Bazarov 2022 年 7 月 11 日
Hi,
I have the same problem. Can anybody help?

Akshay Jankee
Akshay Jankee 2023 年 2 月 2 日
Hi Bartosz and Bazarov,
I also need to do something similar. Have you managed to sort out this issue?
Best regards,
Akshay

TryHarder
TryHarder 2023 年 11 月 22 日
編集済み: TryHarder 2023 年 11 月 22 日
Hi everyone,
I have the same issue when I add a constant as an input of my function.
FWIW, the following function works nice from a MatLab function in Simulink :
function dataWrite = dataWrite
modbustype = 'tcpip';
modbusserver = '192.168.0.80';
modbusport = 502 ;
m = modbus(modbustype, modbusserver,modbusport);
write(m,'coils', 1, 1) ;
dataWrite = 1;
But with a constant as an input (activated with a button (on-off)), it did not work and I get the same error message...
function dataWrite = dataWrite(input)
modbustype = 'tcpip';
modbusserver = '192.168.0.80';
modbusport = 502 ;
value = uint8(input) % also test with double(input) or without cast in ordre to debug...
m = modbus(modbustype, modbusserver,modbusport);
write(m,'coils', 1, value) ;
dataWrite = value ; % To a display bloc
If someone had faced the same issue, I am all ears
Thx

Van Tu Duong
Van Tu Duong 2024 年 5 月 9 日
編集済み: Van Tu Duong 2024 年 5 月 9 日
This can be fixed by using coder.extrinsic. You can try this code:
function dataWrite = dataWrite(input)
modbustype = 'tcpip';
modbusserver = '192.168.0.80';
modbusport = 502 ;
coder.extrinsic('modbus');
coder.extrinsic('write');
m = modbus(modbustype, modbusserver,modbusport);
write(m,'coils', 1, input) ;
dataWrite = input;

カテゴリ

Help Center および File ExchangeModeling についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by