G400 (Raptor) UDP issue

Before becoming crazy to find out why, please is there a known UDP problem on Premium Library in GHI SDK R3?


           Socket ntpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);            
            try
            {
                EndPoint remoteEP = new IPEndPoint(new IPAddress(serverAddress) , 123);

                // init request
                byte[] ntpData = new byte[48];
                Array.Clear(ntpData, 0, 48);
                ntpData[0] = 0x1B; // set protocol version

                // send request              
                ntpSocket.SendTo(ntpData, remoteEP); //<<--- EXCEPTION here!!

                // wait, if no response, timeout
                if (ntpSocket.Poll(timeout * 1000 * 1000, SelectMode.SelectRead))
                {
                    // get response
                    ntpSocket.ReceiveFrom(ntpData, ref remoteEP);
                    ntpSocket.Close();


This code idea come from FixedTimeService useful routine by eolson.
It works on my STM32F4 firmware, compiled by myself, but can’t get it working on G120 and G400 …
I missed something ?

it would be helpful to tell us what kind of an exception is occurring.

No known issues

Thanks guys, but I can’t figure out the problem.

This is the exception:

    
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::sendto [IP: 0000] ####
    #### System.Net.Sockets.Socket::SendTo [IP: 0022] ####
    #### System.Net.Sockets.Socket::SendTo [IP: 0011] ####
    #### Microsoft.SPOT.Time.FixedTimeService::GetTimeFromNTP [IP: 0062] ####
    #### Microsoft.SPOT.Time.FixedTimeService::UpdateNow [IP: 000a] ####
    #### RaptorNet.TcpIpSetup::TimeServerRun [IP: 00a3] ####

I will try with different code.

please show your network initialization code.

This is G400 Raptor init code:

        
static EthernetENC28J60 Eth1;

public static void StartEthernet(EthernetENC28J60 eth)
        {
            Eth1 = eth; 
            Eth1.Open();
            NetworkInterfaceExtension.AssignNetworkingStackTo(Eth1);

            Eth1.CableConnectivityChanged += new EthernetENC28J60.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
            //Eth1.CableConnectivityChanged += new EthernetBuiltIn.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
            Eth1.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Eth1_NetworkAddressChanged);
            Eth1.NetworkInterface.EnableStaticIP(_boardIp, _boardNetmask, _boardGw); // class C net
            string[] DnsAddresses = new string[] { "8.8.8.8", "192.168.20.1" }; //Test so hardwired in code
            Eth1.NetworkInterface.EnableStaticDns(DnsAddresses);
            Eth1.NetworkInterface.PhysicalAddress = new byte[] { 0x00, 0x21, 0x03, 0x00, 0x00, 0x0B }; // test MAC
            if (!Eth1.IsCableConnected)
            {
                NetworkAvailablityBlocking = new ManualResetEvent(false);
                do
                {
                    if (!Eth1.IsCableConnected)
                    {
                        Debug.Print("Ethernet cable is not connected yet.");
                    }
                    else
                        break;
                }
                while (!NetworkAvailablityBlocking.WaitOne(5000, false));
            }

            while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any)
            {
                Debug.Print("IP address is not set yet.");
            }
            Debug.Print("IP address is set");
        }


        static void Eth1_CableConnectivityChanged(object sender, EthernetENC28J60.CableConnectivityEventArgs e)
        //static void Eth1_CableConnectivityChanged(object sender, EthernetBuiltIn.CableConnectivityEventArgs e)
        {
            Debug.Print("Built-in Ethernet Cable is " + (e.IsConnected ? "Connected!" : "Disconnected!"));

            if (e.IsConnected)
            {
                if (NetworkAvailablityBlocking != null)
                    NetworkAvailablityBlocking.Set();
                // restart tcp listner thread
                //StartEthernet();
                Thread.Sleep(5000);
                PowerState.RebootDevice(false);
                //SMSHttpListenerThread = new Thread(new ThreadStart(TcpThreadManager));
                //SMSHttpListenerThread.Start();
            }
        }

        static void Eth1_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("New address for The built-in Ethernet Network Interface ");

            Debug.Print("Is DhCp enabled: " + Eth1.NetworkInterface.IsDhcpEnabled);
            Debug.Print("Is DynamicDnsEnabled enabled: " + Eth1.NetworkInterface.IsDynamicDnsEnabled);
            Debug.Print("NetworkInterfaceType " + Eth1.NetworkInterface.NetworkInterfaceType);
            Debug.Print("Network settings:");
            Debug.Print("IP Address: " + Eth1.NetworkInterface.IPAddress);
            Debug.Print("Subnet Mask: " + Eth1.NetworkInterface.SubnetMask);
            Debug.Print("Default Getway: " + Eth1.NetworkInterface.GatewayAddress);
            Debug.Print("Number of DNS servers:" + Eth1.NetworkInterface.DnsAddresses.Length);

            for (int i = 0; i < Eth1.NetworkInterface.DnsAddresses.Length; i++)
                Debug.Print("DNS Server " + i.ToString() + ":" + Eth1.NetworkInterface.DnsAddresses[i]);

            Debug.Print("----------------------------------------------");
        }


Note that if I use TCP Socket it works fine, also the HttpListener works fine.

@ dobova -

Do your several DNS enable you to ping and access the serverAddress that you target ?

OMG !! I got it …

Changing from this:

           
.....
            Eth1 = eth;
            Eth1.Open();
            NetworkInterfaceExtension.AssignNetworkingStackTo(Eth1); // <<--- move

            Eth1.CableConnectivityChanged += new EthernetENC28J60.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
            //Eth1.CableConnectivityChanged += new EthernetBuiltIn.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
            Eth1.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Eth1_NetworkAddressChanged);
            Eth1.NetworkInterface.EnableStaticIP(_boardIp, _boardNetmask, _boardGw); // class C net
            string[] DnsAddresses = new string[] { "8.8.8.8", "192.168.20.1" }; //Test so hardwired in code
            Eth1.NetworkInterface.EnableStaticDns(DnsAddresses);
            Eth1.NetworkInterface.PhysicalAddress = new byte[] { 0x00, 0x21, 0x03, 0x00, 0x00, 0x0B }; // test MAC
            if (!Eth1.IsCableConnected)

To this:


            Eth1 = eth;
            Eth1.Open();

            Eth1.CableConnectivityChanged += new EthernetENC28J60.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
            //Eth1.CableConnectivityChanged += new EthernetBuiltIn.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
            Eth1.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Eth1_NetworkAddressChanged);
            Eth1.NetworkInterface.EnableStaticIP(_boardIp, _boardNetmask, _boardGw); // class C net
            string[] DnsAddresses = new string[] { "8.8.8.8", "192.168.20.1" };
            Eth1.NetworkInterface.EnableStaticDns(DnsAddresses);
            Eth1.NetworkInterface.PhysicalAddress = new byte[] { 0x00, 0x21, 0x03, 0x00, 0x00, 0x0B };
            NetworkInterfaceExtension.AssignNetworkingStackTo(Eth1);
            if (!Eth1.IsCableConnected)
.....

So I used a wrong place for [em]NetworkInterfaceExtension.AssignNetworkingStackTo(Eth1);[/em] !!!
This didn’t create problem on older SDK.

@ dobova -

Was it the first time you set the IP Configuration to the ENC28 ?

I would like to understand how the position of the Assign can have such a consequence…

Sorry @ dobova I don’t mean to hijack this thread, but this is a good example of network stack initialization “best practice” that keeps coming up over and over again on this forum. It would be extremely useful for both novices and experienced NETMF developers to have this information organized in such a way that it can be browsed and searched by category, device, NETMF version, etc.

2 Likes

@ LuisCpro : This is the first ethernet setup point when program loaded into the board (after other setup job).

@ jasdev : This code I’d hijack from some sample coming from Gus for EMX boards, then revised for other specific boards. I don’t think it’s the “best” practice, but it can work. For example I completely miss the “no cable connected” problem retry in this case.

But there’s one point: we haven’t a satisfactory document consisting of tables about:
[ul]

  1. Best practice to Ethernet init on various board for ENC28 or Builtin, here included SPI and other pins related to each board. EMX Spider, G120 Cobra II, G400 HDR and Raptor and so on. I couldn’t find nothing about raptor, which socket and various INT,CS, RESET pin related, although using it as a gadgeteer socket you avoid this knowledge[/ul]
    [ul]2. Best practice to (micro)SD init and use on various board. Card Detecting, StorageDev or PersistentStorage support and so on.[/ul]
    [ul]3. Best practice to LCD init. For example all GHI display parameters needed to start from code an LCD.[/ul]
2 Likes

Edit:
Look for example if you go no-gadgeteer:


        // ********** G400 Raptor socket 1
        public static EthernetENC28J60 Eth1 = new EthernetENC28J60(SPI.SPI_module.SPI2, GHI.Hardware.G400.Pin.PB5, GHI.Hardware.G400.Pin.PB0, GHI.Hardware.G400.Pin.PA7, 4000);
        // ********** G400 HDR
        //public static EthernetENC28J60 Eth1 = new EthernetENC28J60(SPI.SPI_module.SPI2, GHI.Hardware.G400.Pin.PC22, GHI.Hardware.G400.Pin.PC31, GHI.Hardware.G400.Pin.PA5);
        // ********** G120 Cobra II
        //public static EthernetENC28J60 Eth1 = new EthernetENC28J60(SPI.SPI_module.SPI2, GHI.Hardware.G120.Pin.P1_17, GHI.Hardware.G120.Pin.P2_21 , GHI.Hardware.G120.Pin.P1_14);
        // ********** EMX Spider
        //public static EthernetBuiltIn Eth1 = new EthernetBuiltIn();

(you can use #define obviously)

This code initialize SPI for the various boards.

I think that some single-point access wiki page will be good to revise all that info.

1 Like

@ dobova - do you mind sharing the VS.Net project? I got the same problems where the local endpoint is 0.0.0.0 - and cannot seem to resolve it…

@ danibjor - Hi, I can’t share the entire project, it’s too big and is made of many modules/classes. Please which part of the code are you interested in ? I can simplify code for the porpouse.

@ dobova - thanks, but I got it working.