2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MATLAB での簡単 TCP/IP 通信 (Toolbox なし)

Posted at

経緯

MATLABで刺激制御とオンライン自動解析系を組みたかったので調べた.
記録PCが刺激PCに命令を出す形で計測がスタートする.時間精度がいらない制御なら十分.

クライアント側 (記録PC)
計測担当のPC.刺激PC出して欲しい刺激のIDを送る.
IPはサーバー側PCに設定する.

clear all
jTcpObj = jtcp('request','192.168.1.1', 3000, 'timeout', 2000);
mssg = [];
for i = 1:10
	disp('-------------------')
	disp(['Send: ' num2str(i)])
	jtcp('write', jTcpObj, num2str(i));
	
	pause(1)
	while isempty(mssg)
		mssg = jtcp('read',jTcpObj);
	end
	disp(mssg)
	mssg = [];
end
jtcp('write', jTcpObj, 'quit');
jtcp('close',jTcpObj);

サーバー側 (刺激PC)
クライアントから刺激IDを受け取って表示する.

clear jTcpObj;
clearvars
i = 1;
disp('Waiting Access from client...')
jTcpObj = jtcp('accept', 3000, 'timeout', 30000);
disp(['Connected: ' jTcpObj.remoteHost])
mssg = [];
while true
    disp('---------------');
    disp(['trial: ' num2str(i) ', waiting stimID ...'])
    while isempty(mssg)
        mssg = jtcp('read',jTcpObj);                
    end
    jTcpObj.outputStream.flush;
    ids{i} = mssg;                
    if strcmp(mssg, 'quit')
        disp('Received "quit" closing socket...')
        jtcp('close',jTcpObj);
        break;        
    end      
    jtcp('write',jTcpObj,['Stim Server Received: "' mssg '"']);
    
    % stimuli
    disp(['"' mssg '" Start']);    
    pause(2)
    disp(['"' mssg '" Finished']);    

    i = i+1; mssg = []; 
end
disp('')
disp('Session Finished')
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?