2021a之后版本matlab与python TCP通信详解

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 数据。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值