2021a之后版本matlab与python TCP通信详解
问题初始:
我想要使用python调用 matlab来进行一些科学计算,但是调用过程中 matlab.eng 转换数据以及调用启动需要大量的时间,所以萌生出一个想法:使用本机的TCP
参考优化调用的链接:http://www.daimazhu.com/etagid33143b0/
matlab 服务端
matlab 中有的新的tcpserver对象来进行服务的构建,这里用得到是一个请求访问来的时候回调函数 @requestDATACommand,以及server收到一个代码终止符后自动处理二进制数据的 readArduinoData函数。 运行后就已经将服务启动了。函数之间和上级之间的传参可以使用global 变量的方式。
ip = '127.0.0.1';
port = 5005;
% 构造服务器端tcpip对象
server = tcpserver(ip, port,"Timeout",40);
server.ConnectionChangedFcn = @requestDataCommand;
configureCallback(server,"terminator", @readArduinoData);
configureTerminator(server,"LF");
function requestDataCommand(src,~)
if src.Connected
% Display the server object to see that Arduino client has connected to it.
disp("The Connected and ClientAddress properties of the tcpserver object show that the Arduino is connected.")
% disp(src.BytesAvailableFcnMode)
% Request the Arduino to send data.
disp("Send the command: ")
end
end
function readArduinoData(src,~)
% Read the sine wave data sent to the tcpserver object.
recive_tmie = tic;
src.UserData = readline(src);
input = jsondecode(src.UserData);
toc(recive_tmie)
end
python clinet 代码
xxxx 后续补充吧,python在服务器上(或者也能够在其他博客里面找到,就是访问连接服务器,然后sent数据,receive 数据。