
I want to connect tcp/ip client to server so you can see a screenshot with microgrid as a client and i also share a matlab code as a server. there have a some problem
5 次查看(过去 30 天)
显示 更早的评论
this is server code and you can see the problem also- server = tcpserver("127.0.0.1", 3000);
configureTerminator(server, "LF"); % Optional: Ensure terminator matches
disp("✅ Server started. Waiting for Simulink client...");
% Wait for client connection
while ~server.Connected
pause(0.1);
end
disp("✅ Client connected!");
while true
if server.NumBytesAvailable >= 8 % Wait for at least 8 bytes (1 double)
voltage = read(server, 8, "double");
v = voltage(end);
fprintf("📥 Received voltage: %.2f V\n", v);
% Control logic
controlSignal = double(v <= 50); % 1 if <=50, else 0
% Send response
if server.Connected
write(server, controlSignal, "double");
fprintf("📤 Sent control signal: %d\n", controlSignal);
else
fprintf("⚠️ Client disconnected.\n");
break;
end
end
pause(0.01);
end
✅ Server started. Waiting for Simulink client...
✅ Client connected!
Warning: The specified amount of data was not returned within the Timeout period for 'read'.
'tcpserver' unable to read all requested data. For more information on possible reasons, see tcpserver Read
Warnings.
📥 Received voltage: 0.00 V
⚠️ Client disconnected. server = tcpserver("127.0.0.1", 3000);
configureTerminator(server, "LF"); % Optional: Ensure terminator matches
disp("✅ Server started. Waiting for Simulink client...");
% Wait for client connection
while ~server.Connected
pause(0.1);
end
disp("✅ Client connected!");
while true
if server.NumBytesAvailable >= 8 % Wait for at least 8 bytes (1 double)
voltage = read(server, 8, "double");
v = voltage(end);
fprintf("📥 Received voltage: %.2f V\n", v);
% Control logic
controlSignal = double(v <= 50); % 1 if <=50, else 0
% Send response
if server.Connected
write(server, controlSignal, "double");
fprintf("📤 Sent control signal: %d\n", controlSignal);
else
fprintf("⚠️ Client disconnected.\n");
break;
end
end
pause(0.01);
end
✅ Server started. Waiting for Simulink client...
✅ Client connected!
Warning: The specified amount of data was not returned within the Timeout period for 'read'.
'tcpserver' unable to read all requested data. For more information on possible reasons, see tcpserver Read
Warnings.
📥 Received voltage: 0.00 V
⚠️ Client disconnected. server = tcpserver("127.0.0.1", 3000);
configureTerminator(server, "LF"); % Optional: Ensure terminator matches
disp("✅ Server started. Waiting for Simulink client...");
% Wait for client connection
while ~server.Connected
pause(0.1);
end
disp("✅ Client connected!");
while true
if server.NumBytesAvailable >= 8 % Wait for at least 8 bytes (1 double)
voltage = read(server, 8, "double");
v = voltage(end);
fprintf("📥 Received voltage: %.2f V\n", v);
% Control logic
controlSignal = double(v <= 50); % 1 if <=50, else 0
% Send response
if server.Connected
write(server, controlSignal, "double");
fprintf("📤 Sent control signal: %d\n", controlSignal);
else
fprintf("⚠️ Client disconnected.\n");
break;
end
end
pause(0.01);
end
✅ Server started. Waiting for Simulink client...
✅ Client connected!
Warning: The specified amount of data was not returned within the Timeout period for 'read'.
'tcpserver' unable to read all requested data. For more information on possible reasons, see tcpserver Read
Warnings.
📥 Received voltage: 0.00 V
⚠️ Client disconnected.

0 个评论
回答(1 个)
Harsh
2025-5-8
I have encountered a similar issue before. I recommend using the `echotcpip` function instead. You can implement the logic `controlSignal = double(v <= 50); % 1 if <= 50, else 0` in a MATLAB Function Block, which processes the data received after the "TCP Receive" block.
Here's how the setup should look:
After running the following command the model can be executed.
echotcpip('on', 3000) % Starts an echo tcp/ip server at port 3000

Please feel free to refer to the following documentation example, which demonstrates a similar scenario:
https://www.mathworks.com/help/instrument/send-and-receive-data-over-a-tcpip-network.html
I hope this helps. Thanks!
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!