TCP/IP server to connect to S7 1200

33 views (last 30 days)
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('', 2000, 'NetworkRole', 'server');
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.

Accepted Answer

Arsalan Shah
Arsalan Shah on 30 Aug 2019
I managed to indirectly find a very simple solution from some OPC tutorial. I used an Interpreted Matlab Fcn block and inside the block I wrote a function which creates a connection and sends data to PLC over TCP/IP. In order to reuse the old connection instead of making a new one every time I defined my TCPIP object as a consistent variable (details here). This way the variable is stored in Matlab memory when the block is not executing.
function [x] = dataWriteandReadTCPIP(b)
persistent init_Server;
persistent tcpClient;
persistent testVal;
persistent bytesAvailable;
if (isempty(init_Server))
testVal = 0;
%bytesAvailable = 0;
init_Server = 0;
if init_Server == 0
init_Server = 1;
tcpClient = tcpip('', 2000, 'NetworkRole', 'server');
A = single(b);
B = typecast(A,'uint8');
C = fliplr(B);
fwrite(tcpClient, C);
newBytesAvailable = get(tcpClient,'BytesAvailable');
if newBytesAvailable > 0
data_Temp = fread(tcpClient, 4);
data_Temp = flipud(data_Temp);
data_Temp = uint8(data_Temp);
testVal = typecast(data_Temp,'single');
bytesAvailable = newBytesAvailable;

More Answers (1)

omkar tulankar
omkar tulankar on 17 Nov 2020
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;
% OPC UA server (PLC) address and connecting client (Simulink) to the
% server
if init_Server == 0
init_Server = 1;
uaClient = opcua('',4840);
% 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);
% 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;
% assign "Motor_Speed" ("testVal") value to the output x of the function
x = double(testVal);
MATLAB 2020a
TIA portal V15
PLC SIM Advanced Ver 3.0
I am also attaching the MATLAB model for your reference.


Community Treasure Hunt

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

Start Hunting!

Translated by