locked
Sending data from Matlab to an external program using UDP

    General discussion

  • Hi all,

    I'm totally new to UDP protocols. I'm trying to send data from Matlab to an external program using UDP, but of course, I'm not able to make it work.

    My code is as follows:

    In Matlab (Emitter):

    port=8051;
    host='127.0.0.1';
    u = udp(host,port, 'LocalPort',8052);
    fopen(u)
    x=2;
    fwrite(u,x,'uint')
    fclose(u)

    In the Externel Program (Receiver)

    private  void ReceiveData()
            {
         
                client = new UdpClient(8051);
                while (true)
                {
                    try
                    {
                      IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);

                      byte[] data = client.Receive(ref anyIP);    //Receive data from client
         
                        // Codify data with format UTF8 to TextFormat
                        string text = Encoding.UTF8.GetString(data);
        
                    }
                    catch (Exception err)
                    {
                        print(err.ToString());
                    }
                }
            }

    Any idea of how I can implement it, or why it is not working will be really appreaciated. Thanks a lot in advance!

    Monday, October 22, 2012 7:28 PM

All replies

  • Hi,

    Please consider to use these forums:http://social.msdn.microsoft.com/Forums/en-US/ncl/threads


    Cheers, Amy

    Wednesday, October 24, 2012 7:28 AM
  • Your program isn't doing anything with the code so it may appear like it is doing nothing. You also have to change your EndPoint port number from 0 to 8051.  You should startup the Visual Studio application before running the Matlb code.  The program will terminate if it is actually running (except you are in an endless loop).  Let me example the code completely so you will understand how to debug the code.

    A connection consists of 3 items

    1) The Source IP address

    2) The Destination IP address

    3) The Port number which is the same on both end of the connection

    Here is the matlab program with explainations

    The code is opening up tow connections.  One for transmitting and the other for receiving.

    port=8051;         -------->  The Transmit Port Number
    host='127.0.0.1';    -----------> The destination IP address - 127.0.0.1 is the loopback address

                                   ------------> Every ethernet device has a loopback address

                                   -------------> So in this case you are sending data to a proces on the same

                                   -------------> computer where it was generated

    u = udp(host,port, 'LocalPort',8052);   ----> configure the connection.  LocalPort and 8052 is a

                                                                ----> is a second port for receivng data 

    fopen(u)                 ----------> open tow connections
    x=2;
    fwrite(u,x,'uint')      ---------> write data
    fclose(u)                 ----------> close connection

    ---------------------------------------------------------------------------------------------------------------

    private  void ReceiveData()
            {
         
                client = new UdpClient(8051);   //open a UDP port for listeneing
                //while (true)      ----> comment out statement so you are not in an endless loop
                //{                      ---->  comment out statement so you are not in an endless loop
                    try
                    {
                      IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 8051);   

                                                                                                 //IPAddress.Any is 127.0.0.1

                                                                                                 // change port number to 8051


                      byte[] data = client.Receive(ref anyIP);    //Receive data from client

                                                                                       //Receive will wait until connection is closed     
                        // Codify data with format UTF8 to TextFormat
                        string text = Encoding.UTF8.GetString(data);
                        print(test);           //add this line so you can see the results  
                    }
                    catch (Exception err)
                    {
                        print(err.ToString());
                    }
                //}          ---------------> commend out code so you are not in an endless loop
            }


    jdweng

    Wednesday, October 24, 2012 11:06 AM
  • Dear Joel,

    Thanks a lot for your clarifying explanation. Unfortunatelly I could fix the problem before I read it, but still you gave me some usefull clues for the future. So THANK YOU VERY MUUUCH!!!

    Regarding your considerations, and just in case somebody may find it interesting:

    1. I was sending the message '2' from Matlab to the external program, but the external program was not outputing it by the print command. As you said, the program was not doing anything, but it was suppossed to 'print' the message it was receiving. That was the main problem. However, after some tests I realized, that the program was actually reading the message but it was not 'interpreting' it, due to a format incompatibility (?). This is just my guess.

    2. The solution was as easy as using the Matlab command fprintf instead of fwrite to write on the server. That way, the external program is able to read what I am sending from Matlab.

    3. From what I understood, using port 0 instead of 8051, I'm reading any port from the specified IP. In fact, the code works with port 0. But, just for avoiding future problems, and once I have cheked that the communication is working, I will specifically indicate to use port 8051.

    Thanks again for your help!

    Wednesday, October 24, 2012 2:03 PM