Loading

1

% clc
% clear
% tcpclient = tcpip('localhost', 8000, 'Timeout', 60,'OutputBufferSize',10240,'InputBufferSize',10240);%连接这个ip和这个端口的TCP服务器
% fopen(tcpclient);
% fwrite(tcpclient,'please sent');%发送一段数据给tcp服务器。服务器好知道matlab的ip和端口
% while(1) %轮询,直到有数据了再fread
% nBytes = get(tcpclient,'BytesAvailable');
% if nBytes>0
% break;
% end
% end
% receive = fread(tcpclient,nBytes);%读取tcp服务器传来的数据
% %关闭连接
% fclose(tcpclient);
% data=str2num(char(receive(2:end-1)')); %将ASCII码转换为str,再将str转换为数组
% disp(data)
% delete(tcpclient);

% Open Socket
ip = '0.0.0.0';
port = 2002;
fprintf('Socket openning @%s:%d ... ',ip,port);
t = tcpip(ip, port, 'NetworkRole','server');
fopen(t);
fprintf('Opened.\n');
tick = 0;

while true
% Read from socket (wait here)
while t.BytesAvailable == 0, WAIT=true; end
data = fread(t, t.BytesAvailable);
string = char(data)';
disp(string); % print-out
% Read mat file
%mat_name2load = 'script/file_from_python.mat';
%l = load(mat_name2load);
%disp(l);

% Save mat file (echoing the loaded mat file)
mat_name2save = 'script/file_from_matlab.mat';
x = l.x;
y = l.y;
save(mat_name2save,'x','y'); % save back
% Send to socket 
tx_data = sprintf('[%d] This is from MATLAB.',tick);
fwrite(t, tx_data);

% terminate 
tick = tick + 1;
if tick >= 10, break; end
pause(1e-0);

end

% Close
fclose(t);
fprintf('Closed.\n');

posted @ 2021-01-11 17:05  三只猫-  阅读(106)  评论(0编辑  收藏  举报