tcpip connection with pnet

1 min read

We use TCP/UDP/IP Toolbox 2.0.5 to read and write data from/to a TCPIP port. It’s fast and reliable. The version we use is 2.0.5. Below is a matlab sample script showing how to connect to another computer (called ETG-4000) with TCPIP :

%Connect LAN Port
fid=pnet('tcpconnect','172.17.101.1',51027);

if pnet(fid,'status')==-1
  disp('Connection Failed.');
  return;
end;

%Write to port (i.e. Send Command for ETG-4000)
pnet(fid,'printf','++Hello ETG-4000\r\n');

%Read from port (i.e. Receive Command from ETG-4000)
welcomeMessage = pnet(fid,'read',74); 

disp('Press ETG-4000 START Button!');

%//////////////////////// Get ETG-4000 Data ////////////////////////////////
while(1);
            buff1=pnet(fid,'read',4, 'uint8');
            hsize=bread(buff1,'int32');

            if hsize==12;%Data is comming!
                %disp('hsize is 12');
                %///////////////////////// Data Number ///////////////////////////////
                buff2=pnet(fid,'read',4, 'uint8');
                num=bread(buff2,'int32');%Number of Data
                %disp(['num: ' num]);

                %disp(['num of buff2 ' num2str(num)]);
                %//////////////////////////// Data Size:428 ////////////////////////////
                buff3=pnet(fid,'read',4, 'uint8');
                dsize=bread(buff3,'int32');
                %disp(['dsize ' dsize]);
                %/////////////////////////// Hb Data ////////////////////////////////
                for ch=1:52;%Oxy
                    buff4=pnet(fid,'read',4, 'uint8');
                    oxy(ch,num)=bread(buff4,'single');
                %    disp(oxy(ch,num));
                end;
                for ch=1:52;%Deoxy
                    buff5=pnet(fid,'read',4, 'uint8');
                    deo(ch,num)=bread(buff5,'single');
                end;

                %//////////////////////////// Mark ///////////////////////////////
                buff6=pnet(fid,'read',2, 'uint8');
                mark(num)=bread(buff6,'int16');

                %/////////////////////////// Time ////////////////////////////////
                buff7=pnet(fid,'read',10);
                time(num,:)=char(buff7);
                %disp(time(num,:))

          end;
          %(check status) Push ETG-4000 Stop Button
          stat=pnet(fid,'status');
          if(stat == 0)
            break;
          end
end;

pnet('closeall');
clear fid;

bread is a function to convert binary data to a certain data type:

function y = bread(x, type)
y = typecast(x, type); % work for pnet

One important thing is you need specify the 4th parameter (datatype) when you read data in pnet(‘read’). The default value is ‘char’ and if your incoming data is float, you will find any value in a byte which is bigger than 127 is reset to 255. Specifying the 4th parameter to ‘uint8’ solves this problem. I spend a day to find this.



写作助手,把中式英语变成专业英文


Want to receive new post notification? 有新文章通知我

第五十八期fNIRS Journal Club通知2024/12/07, 10am 王硕教授团队

理解噪音中的言语对老年听力损失患者来说是一个重大挑战。来自首都医科大学附属北京同仁医院耳鼻咽喉科研究所王硕教授团队的助理研究员王松建将为大家介绍他们采用同步EEG-fNIRS技术,从神经与血流动力学两
Wanling Zhu
10 sec read

第五十七期fNIRS Journal Club视频 王欣悦博士

Youtube: https://youtu.be/vyo-kECC2Ps 优酷:https://v.youku.com/v_show/id_XNjQzNTA0ODIwMA==.html 肢体语言——
Wanling Zhu
20 sec read

第五十七期fNIRS Journal Club通知2024/11/02, 10am 王欣悦博士

肢体语言——例如人际距离、眼神、手势等,如何影响我们的交流,是一个有趣的谜题。它们是优雅而神秘的代码,无本可依、无人知晓,却又无人不懂。来自南京师范大学的王欣悦博士将分享如何通过fNIRS超扫描技术,
Wanling Zhu
16 sec read

4 Replies to “tcpip connection with pnet”

  1. Hi Xu!

    Thanks for the nice script! Works very well 🙂

    Do you know if it is possible to start and stop the ETG-4000 by using TCP/IP? I want my stimulus program to trigger the start, instead of having someone pressing the ETG-4000 START button…

  2. Dear Xu Cui,
    Tnank you for your script.But I have some trouble here.
    Can it work on the win32 system? Does it need to mix matlab and C plus programming, since my error is : Invalid MEX-file ‘F:\program_BCI\2011-3\tcp_udp_ip\pnet.mexw32’.
    My matlab version is 7.1. win32 system
    Do you have any suggestion?
    Thank you very much!!

  3. I’m not sure. You don’t need C programming though. MEX file is always a nightmare – it is so system dependent. You may want to contact the original author of pnet.

Leave a Reply

Your email address will not be published. Required fields are marked *