SNTP Client for TinyCLR available?

Hi, are already examples for SNTP Client and Timeservice available ?

There was something for netmf that your can easily port. I am not sure what TinyCLR

i created this one - hope this could help you …

called namespace

using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;

method


     /* compatible with .NET Microframework and nanoFramework too        
       https://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c      
       */
        public static DateTime GetNetworkTime(int CorrectLocalTime=0)
        {
            const string ntpServer = "pool.ntp.org";
            var ntpData = new byte[48];
            ntpData[0] = 0x1B; //LeapIndicator = 0 (no warning), VersionNum = 3 (IPv4 only), Mode = 3 (Client Mode)

            var addresses = Dns.GetHostEntry(ntpServer).AddressList;
            var ipEndPoint = new IPEndPoint(addresses[0], 123);
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.Connect(ipEndPoint);

            Thread.Sleep(1); // added to support TinyCLR OS too

            socket.Send(ntpData);
            socket.Receive(ntpData);
            socket.Close();

            ulong intPart = (ulong)ntpData[40] << 24 | (ulong)ntpData[41] << 16 | (ulong)ntpData[42] << 8 | (ulong)ntpData[43];
            ulong fractPart = (ulong)ntpData[44] << 24 | (ulong)ntpData[45] << 16 | (ulong)ntpData[46] << 8 | (ulong)ntpData[47];

            var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
            var networkDateTime = (new DateTime(1900, 1, 1)).AddMilliseconds((long)milliseconds);

            return networkDateTime.AddHours(CorrectLocalTime);
        }

you can call it in your application as below

    DateTime dateTime = GetNetworkTime(2);
2 Likes

Sweet! Do you mind if we add this code to docs?

1 Like

yes you can use and publish it on doc

but need to be tested with TinyClrOS if it work as is,

still not tested with TinyCLR OS in wait for Wifi and those days i receive enc28j60 …

i will be able to test sntp and in wait to test MQTT.

there’s a toolkit for .net microframework

ntp
smtp
pop
…

i belive it could easy convert into TinyCLR

Thanks, @valon_hoti_gmail_com
Your code worked on the SC20260D board with the HanRun HR9111105A Enc28 Ethernet module.
Only one little thing had to be changed.
I had to add a Thread.Sleep(0); after socket.Connect(ipEndPoint);

1 Like

thank you so now I updated sample above with Thread.Sleep(1); //because Thread.Sleep(0) is about 30 - 50 microcesonds , so better is 1000 microseconds or 1 millisecond …

1 Like

I think from the amount of time it doesn’t matter if it is 1 ms or less. But sometimes it Thread.Sleep(0) is sufficient to let things in the background (other threads) take place before the code continues.
Btw: I think that for a “real world” application some things should be added: timeout for the socket.receive command, putting the socket in a Using statement, handling situations where the DNS server doesn’t respond etc.

1 Like

I have now ported @eolson NETMF “FixedTimeservice” to TinyCLR. If GHI doesn’t object because of the NDA I could post an example project on GitHub

2 Likes

Your code/project doesn’t release any NDA covered materials. Please do not include the TinyCLR NuGet packages in your project.

Thanks for asking.

1 Like

Here is the TimeService Code.


It is not yet tested thoroughly, please give feedback if it doesn’t work as expected.

2 Likes