How can I send the data from command window to Simulink using TCP/IP connection ?
33 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Shubham
2023-10-29
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!!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Development Computer Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!