首页 > 编程知识 正文

modbustcpip通讯实例(tcpip通讯 matlab,TCPIP通信)

时间:2023-05-04 02:57:41 阅读:121545 作者:550

% A端:发送命令并接收b端反馈

% A端IP为192.168.123.30,b端IP为192.168.123.10

clear clc; 关闭全部;

%构建服务器端tcpip对象

tcpip服务器=tcpip '0.0.0',5001,'网络角色','服务器';

set (tcpip服务器,' Timeout ',10 );

N=1024;

set (tcpip服务器,' InputBufferSize ',8*N );

set (tcpip服务器,' OutputBufferSize ',1024;

打开%连接目标

fopen (tcpip服务器;

%发送命令

instruction=' pleasesendbackasignal.';

write (tcpip服务器,instruction,' int8';

instruction sendingsucceeds.' (disp );

numsent=get(tcpipserver,' valuesSent ';

disp (strcat (字节金融工具) : (,num2str ) numsent );

%等待接收数据

while(1)。

nbytes=get (tcpip服务器,' BytesAvailable ';

if nBytes 0

布雷克;

结束

结束

%接收数据

recvraw=fread(tcpipserver,nBytes/8,' double ' );

%绘制接收数据图像

图形;

打印(recv raw; 网格打开;

接收方签名框架(title );

%关闭和删除连接对象

fclose (tcpip服务器;

delete (tcpip服务器);

% B侧:接收指令,反馈1024点正弦波重叠噪声

clear clc; 关闭全部;

%生成反馈数据

N=1024;

t=[1:N]/N*4*pi;

signal=sin(t ) 0.05*rand(1) 1,n );

图形;

打印(t,信号);

网格打开;

标题(signalontheendofb.' ) )。

%构建客户端tcpip对象

tcpip客户端=tcpip (' 192.168.123.30 ',5001,

'网络角色','客户端'; %设置对象属性,a端的IP为192.168.123.30

set (tcpip客户端,' OutputBufferSize ',8*N ); 设置%缓存的长度

set (tcpip客户端,' InputBufferSize ',1024; 设置%缓存的长度

set (tcpip客户端,' Timeout ',60 ); %将连接时间设置为1分钟

打开%连接目标

fopen (tcpip客户端;

等待接收%命令

while(1)。

nbytes=get (tcpip客户端,' BytesAvailable ';

if nBytes0

布雷克;

结束

结束

%接收命令

接收指令=fread (tcpip客户端,nBytes,' int8' );

disp (接收指令(: ),通道)接收指令);

%反馈数据

write (tcpip客户端,信号,' double ' );

%关闭和删除连接对象

fclose (tcpip客户端;

删除(tcpip客户端;

这是你写的。 我试了一下,其中b端的IP换成我这里的服务端电脑的IP也无法连接。 也有错误。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。