TCP/IP server to connect to S7 1200
이전 댓글 표시
I am trying to control my Simulink model through an S7 1200 PLC. The idea is to send the process value from the model to the PLC and send a corresposding control variable from PLC to the simulink model.
The TIA Portal only has TCP/IP client block, and the instrument Control Toolbox of Matlab also has TCP client send/receive blocks. So I decided to write my own function to make a TCPIP server on Matlab. The code is quite simple but the problem is that the connection gets destroyed once the fucntion block is executed. This makes my simulation run very slow (1 second is simulated in 8 seconds).
t = tcpip('0.0.0.0', 2000, 'NetworkRole', 'server');
fopen(t);
Is there a way to make the connection to the PLC once and then reuse that connection throughout the simulation?
I have spent a lot of time trying to find a solution but no success yet. The Modbus object won't even run inside the custom function. UDP also needs to connect every time so I am out of options here. Is there anyone that can help me please.
채택된 답변
추가 답변 (1개)
omkar tulankar
2020년 11월 17일
0 개 추천
Hi arsalan ,
I am also trying to connect MATLAB simulink model with the help of OPC connection.
I have developed a model for motor speed control using Variable frequency drive.
For developing this model , I am using simscape specialized power system blocks.
But when I establish the communication and feed the values , the output that is delivered , is varying and inconsistent.
For example , if I feed 100 rpm as input to VFD , ideally the output speed of the motor must be 100 rpm, but this is not happening and the output is constantly varying and never stablizes to fed input value.
Do you have any particular solution for this ?
I am using following code :
function [x] = Read_OPC_Func(y)
% variables
persistent init_Server;
persistent init_Nodes;
persistent uaClient;
persistent Var_Node_In;
persistent Var_Node_Out;
persistent testVal;
% initialize variables
if (isempty(init_Server))
testVal = 0;
init_Server = 0;
init_Nodes = 0;
end
% OPC UA server (PLC) address and connecting client (Simulink) to the
% server
if init_Server == 0
init_Server = 1;
uaClient = opcua('172.16.4.20',4840);
connect(uaClient);
end
% define variable nodes in the server
if uaClient.isConnected == 1 && init_Nodes == 0
init_Nodes = 1;
% find DB "OpcInterface" of the server
%DB_Node = findNodeByName(uaClient.Namespace, 'OpcInterface', '-once');
% find variables "ref_speed" and "Motor_speed" in the DB "OpcInterface
Var_Node_In = opcuanode(3, '"OpcInterface"."ref_speed"',uaClient);
Var_Node_Out = opcuanode(3, '"OpcInterface"."Motor_speed"',uaClient);
end
% read and write variables of the server
if uaClient.isConnected == 1 && init_Nodes == 1
% read "ref_speed" value from server and store in "val"
[val, ~, ~] = readValue(uaClient, Var_Node_In);
% assign input y of the function to "Motor_speed" variable
%writeValue(uaClient, Var_Node_Out, y);
% assign "val" to variable "testVal"
testVal = val;
end
% assign "Motor_Speed" ("testVal") value to the output x of the function
x = double(testVal);
end
Ver:
MATLAB 2020a
TIA portal V15
PLC SIM Advanced Ver 3.0
I am also attaching the MATLAB model for your reference.
Thanks
카테고리
도움말 센터 및 File Exchange에서 Simulink PLC Coder에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!