版本更新:
Matlab代码经历过更新,其中Buffer这类属性都不再被支持使用。
作为取代的新代码参考:
Transition Your Code to tcpclient Interface - MATLAB & Simulink - MathWorks 中国
更多细节参考下面这个之前的笔记。
之前写的博客:
Matlab作为上位机TCP/IP发送视频给FPGA_matlab网络链接-CSDN博客
代码描述:
PC作为客户端,FPGA作为服务端
Matlab安装约定好的帧头对配置包和数据包进行打包,使用TCP/IP进行发送。
代码内容:
clc; clear all; close all; warning off; % ConfigPacket Frame Packet ConfigPacket_Length = 14;% 配置包单帧长 ConfigPacket_Content = zeros(1,ConfigPacket_Length,"uint8");% 配置包单帧内容初始化 image_w = 640; image_h = 512; Set_ConfigPacket_Content = [0,image_w,image_h,0];% 配置包内容数组 Set_ConfigPacket_Length = length(Set_ConfigPacket_Content);% 配置包总帧长 Readback_ConfigPacket = zeros(Set_ConfigPacket_Length, ConfigPacket_Length, "uint8"); % Data Frame Packet framehead = uint8([170,170,170,170,170,170,170,170]);% 配置包单帧内容初始化 Data_Line_Buf = zeros(1, image_w * 2,"uint8"); % Set Connection Client_1 = tcpclient("192.168.1.10",10000,"Timeout",20,"ConnectTimeout",30); for j = 1:Set_ConfigPacket_Length for i = 1:ConfigPacket_Length if i <= 8 ConfigPacket_Content (i) = 85;% Frame Header elseif i == 9 ConfigPacket_Content (i) = j - 1;% addr elseif i == 10 ConfigPacket_Content (i) = 0;% R/W elseif i == 11 ConfigPacket_Content (i) = uint8(bitand((bitshift(Set_ConfigPacket_Content(j),-24)),255)); elseif i == 12 ConfigPacket_Content (i) = uint8(bitand((bitshift(Set_ConfigPacket_Content(j),-16)),255)); elseif i == 13 ConfigPacket_Content (i) = uint8(bitand((bitshift(Set_ConfigPacket_Content(j),-8)),255)); elseif i == 14 ConfigPacket_Content (i) = uint8(bitand((bitshift(Set_ConfigPacket_Content(j),0)),255)); end end % s is a tcp/ip object write(Client_1,ConfigPacket_Content) pause(0.0001) Readback_ConfigPacket(j, 1:end) = read(Client_1, ConfigPacket_Length, "uint8"); end clear Client_1; pause(0.0001); write(Client_1,framehead); pause(0.0001); for n = 1:image_h for m = 1:image_w Data_Line_Buf(2 * m -1) = 0; if n < image_h/4 Data_Line_Buf(2 * m) = 64; elseif n < image_h/2 Data_Line_Buf(2 * m) = 128; elseif n < image_h/2 + image_h/4 Data_Line_Buf(2 * m) = 192; else Data_Line_Buf(2 * m) = 255; end end write(Client_1,Data_Line_Buf) pause(0.000001); end readreq = read(Client_1, 8, "uint8"); pause(0.0001); write(Client_1,framehead); pause(0.0001); for n = 1:image_h for m = 1:image_w Data_Line_Buf(2 * m -1) = 255; if n < image_h/4 Data_Line_Buf(2 * m) = 192; elseif n < image_h/2 Data_Line_Buf(2 * m) = 128; elseif n < image_h/2 + image_h/4 Data_Line_Buf(2 * m) = 64; else Data_Line_Buf(2 * m) = 0; end end write(Client_1,Data_Line_Buf) pause(0.000001); end readreq = read(Client_1, 8, "uint8"); pause(0.0001); write(Client_1,framehead); pause(0.0001); for n = 1:image_h for m = 1:image_w Data_Line_Buf(2 * m -1) = 0; if n < image_h/4 Data_Line_Buf(2 * m) = 64; elseif n < image_h/2 Data_Line_Buf(2 * m) = 128; elseif n < image_h/2 + image_h/4 Data_Line_Buf(2 * m) = 192; else Data_Line_Buf(2 * m) = 255; end end write(Client_1,Data_Line_Buf) pause(0.000001); end readreq = read(Client_1, 8, "uint8"); pause(0.0001); write(Client_1,framehead); pause(0.0001); for n = 1:image_h for m = 1:image_w Data_Line_Buf(2 * m -1) = 255; if n < image_h/4 Data_Line_Buf(2 * m) = 192; elseif n < image_h/2 Data_Line_Buf(2 * m) = 128; elseif n < image_h/2 + image_h/4 Data_Line_Buf(2 * m) = 64; else Data_Line_Buf(2 * m) = 0; end end write(Client_1,Data_Line_Buf) pause(0.000001); end readreq = read(Client_1, 8, "uint8"); clear Client_1;View Code
标签:Set,IP,image,ConfigPacket,TCP,Length,Matlab,170 From: https://www.cnblogs.com/VerweileDoch/p/18106804