Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

UPD Problems


  • Please log in to reply
1 reply to this topic

#1 VanKurt

VanKurt

    Member

  • Members
  • PipPip
  • 19 posts

Posted 24 September 2012 - 08:35 PM

As my first Netduiono project I decided to exchange some data with my PC via ethernet (UDP).
I already got the Netdiuno sending some random data. On the PC I'm running wireshark to see the packets.

Here's my sending code on the Netduino:
            var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
            var remoteEndPoint = new IPEndPoint(IPAddress.Any, 50505);
            serverSocket.Bind(remoteEndPoint);

            var data = new byte[]{1,2,3};
            while (true)
            {
                serverSocket.Send(data);
                Thread.Sleep(1000);
            }

The only problem is: my desktop application does NOT receive those packets (while wireshark on the same machine does!).
Here's the receiving code on PC side:

        private void Form1_Load(object sender, EventArgs e)
        {
            var broadcastAddress = new IPEndPoint(IPAddress.Any, 50505);
            this.udp = new UdpClient();
            this.udp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            this.udp.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.
            this.udp.Client.Bind(broadcastAddress);
            this.udp.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
        }

        void ReceiveCallback(IAsyncResult result)
        {
            ...
        }

I checked the following things:
- Netduino is up, running and sending (packets arrive in wireshark on my PC)
- Netduino and PC are on the same subnet (255.255.255.0)
- Netduino and PC both use UDP on port 50505
- Desktop app is running and UDPClient has been successfully started

But my ReceiveCallback is never called...
Is there someone here who has successfully done this? Any advice? Thanks!

#2 caEstrada

caEstrada

    Advanced Member

  • Members
  • PipPipPip
  • 84 posts

Posted 28 September 2012 - 12:51 PM

VanKurt, I wrote a simple program in VBNet 2010 to debug UDP comms. I have attached the Zip for that Solution. Sorry, commented in spanish but if you run step by step, will understand. I tested it with WireShark. In my case, the PC is the master and sends orders to N+. First, checks if N+ is available on the same network segment. Send me an email to send you the zipped version I already tested. Now, for N+, I have equivalent code to process PC commands using UDP Protocol: int REMOTE_PORT = 8888; string REMOTE_IP = "192.168.1.99"; IPAddress remoteip = IPAddress.Parse(REMOTE_IP); IPEndPoint remoteendpoint = new IPEndPoint(System.Net.IPAddress.Any, REMOTE_PORT); //Configura un socket para recibir Datagramas via Internetwork usando el protocolo UDP Socket udp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //Vamos a procesar los broadcasts sin importar de cual estación vengan IPEndPoint broadcastEndPoint = new IPEndPoint(System.Net.IPAddress.Any, REMOTE_PORT); //Esta es la configuración para procesar comunicaciones UDP Broadcast, probado con el WireShark udp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); udp.SendTimeout = 100; if (udp.Available > 0) { //Separa en RAM tanto Bytes como sean necesarios byte[] byteReceiveBuffer = new byte[udp.Available + 20]; //Vamos a escuchar cualquier estación por el puerto definido //EndPoint remoteEndPoint = new IPEndPoint(System.Net.IPAddress.Any, REMOTE_PORT); EndPoint remoteEndPoint = new IPEndPoint(remoteip, REMOTE_PORT); //Cuantos bytes llegaron y copialos int iNumberOfBytes = udp.ReceiveFrom(byteReceiveBuffer, ref remoteEndPoint); //Convierte los bytes ahora a un string leible en humano char[] charReceiveBuffer = System.Text.UTF8Encoding.UTF8.GetChars(byteReceiveBuffer); //Envía una copia de los recibida udp.SendTo(byteReceiveBuffer, iNumberOfBytes, SocketFlags.Broadcast, remoteEndPoint); }




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.