Matlab作为上位机TCP/IP发送视频给FPGA

演示:

PC通过TCP传视频到FPGA-CSDN直播

代码功能描述:

1.自定义帧头写入FPGA PS端,PS端解析帧头辨别是配置还是数据;

2.如果是配置包则将帧中数据写入配置的地址,PL端读取这部分地址传递给HDMI模块;

3.接收数据包写入指定区域,缓存多帧并读取写好的区域;

连接代码:

上半部分是用以前版本的函数写的,下半部分是新的;

简洁了很多。

clc;
clear all; 
close all; 
warning off;

% Config_list = ones(10,10,"uint8");
Config_list = zeros(10,10,"uint8");

% % set olden version
% Client_0 = tcpip("192.168.1.119",10000);
% 
% set(Client_0,'InputBufferSize',256*64);
% set(Client_0,'OutputBufferSize',256*64);
% 
% fopen(Client_0);
% % s is a serial object
% fwrite(Client_0,Config_list)

% set new version
Client_1 = tcpclient("192.168.1.119",10000,"Timeout",20,"ConnectTimeout",30);
% s is a serialport object
write(Client_1,Config_list)

发送代码2:

帧头部分可以自己定义,根据写的程序来:

clc;
clear all; 
close all; 
warning off;

% ConfigPacket Frame Packet
ConfigPacket_Length = ();% 配置包单帧长
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([]);% 配置包单帧内容初始化
Data_Line_Buf = zeros(1, image_w * 2,"uint8");


% Set Connection 
Client_1 = tcpclient("192.168.1.10",10000,"Timeout",20,"ConnectTimeout",30);

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;

Python实现的参考:

Xilinx ZYNQ+TCP通信+Python上位机 实现实时视频传输系统 - 知乎 (zhihu.com)

GitHub - yg99992/Image_transfer_open_source: ZYNQ-7000 based data transfer through TCP/IP protocol

参考:


创建 TCP/IP 客户端并配置设置 - MATLAB & Simulink - MathWorks 中国
MATLAB实现tcp连接 - 知乎 (zhihu.com)

MATLAB :【11】一文带你读懂serialport串口收发原理与实现_matlab serialport-CSDN博客

 Transition Your Code to serialport Interface - MATLAB & Simulink - MathWorks 中国

Transition Your Code to tcpclient Interface - MATLAB & Simulink - MathWorks 中国

串行通信的动态输入缓冲区大小 - MATLAB Answers - MATLAB Central (mathworks.cn)

如何更改串口的收发缓存的大小 - MATLAB Answers - MATLAB Central

Which is the Buffer Size of a TCP/IP socket (tcpclient)? - MATLAB Answers - MATLAB Central (mathworks.cn)

Matlab代码的版本更新:

因为TCP/IP函数即将被移除,之后请使用新的函数作为代替:

tcpip will be removed. Use tcpclient or tcpserver instead. For more information on updating your code, see Compatibility Considerations.

(To be removed) Create TCPIP object - MATLAB tcpip - MathWorks 中国

串行属性用于使用 serial 对象配置通信和配置读写行为,可以用来配置缓存区,

这个也将被删除,serial 配置缓冲区已经成为了历史。

        Removed Functionality The ValuesReceived and ValuesSent properties will be removed. You can calculate the number of values sent using the NumBytesAvailable property and the data type of the data available.

For example, if the NumBytesAvailable is 20 bytes of uint32 data, the number of values sent is five since each uint32 value is four bytes.
        The readasync and stopasync functions and the ReadAsyncMode and TransferStatus properties will be removed. The updated interface reads data asynchronously.
The BytesToOutput, InputBufferSize, and OutputBufferSize properties will be removed. Buffer sizes are automatically managed and sized as needed.
        The BreakInterruptFcn, OutputEmptyFcn, and PinStatusFcn properties will be removed. You can set callback functions using configureCallback in the updated interface, but not for these properties.
        The RecordDetail, RecordMode, RecordName, and RecordStatus properties will be removed.
        The TimerFcn and TimerPeriod properties will be removed. Use timer instead.
        The Name, Type, ObjectVisibility, Status, and Tag properties will be removed.

  • 26
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值