How can I send the data from command window to Simulink using TCP/IP connection ?
29 views (last 30 days)
Show older comments
I try to set up TCP/IP connection for MATLAB : tcpserver and tcpclient function can work normally (IP address : 'localhost' ,Port : 4000)
Similarly, Simulink side, TCP/IP Client Send and TCP/IP Client Receive blocks can work normally (IP address : 'localhost' ,Port : 4000) and using echotcpip('on',4000) to create server.
I want to create the TCP/IP conncetion to send the data by using TCP/IP function (tcpserver ,tcpclient, etc.) to Simulink side by using TCP/IP Client Receive block.
Note that I want to set up command window is Tx and Simulink is Rx.
Thus, command window side use tcpserver("localhost",4000) to create TCP/IP connection. However, the TCP/IP Client Receive block at Simulink side doesn't receive the data and show ERROR : Cannot create a communication link with the remote server.
How can I set up to send the data from command window to Simulink using TCP/IP connection ?
Thanks for your answer.
0 Comments
Accepted Answer
Shubham
on 29 Oct 2023
I understand that you wish to send data from the MATLAB command window to a Simulink model.
For this you can create a TCP connection from MATLAB and set it as a server. Then you need to enter the same address and port number in the “TCP/IP Receive” Simulink block .
You can refer to the following example for setting up model parameters and establishing a connection in Simulink:
For transferring data from the MATLAB Command Window to Simulink you can try out the following example:
I have tried the following at my end:
MATLAB script:
server = tcpserver('0.0.0.0',4000,ByteOrder="big-endian",ConnectionChangedFcn=@connectionFcn)
sim("simul.slx");
function connectionFcn(src, ~)
if src.Connected
freq = 1; % frequency (Hz)
time = (0:1/(freq*100):1);
amp = 1; % amplitude (V)
phi = 0; % phase
data = amp*sin(2*pi*freq*time+phi);
write(src,data,"double");
end
end
Simulink Model:
Output Produced:
You can also refer to:
Hope this helps!!
More Answers (0)
See Also
Categories
Find more on Development Computer Setup 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!