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

Problems in port Ethernet/IP


  • Please log in to reply
2 replies to this topic

#1 davasquez

davasquez

    Member

  • Members
  • PipPip
  • 16 posts

Posted 17 October 2012 - 05:13 AM

Hi, I want to send a message the netduino plus to Pc, via Port Ethernet/IP AF12 and Udp, i dont know if is error in code or is a problem that this port. What is error?. Thanks. // code netduino plus OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); Socket socket= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 44818); try { socket.Connect(localEndPoint); } catch (SocketException ex) { Debug.Print(ex.Message); } string texto="Hola desde Netduino"; byte[] bytesToSend=new byte[48]; bytesToSend = Encoding.UTF8.GetBytes(texto); socket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None,localEndPoint); //code PC server Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 44818); 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());

#2 Daniel Stritt

Daniel Stritt

    Member

  • Members
  • PipPip
  • 15 posts
  • LocationNorman, OK, USA

Posted 17 October 2012 - 09:12 AM

The problem might be that your end point is for "Address Any", which is used for listening sockets. You should create an endpoint that points to your pc. If you are on a network (such as a router) that the pc and netduino are connected to, you only need the IP address of your PC on your network, not the internet IP. Maybe someone can elaborate a little more if need be.
To perform the same action over and over, and expect a variant reaction, is surely a sign that you use computers.

#3 Morpheus

Morpheus

    New Member

  • Members
  • Pip
  • 2 posts

Posted 18 October 2012 - 10:25 AM

Hi, I want to send a message the netduino plus to Pc, via Port Ethernet/IP AF12 and Udp, i dont know if is error in code or is a problem that this port. What is error?. Thanks.

// code netduino plus
OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
Socket socket= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 44818);
try
{
socket.Connect(localEndPoint);
}
catch (SocketException ex)
{
Debug.Print(ex.Message);
}
string texto="Hola desde Netduino";
byte[] bytesToSend=new byte[48];
bytesToSend = Encoding.UTF8.GetBytes(texto);
socket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None,localEndPoint);

//code PC server
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 44818);
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());



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

I have used the following code and works fine. This method sends to URL the "message" from the arguments. It is useful when you have create a webserver to a server that you want to POST. Please be aware that you have to check the following. First you must check that your board has the same MAC address with that on the label. If not you can set this up from the MFDeploy tool. Then you must check that your board gets an IP. If you connect the N+ to a network you can check the DHCP flag enabled (from the MFDeploy.exe). This will make the board to acquire an ip automatically from the lan switch.

public static int PostData(string URL, string message)
        {
            try
            {
                byte[] byteArray = UTF8Encoding.UTF8.GetBytes(message);
                using (HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(URL))
                {
                    // HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(URL);
                    myReq.Method = "POST";
                    myReq.ContentType = "application/xml";
                    myReq.ContentLength = byteArray.Length;

                    using (Stream dataStream = myReq.GetRequestStream())
                    {
                        //Stream dataStream = myReq.GetRequestStream();
                        dataStream.Write(byteArray, 0, byteArray.Length);
                        dataStream.Close();
                    }

                    HttpWebResponse WebResp = (HttpWebResponse)myReq.GetResponse();

                }

                return 0;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

You have to let it all go. Fear, doubt, and disbelief. Free your mind.




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.