|
準(zhǔn)備:操作系統(tǒng)xp兩臺pc, matlab為2011a版本(或以上)
網(wǎng)線直連,設(shè)置兩臺PC為同一局域網(wǎng)內(nèi)(如192.168.1.101與192.168.1.102);
PC1:發(fā)端
>> data = membrane(1); %生成數(shù)據(jù)
>> s = whos('data'); %提取數(shù)據(jù)參數(shù)
>> tcpipServer = tcpip('0.0.0.0',55000,'NetworkRole','Server'); %設(shè)置對象屬性
>> set(tcpipServer,'OutputBufferSize',s.bytes); %設(shè)置緩存長度
>> fopen(tcpipServer); %打開對象
>> fwrite(tcpipServer,data(:),'double'); %寫入數(shù)據(jù)
PC2:收端
>> tcpipClient=tcpip('192.168.1.103',55000,'NetworkRole','Client')%設(shè)置對象屬性
>> set(tcpipClient,'InputBufferSize',7688); %設(shè)置緩存長度
>> set(tcpipClient,'Timeout',30); %設(shè)置連接時間
>> fopen(tcpipClient); %打開連接對象
>> rawData=fread(tcpipClient,961,'double'); %接收數(shù)據(jù),為行向量
>> data1=reshape(rawData,31,31); %數(shù)據(jù)整理
>> surf(data1) %繪圖
注意:在PC1上打開對象時,若PC2連接對象尚未打開,則PC1 workspace中一直顯示busy,當(dāng)PC2連接對象打開后,PC1中對象打開成功后,才可以寫入數(shù)據(jù)。
|
|