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

Netduino + hangs while calling time using NTP service


  • Please log in to reply
8 replies to this topic

#1 skarphedinnos

skarphedinnos

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIceland

Posted 24 October 2012 - 09:51 PM

I posted my problem to another thread. Since I haven't got any response I thought it might be cause of the title. Anyways, here is my post again. Hope someone can help me out :) Hi. I have been trying this NTP for couple of days now. I have the same problem, the program hangs on s.Receive(ntpData). I have tried various NTP servers, both server names and ip addresses. The ND is connected to ethernet on laptop using bridged connection between the ethernet and wifi. If I put some NTP server name I can see it's IP while debugging. BTW I'm using latest firmware(4.201). Any ideas? Or can someone post code with NTP server that is currently working? Thanks, Skarphedinn.

#2 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 24 October 2012 - 10:56 PM

Skarphedinnos, I am not best person to help on this question, but I might be able to if I had a little more information. For instance having the code to look at would allow someone to verify proper usage and declarations. What classes are you using? Assuming that you are using C# are you using the proper case etc. Please provide more information and I think you will likely get the help that you are looking for. Chuck

#3 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 24 October 2012 - 11:09 PM

Sounds like your server may be busy or down for maintenance. I use the DayTime Protocol with the University of Colorado, Boulder servers and they are always available,

http://tf.nist.gov/tf-cgi/servers.cgi

You may also have a network problem. Do a ping sweep on your router DHCP address range and then then arp -a from the command line and see if the Netduino and laptop show up. I use a fixed IP address for the Netduino and a wireless to ehernet adapter. In this configuration, the Netduino will take the MAC address of the wireless adapter.
C:\Users\beb\Desktop>arp -a

Interface: 192.168.0.126 --- 0xb
  Internet Address      Physical Address      Type
  192.168.0.1           98-fc-11-6e-6a-40     dynamic
  192.168.0.3           64-70-02-32-6e-38     dynamic <-- Ethernet/wireless adapter(voIP)
  192.168.0.4           80-1f-02-19-25-98     dynamic <-- Ethernet/wireless adapter (Netduino)
  192.168.0.50          80-1f-02-19-25-98     dynamic <-- Netduino Plus
  192.168.0.102         4c-e6-76-12-ba-24     dynamic
  192.168.0.103         98-4b-e1-55-78-d6     dynamic
  192.168.0.114         c8-bc-c8-71-d6-21     dynamic
  192.168.0.117         f4-ce-46-4d-22-bc     dynamic
  192.168.0.128         64-70-02-32-6e-38     dynamic <-- VoIP
  192.168.0.136         08-ed-b9-3d-00-0b     dynamic
  192.168.0.138         00-1e-64-24-94-98     dynamic
  192.168.0.140         c4-17-fe-ac-99-b6     dynamic
  192.168.0.142         9c-ad-ef-00-18-78     dynamic
  192.168.0.255         ff-ff-ff-ff-ff-ff     static
  224.0.0.2             01-00-5e-00-00-02     static
  224.0.0.22            01-00-5e-00-00-16     static
  224.0.0.251           01-00-5e-00-00-fb     static
  224.0.0.252           01-00-5e-00-00-fc     static
  239.255.255.250       01-00-5e-7f-ff-fa     static
  255.255.255.255       ff-ff-ff-ff-ff-ff     static

If your Netduino and laptop do not show up in a table like this, then you have a network configuration problem.

Baxter

#4 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 24 October 2012 - 11:25 PM

Forgot to attach a batch file to do a ping sweep and then arp. I found this on Stackoverflow. Rename to suffix .bat Baxter

Attached Files



#5 skarphedinnos

skarphedinnos

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIceland

Posted 25 October 2012 - 02:33 AM

Skarphedinnos,

I am not best person to help on this question, but I might be able to if I had a little more information.

For instance having the code to look at would allow someone to verify proper usage and declarations.
What classes are you using?

Assuming that you are using C# are you using the proper case etc.

Please provide more information and I think you will likely get the help that you are looking for.

Chuck

Hi Chuck.

Here is the code:
using System;
using System.Net;
using System.Net.Sockets;
using Microsoft.SPOT;


public static class Ntp
{
    public static bool UpdateTimeFromNtpServer(string server, int timeZoneOffset)
    {
        try
        {
            var currentTime = GetNtpTime(server, timeZoneOffset);
            Microsoft.SPOT.Hardware.Utility.SetLocalTime(currentTime);

            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// Get DateTime from NTP Server
    /// Based on:
    /// http://weblogs.asp.net/mschwarz/archive/2008/03/09/wrong-datetime-on-net-micro-framework-devices.aspx
    /// </summary>
    /// <param name="timeServer">Time Server (NTP) address</param>
    /// <param name="timeZoneOffset">Difference in hours from UTC</param>
    /// <returns>Local NTP Time</returns>
    private static DateTime GetNtpTime(String timeServer, int timeZoneOffset)
    {
        // Find endpoint for TimeServer
        var ep = new IPEndPoint(Dns.GetHostEntry(timeServer).AddressList[0], 123);

        // Make send/receive buffer
        var ntpData = new byte[48];

        // Connect to TimeServer
        using (var s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
        {
            // Set 10s send/receive timeout and connect
            s.SendTimeout = s.ReceiveTimeout = 10000; // 10,000 ms
            s.Connect(ep);

            // Set protocol version
            ntpData[0] = 0x1B;

            // Send Request
            s.Send(ntpData);

            // Receive Time
            s.Receive(ntpData);

            // Close the socket
            s.Close();
        }

        const byte offsetTransmitTime = 40;

        ulong intpart = 0;
        ulong fractpart = 0;

        for (var i = 0; i <= 3; i++)
            intpart = (intpart << 8) | ntpData[offsetTransmitTime + i];

        for (var i = 4; i <= 7; i++)
            fractpart = (fractpart << 8) | ntpData[offsetTransmitTime + i];

        ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);

        var timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
        var dateTime = new DateTime(1900, 1, 1);
        dateTime += timeSpan;

        var offsetAmount = new TimeSpan(timeZoneOffset, 0, 0);
        var networkDateTime = (dateTime + offsetAmount);

        return networkDateTime;
    }
}
I have tried various examples I found here and there, but they are basically all the same. The program seems to having hard time here:
// Receive Time
            s.Receive(ntpData);




Sounds like your server may be busy or down for maintenance. I use the DayTime Protocol with the University of Colorado, Boulder servers and they are always available,

http://tf.nist.gov/tf-cgi/servers.cgi

You may also have a network problem. Do a ping sweep on your router DHCP address range and then then arp -a from the command line and see if the Netduino and laptop show up. I use a fixed IP address for the Netduino and a wireless to ehernet adapter. In this configuration, the Netduino will take the MAC address of the wireless adapter.

C:\Users\beb\Desktop>arp -a

Interface: 192.168.0.126 --- 0xb
  Internet Address      Physical Address      Type
  192.168.0.1           98-fc-11-6e-6a-40     dynamic
  192.168.0.3           64-70-02-32-6e-38     dynamic <-- Ethernet/wireless adapter(voIP)
  192.168.0.4           80-1f-02-19-25-98     dynamic <-- Ethernet/wireless adapter (Netduino)
  192.168.0.50          80-1f-02-19-25-98     dynamic <-- Netduino Plus
  192.168.0.102         4c-e6-76-12-ba-24     dynamic
  192.168.0.103         98-4b-e1-55-78-d6     dynamic
  192.168.0.114         c8-bc-c8-71-d6-21     dynamic
  192.168.0.117         f4-ce-46-4d-22-bc     dynamic
  192.168.0.128         64-70-02-32-6e-38     dynamic <-- VoIP
  192.168.0.136         08-ed-b9-3d-00-0b     dynamic
  192.168.0.138         00-1e-64-24-94-98     dynamic
  192.168.0.140         c4-17-fe-ac-99-b6     dynamic
  192.168.0.142         9c-ad-ef-00-18-78     dynamic
  192.168.0.255         ff-ff-ff-ff-ff-ff     static
  224.0.0.2             01-00-5e-00-00-02     static
  224.0.0.22            01-00-5e-00-00-16     static
  224.0.0.251           01-00-5e-00-00-fb     static
  224.0.0.252           01-00-5e-00-00-fc     static
  239.255.255.250       01-00-5e-7f-ff-fa     static
  255.255.255.255       ff-ff-ff-ff-ff-ff     static

If your Netduino and laptop do not show up in a table like this, then you have a network configuration problem.

Baxter


Hi Baxter.
Thanks for good comment. I am used to do basic things in command prompt but I havent seen this "arp -a" before, brilliant :)
This seems to be the problem. The thing is I have my ND connected to the laptop and the laptop is connected wifi. Then those two connections are bridget together. If I use other pc (hardwired) and do "arp -a" I cannot see either the laptop nor the ND.
I will try connecting the ND directly to the router :)
Thanks guys...

#6 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 25 October 2012 - 02:57 AM

Skarphedinnos, It didn't find any problems in the code, but it looks like baxter may have you on the right track. It will try it on my ND+ and computer tommorrow. I normal connect the Netduino to the router via a IOGear WiFi adapter that is ported for access via the internet remotely. It is a GWU627 a are reasonablly priced. Good Luck, Chuck

#7 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 25 October 2012 - 08:21 AM

use the timeout. sktTimeServer.SendTimeout = 5000 sktTimeServer.ReceiveTimeout = 5000

#8 skarphedinnos

skarphedinnos

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIceland

Posted 25 October 2012 - 10:24 AM

Got it to work :) I simply connected the ND to my router directly and whallahh. Thanks a lot guys. Skarphedinn.

#9 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 25 October 2012 - 10:52 AM

Got it to work :)
I simply connected the ND to my router directly and whallahh.

Thanks a lot guys.

Skarphedinn.

and Girls. :P




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.