【MATLAB】UDP通信(干货代码分享,有注释)

写作时间:2020-07-24
目录:
1.问题
2.代码
3.总结

正文:

1.问题

使用MATLAB,实现UDP传输,接收由网络发送过来的图像数据。

2.代码
以图像传输为例。



% UDP for Image Receive 
%---------------------------------------------
% Revision History
% 2020-07-24
% written by alexMario
% 不要直接照抄,看懂后,才可以不变应万变!!!!
%---------------------------------------------
clc
clear 
%1. 手动设置(保持和发送端一致)-------------
%%图像数据信息与图像打包方式(以下的需要提前与发包端设置统一)
numRows=512;%图像的行
numCols=640;%图像的列
pixelDepth=2;%Bytes  单个像素数据存储深度,即就是像素深度为16bit
BytePerPck=4096 ;%Bytes   一个包里面有多少Bytes
%------------------------------------------------------
numPixelsPerIm=numRows*numCols;%一个图像里面有多少像素
pixelNumPerPck=BytePerPck/pixelDepth;%一个包里面有多少像素
TSFL_Bytes=8;%Type_Serial_Frame_Len 信息所占的 Bytes
BytesPerImage=numPixelsPerIm*pixelDepth;%一个图像里面有多少Bytes
PckNumPerImage=BytesPerImage/BytePerPck;%一个图像里面有多少个包
BytePerPck_act=BytePerPck+TSFL_Bytes;%Bytes  实际上一个包有多少Bytes
dataNumPerPck=BytePerPck_act/2; %实际上一个包有多少个uint16的数据
%-----------------------------------------------------------
%-----------------------------------------------------------
%%2. UDP communication
u=udp('192.168.8.168', 'RemotePort', 8000, 'LocalPort', 8004);
u.InputBufferSize=4096*2048;%Bytes
u.DatagramTerminateMode='on';
u.Timeout=10;
fopen(u);
u.Status
drop_flag=1;%丢包标志
pckID=0;%第几包
Datapck_num=0;
Im=zeros(numPixelsPerIm,1);
fig1=figure(1);
ax=axes(fig1);
set(fig1,'Name','UDP image');
%3. 收包和组包------------------------------------------------------
NumGetFrame=0;
while ishandle(fig1)
    [PCKdata,cc]=fread(u,dataNumPerPck,'uint16');
    if cc<4
        disp('包内的数据不够!小于4个,重新再组帧');
        continue
    end
    Type_pck=swapbytes(uint16(PCKdata(1)));%交换高低位
    Serial_pck=swapbytes(uint16(PCKdata(2)));
    if  Type_pck==uint16(1)
        pckID=0;
        drop_flag=0;
        Datapck_num=0;
    elseif Type_pck==uint16(2)
        vaildData_pck=uint16(PCKdata(5:end));
        if length(vaildData_pck)~=uint16(pixelNumPerPck)
            disp('数据包内的像素数据不够!重新再组帧');
            continue
        end
        if drop_flag==0 && Serial_pck==pckID+1     
            aa=(uint32(Serial_pck)-1)*uint32(pixelNumPerPck)+1;
            bb=uint32(Serial_pck)*uint32(pixelNumPerPck);
            Im(aa:bb)=vaildData_pck;
            Datapck_num=Datapck_num+1;
            pckID=Serial_pck;
            drop_flag=0;
        else
            drop_flag=1;%让下一包丢包的标志
        end
    else %Type_pck==3
        if  Datapck_num==PckNumPerImage
   
            Im=swapbytes(uint16(Im));
            Iin=reshape(Im,[numCols,numRows]);
            Iin=Iin';
            Iout=myDIP_fun( Iin );% do your image process(加自己的处理函数DIP)
            imshow(uint8(Iout),'Parent',ax);%show your processed image  resault
            NumGetFrame=NumGetFrame+1;
            disp(['散包组一帧显示',num2str(NumGetFrame)]);  
        end
        drop_flag=0;
    end
end
fclose(u);
u.Status
delete(u);
clear u;
toc;
%-----------------------------------------------------------
%-----------------------------------------------------------
close all;
disp('退出,结束');

不要直接照抄,看懂后,才可以不变应万变!!!!
因为UDP包格式不一样!!

3.总结
以图像传输为例,实现了通过UDP的数据传输,然后再组包成单帧图像。
包格式为自己定义,所以在使用的时候,根据自己的需求更改包的格式。

注:
关于UDP。Internet 协议集支持一个无连接的传输协议,该协议称为用户数据报协议(UDP,User Datagram Protocol)。UDP 为应用程序提供了一种无需建立连接就可以发送封装的 IP 数据包的方法。


THE END~

猜你喜欢

转载自blog.csdn.net/hahahahhahha/article/details/107569991