![Photo](http://www.gravatar.com/avatar/24e4eaf77166b7a1e82c49b71109296f?s=100&d=http%3A%2F%2Fforums.netduino.com%2Fpublic%2Fstyle_images%2Fmaster%2Fprofile%2Fdefault_large.png)
Problems in port Ethernet/IP
#1
Posted 17 October 2012 - 05:13 AM
#2
Posted 17 October 2012 - 09:12 AM
#3
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; } }
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users