UDP 接收和发送 Matlab [英] UDP Receive and Send Matlab

查看:36
本文介绍了UDP 接收和发送 Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从外部设备接收数据包,然后将数据发送到另一台设备.我有一个有效的 Simulink 模型,但是我不知道如何在 Matlab 中对其进行编码.

I am currently working on receiving packets of data from an external device and then ill be sending data to another device. I having a working Simulink model however i don't know how to code it in Matlab.

Matlab 中 UDP 接收块的参数如下图所示UDP 接收参数

The parameters for the UDP receive block in Matlab are shown in this image UDP Receive Parameters

到目前为止,我制作相同链接的代码是

The code I have so far for making the same link is

echoudp('on', 25000)
u = udp('0.0.0.0', 25000)
fopen(u)
while True
A = fread(u, 8156, 'unit16')
end

我希望能够连续读取数据并打印出来.然而,fread 没有给我任何数据,但 simulink 模型工作正常.知道我哪里出错了吗?

I want to be able to read the data continuously and print it out. However the fread is giving me no data what so ever but the simulink model works fine. Any idea where i am going wrong?

推荐答案

这应该可行:

clc();

echoudp('on',25000);

u = udp('127.0.0.1',25000);
u.InputBufferSize = 10000;
u.OutputBufferSize = 10000;
u.ReadAsyncMode = 'continuous';
u.BytesAvailableFcn = @BytesAvailable_Callback;
u.BytesAvailableFcnMode = 'terminator';

fopen(u);

if (~strcmp(u.Status,'open'))
    NetworkError(u,'Connection failed!');
end

try
    for i = 1:10
        fprintf(u,'Hey!');
    end
catch e
    NetworkError(u,['Communication failed! (' e.message ')']);
end

pause(1);

NetworkDispose(u);

function BytesAvailable_Callback(u,evt) %#ok<INUSD>
    data = fscanf(u);
    disp('Data Received!');
    disp(data);
end

function NetworkDispose(u)  
    fclose(u);
    delete(u);
    clear u;

    echoudp('off');
end

function NetworkError(u,msg)
    NetworkDispose(u);
    error(msg);
end

这会设置一个通过 UDP 进行连续(和异步)侦听.有关详细信息,请阅读 this.

This sets up a continuous (and asynchronous) listening over UDP. For more information read this.

这篇关于UDP 接收和发送 Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆