Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
UDP Reading problem

Subject: UDP Reading problem

From: Stuart Layton

Date: 28 Jun, 2011 13:43:04

Message: 1 of 2

My Code --> http://ideone.com/zo5fE

I have data coming in from another computer, in the form of UDP packets, that I'd like to plot in real time. To do this I have setup a UDP object and set a callback to be fired everytime a packet is received. This works pretty well for a few moments.

When I run the function the "Packet Read" message is displayed several times but eventually the message stop appearing. Using Wireshark I can verify that packets are in fact arriving on my machine. Also if I execute. fclose(u); fopen(u); the message appears again for a while but eventually stops again.

Am I doing something obviously wrong here?

Subject: UDP Reading problem

From: Trent Jarvi

Date: 28 Jun, 2011 14:06:12

Message: 2 of 2


"Stuart Layton" <neurostu@yahoo.com> wrote in message
news:iuclp8$bq8$1@newscl01ah.mathworks.com...
> My Code --> http://ideone.com/zo5fE
>
> I have data coming in from another computer, in the form of UDP packets,
> that I'd like to plot in real time. To do this I have setup a UDP object
> and set a callback to be fired everytime a packet is received. This works
> pretty well for a few moments.
> When I run the function the "Packet Read" message is displayed several
> times but eventually the message stop appearing. Using Wireshark I can
> verify that packets are in fact arriving on my machine. Also if I
> execute. fclose(u); fopen(u); the message appears again for a while but
> eventually stops again.
>
> Am I doing something obviously wrong here?

Hi Stuart,

The FREAD call will attempt to read an entire input buffer by default. To
make it more responsive, you might try something like the following:
function u = strippedDownReceiver(varargin)

args.host = '10.121.43.56';
args.rxPort = 5000;
args.txPort = 10000; % shouldn't matter as we aren't txing
args.udpObjBufferSize = 1024*8;

% -------------------------------------------
% Networking variables
% -------------------------------------------

u = [];
nPacket = 0;

% -------------------------------------------
% Start Everything up
% -------------------------------------------

initNetwork();

% -------------------------------------------
% Network and Buffer Related Function
% -------------------------------------------
    function initNetwork()
        u = udp(args.host, args.txPort, 'LocalPort', args.rxPort);
        set(u,'BytesAvailableFcn', @udpPacketRxCallback);
        fopen(u);
    end

    function udpPacketRxCallback(obj, event) if( obj.BytesAvailble >
0) data = fread(obj, obj.BytesAvailable); disp('Packet
read'); else disp('Packet already read?');
end endend
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us