准备:操作系统xp两台pc, matlab为2011a版本(或以上)
网线直连,设置两台PC为同一局域网内(如192.168.1.101与192.168.1.102);
PC1:发端
>> data = membrane(1); %生成数据
>> s = whos('data'); %提取数据参数
>> tcpipServer = tcpip('0.0.0.0',55000,'NetworkRole','Server'); %设置对象属性
>> set(tcpipServer,'OutputBufferSize',s.bytes); %设置缓存长度
>> fopen(tcpipServer); %打开对象
>> fwrite(tcpipServer,data(:),'double'); %写入数据
PC2:收端
>> tcpipClient=tcpip('192.168.1.103',55000,'NetworkRole','Client')%设置对象属性
>> set(tcpipClient,'InputBufferSize',7688); %设置缓存长度
>> set(tcpipClient,'Timeout',30); %设置连接时间
>> fopen(tcpipClient); %打开连接对象
>> rawData=fread(tcpipClient,961,'double'); %接收数据,为行向量
>> data1=reshape(rawData,31,31); %数据整理
>> surf(data1) %绘图
注意:在PC1上打开对象时,若PC2连接对象尚未打开,则PC1 workspace中一直显示busy,当PC2连接对象打开后,PC1中对象打开成功后,才可以写入数据。
|