How to continuously read data from UDP port

Discussion in 'MATLAB' started by Yohan, Jun 22, 2009.

  1. Yohan

    Yohan Guest

    Dear all,

    I am trying to read data from UDP port using the Instrument Toolbox. What I did is just to create a UDP connection, open the port and then read the data using fscanf.

    Is there a possibility to do this continuously? I mean to have the program continuously listening to the UDP port and when there is incoming data then the data will be read and processed.

    Ideas and suggestions are most welcome.

    Thanks,
    Yohan
     
    Yohan, Jun 22, 2009
    #1
    1. Advertisements

  2. Yohan

    Ankit Desai Guest

    Hi Yohan,

    You can do it using the callback functions.

    You might want to check BytesAvailableFcn documentation.

    You can set BytesAvailableFcnCount to number of bytes that triggers the BytesAvailableFcn callback. And in your callback you can read bytes using fscanf.

    Hope this helps

    -Ankit
     
    Ankit Desai, Jun 27, 2009
    #2
    1. Advertisements

  3. Yohan

    Yohan Guest

    Hi Ankit,

    Thank you very much for your reply.
    I am using the callback function as you suggested, however it didn?t seem to work. I sent UDP data from PC1 to PC2. Initially when I didn?t use the callback function, I have an m-file with fscanf (running on PC2) and run this first. It would run and then waited until it received something or the timeout occured. Then I went to PC1 and sent the UDP packet to PC2. The packet was received fine at PC2 and the program would terminate.

    If I use callback function, does the callback function run continuously in the background or I need to create a loop for the callback function?

    Many thanks,
    Yohan
     
    Yohan, Jul 7, 2009
    #3
  4. Yohan

    Ankit Desai Guest

    Hi Yohan,

    You might want to look at documentation for BytesAvailableFcnMode property.
    There are many settings that you will be required to set for it to work as you expect it to work.

    http://www.mathworks.com/access/helpdesk/help/toolbox/instrument/bytesavailablefcnmode.html

    The documentation for BytesAvailableFcn also has an example: http://www.mathworks.com/access/helpdesk/help/toolbox/instrument/bytesavailablefcn.html

    Also remember that just for UDP objects, there are callback properties - DatagramReceivedFcn and DatagramTerminateMode that you can work with.

    Hope this helps

    -Ankit
     
    Ankit Desai, Jul 7, 2009
    #4
  5. Yohan

    Yohan Guest

    Hi Ankit,

    Finally it works! I am now using the "DatagramReceivedFcn" and set the "DatagramTerminateMode" to 'on'. The callback function works flawlessly now.

    Thanks again for your help.

    Regards,
    Yohan
     
    Yohan, Jul 8, 2009
    #5
  6. Yohan

    Subha Guest

    Hi Yohan,
    I have doubt on DatagramReceivedFcn. I also want to monitor UDP port continuously. If i set DatagramTerminateMode to 'on', it receive one datagram and terminated. Then how you successful with continuous monitoring of port?
    Please help me to find out solution. I want to open port always and whenever data comes into port, it receives the data and store it in a file.
     
    Subha, Nov 23, 2011
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.