@ Gabo2 -
I have small code that I usually use for testing UDP on EMX - JD11
You can copy it and just run. It should work. One locate on EMX, and another on PC
EMX: Send / receive
using System;
using Microsoft.SPOT;
using System;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Security;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net;
using Microsoft.SPOT.Net.NetworkInformation;
using GHI.Premium.Net;
using GHI.Premium.Hardware;
using GHINET = GHI.Premium.Net;
namespace Test_UDP_EMX
{
public class Program
{
public static GHINET.EthernetBuiltIn ethernet_J11D;
static InputPort button = new InputPort((Cpu.Pin)30, false, Port.ResistorMode.PullUp); // Please connect a button to pin 30, socket 8
static OutputPort led = new OutputPort((Cpu.Pin)47, true);
static Boolean isNetworkConfigReady = false;
public static void Main()
{
while (button.Read())
{
led.Write(true);
Thread.Sleep(50);
led.Write(false);
Thread.Sleep(50);
}
StartComunication();
}
static void Interface_NetworkAddressChanged(object sender, EventArgs e)
{
isNetworkConfigReady = false;
Debug.Print("New address for the Network Interface ");
Debug.Print("Is DhCp enabled: " + ethernet_J11D.NetworkInterface.IsDhcpEnabled);
Debug.Print("Is DynamicDnsEnabled enabled: " + ethernet_J11D.NetworkInterface.IsDynamicDnsEnabled);
Debug.Print("NetworkInterfaceType " + ethernet_J11D.NetworkInterface.NetworkInterfaceType);
Debug.Print("Network settings:");
Debug.Print("IP Address: " + ethernet_J11D.NetworkInterface.IPAddress);
Debug.Print("Subnet Mask: " + ethernet_J11D.NetworkInterface.SubnetMask);
Debug.Print("Default Gateway: " + ethernet_J11D.NetworkInterface.GatewayAddress);
Debug.Print("Number of DNS servers:" + ethernet_J11D.NetworkInterface.DnsAddresses.Length);
for (int i = 0; i < ethernet_J11D.NetworkInterface.DnsAddresses.Length; i++)
Debug.Print("DNS Server " + i.ToString() + ":" + ethernet_J11D.NetworkInterface.DnsAddresses[i]);
Debug.Print("------------------------------------------------------");
if (ethernet_J11D.NetworkInterface.IPAddress !="0.0.0.0")
isNetworkConfigReady = true;
}
static void StartComunication()
{
ethernet_J11D = new GHINET.EthernetBuiltIn();
if (!ethernet_J11D.IsOpen)
ethernet_J11D.Open();
if (!ethernet_J11D.NetworkInterface.IsDhcpEnabled)
ethernet_J11D.NetworkInterface.EnableDhcp();
ethernet_J11D.NetworkAddressChanged += Interface_NetworkAddressChanged;
GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D);
while (isNetworkConfigReady == false) // wait for network is ready
{
Thread.Sleep(50);
}
System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress ip = new IPAddress(new byte[] { your PC ip });
IPEndPoint endPoint = new IPEndPoint(ip, 2000);
socket.Connect(endPoint);
String msg = "Hello PC, I am FEZ spider";
byte[] bytesToSend = Encoding.UTF8.GetBytes(msg);
while (true)
{
socket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None, endPoint);
while (socket.Poll(2000000, SelectMode.SelectRead))
{
if (socket.Available > 0)
{
byte[] inBuf = new byte[socket.Available];
EndPoint recEndPoint = new IPEndPoint(IPAddress.Any, 0);
socket.ReceiveFrom(inBuf, ref recEndPoint);
if (!recEndPoint.Equals(endPoint))// Check if the received packet is from the 192.168.0.2
continue;
Debug.Print(new String(Encoding.UTF8.GetChars(inBuf)));
}
}
Thread.Sleep(100);
}
}
}
}
On PC: Send /receive
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Test_UDP_PC
{
class Program
{
static void Main(string[] args)
{
Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint MyServiceEndPoint = new IPEndPoint(IPAddress.Any, 2000);
mySocket.Bind(MyServiceEndPoint);
while (true)
{
while (mySocket.Poll(200000, SelectMode.SelectRead))
{
if (mySocket.Available > 0)
{
byte[] inBuf = new byte[mySocket.Available];
EndPoint recEndPoint = new IPEndPoint(IPAddress.Any, 0);
mySocket.ReceiveFrom(inBuf, ref recEndPoint);
Console.WriteLine("Message From " + ((IPEndPoint)recEndPoint).Address.ToString());
Console.WriteLine(new string(Encoding.UTF8.GetChars(inBuf)));
String msg = "Hello FEZ Spider! I am PC";
byte[] bytesToSend = Encoding.UTF8.GetBytes(msg);
mySocket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None,
(IPEndPoint)recEndPoint);
}
}
}
}
}
}