How can I send the data from command window to Simulink using TCP/IP connection ?
조회 수: 26 (최근 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!