Main Site Documentation

Got the RTC running on my Cobra


#1

I added the 12mm coin battery inside the encloser of my Cobra! :smiley:
Now, I don’t have to look at the information clock that always start at 00:00:00 PM any more! ;D ;D


#2

Nice!

I just set mine up to sync to NTP. It’s almost always connected to Ethernet, so it works out pretty well.


#3

Do you have the power supply connecting to your Cobra all the time?


#4

I think he means that he sync the clock on power up :slight_smile:


#5

Ah, my bad asking a question without thinking… :-[


#6

Hahah, I do have it almost always plugged in, but I use this method to sync the system clock to NTP:

        /// <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">Timeserver</param>
        /// <returns>Local NTP Time</returns>
        public static DateTime NTPTime(String TimeServer)
        {
            // Find endpoint for timeserver
            IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123);

            // Connect to timeserver
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            s.Connect(ep);

            // Make send/receive buffer
            byte[] ntpData = new byte[48];
            Array.Clear(ntpData, 0, 48);

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

            // Send Request
            s.Send(ntpData);

            // Receive Time
            s.Receive(ntpData);

            byte offsetTransmitTime = 40;

            ulong intpart = 0;
            ulong fractpart = 0;

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

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

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

            s.Close();

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

            TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
            DateTime networkDateTime = (dateTime + offsetAmount);

            return networkDateTime;
        }

#7

Thanks Chris ;D