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

Send message Netduino To Pc Via Ethernet/IP


  • Please log in to reply
1 reply to this topic

#1 davasquez

davasquez

    Member

  • Members
  • PipPip
  • 16 posts

Posted 17 October 2012 - 09:10 PM

Hi all, im trying to send a message ND to PC(I want to see the message in PC), to use UDP socket and the port Ethernet/IP AF12. This is my code. //netduino OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); Socket socket= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("130.151.132.55"), 44818); try { socket.Connect(localEndPoint); } catch (SocketException ex) { Debug.Print(ex.Message); } string texto="HI im Netduino"; byte[] bytesToSend=new byte[48]; bytesToSend = Encoding.UTF8.GetBytes(texto); socket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None,localEndPoint); //PC ocket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//con esta linea se crea el socket IPEndPoint sender = new IPEndPoint(IPAddress.Parse("130.151.132.55"), 44818);//define la IP de quien envia, tener en cuenta el mismo puerto server.Connect(sender); byte[] recieved = new byte[48]; EndPoint remoteEP = (sender); int BytesRecevied = server.ReceiveFrom(recieved, ref remoteEP); string dataRecieved = Encoding.ASCII.GetString(recieved); Console.WriteLine(dataRecieved.ToString()); Why show this error?: 'System.Net.Sockets.SocketException' en Microsoft.SPOT.Net.dll What is the error in the code?. Thanks

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 18 October 2012 - 05:16 AM

The following code works fine: I'm using it in my Modbus library.

*** Netduino as UDP master:

//create a UDP socket
            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                //try the connection against the remote device
                var remoteip = new byte[4] { 192, 168, 0, 60 };
                var ipaddr = new IPAddress(remoteip);
                var ept = new IPEndPoint(ipaddr, 502);
                socket.Connect(ept);

                var buffer = new byte[] { 1,2,3 };
                socket.Send(buffer);

                Thread.Sleep(Timeout.Infinite);
           }

*** PC as UDP slave:
//create a UDP socket
            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                //bind it to the port 502 (standard Modbus)
                var ept = new IPEndPoint(IPAddress.Any, 502);
                socket.Bind(ept);

            //loop forever
            while (true)
            {
                //look for incoming data
                int length = socket.Available;

                if (length > 0)
                {
                    var buffer = new byte[length];
                    EndPoint remote = new IPEndPoint(IPAddress.Any, 0);

                    //read the data from the physical port
                    socket.ReceiveFrom(
                        buffer,
                        ref remote);

                    // ...
                }

                Thread.Sleep(0);
            }

            }


Take care to check your firewall, and consider also that the IP you are using does not look as private. I'd try with a local network first (e.g. 192.168.x.x)
Cheers
Biggest fault of Netduino? It runs by electricity.




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.