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
Send message Netduino To Pc Via Ethernet/IP
Started by
davasquez
, Oct 17 2012 09:10 PM
1 reply to this topic
#1
Posted 17 October 2012 - 09:10 PM
#2
Posted 18 October 2012 - 05:16 AM
The following code works fine: I'm using it in my Modbus library.
*** Netduino as UDP master:
*** PC as UDP slave:
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
*** 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