【发布时间】:2018-11-12 08:09:42
【问题描述】:
在编写一个类的过程中将两个matlab实例连接在一起。这些实例将位于不同的计算机上,但我目前正在一台计算机上进行测试。
目前,我能够在两个 matlab 之间建立连接,并且能够在它们之间发送/接收消息。
代码:
classdef connectcompstogether<handle
properties
serverIP
clientIP
tcpipServer
tcpipClient
Port = 4000;
bsize = 8;
Message
end
methods
function gh = connectcompstogether(~)
% gh.serverIP = '127.0.0.1';
gh.serverIP = 'localhost';
gh.clientIP = '0.0.0.0';
end
function SetupServer(gh)
gh.tcpipServer = tcpip(gh.clientIP,gh.Port,'NetworkRole','Server');
set(gh.tcpipServer,'OutputBufferSize',gh.bsize);
fopen(gh.tcpipServer);
display('Established Connection')
end
function SetupClient(gh)
gh.tcpipClient = tcpip(gh.serverIP,gh.Port,'NetworkRole','Client');
set(gh.tcpipClient,'InputBufferSize',gh.bsize);
set(gh.tcpipClient,'Timeout',30);
fopen(gh.tcpipClient);
display('Established Connection')
end
function CloseClient(gh)
fclose(gh.tcpipClient);
end
end
methods
function sendmessage(gh,message)
fwrite(gh.tcpipServer,message,'double');
end
function recmessage(gh)
gh.Message = fread(gh.tcpipClient,gh.bsize);
end
end
end
matlab 1
gh = connectcompstogether;
gh.SetupServer();
gh.sendmessage(555);
matlab 2
gh = connectcompstogether;
gh.SetupClient();
gh.recmessage();
发送的消息是一个 8 位双精度 555。 然而,当查看收到的消息时,它原来是一个矩阵
64
129
88
不明白发生了什么,因为我一直关注的examples 没有这个问题。
并添加上下文。我正在尝试通过 TCP-IP 连接 2 个 matlab,这样我就可以控制一个实例与另一个实例。我的计划是让第二个 matlab 等待命令代码并在第一个 matlab 请求时执行指定的函数。
【问题讨论】: