Use time in TinyCLR

I work with a G120 module based controller and would like to set the local time in the controller, since the controller has to work for a certain amount of time (e.g. 1-20000 hours).
In .NETMF there was a function:
Microsoft.SPOT.Hardware.Utility.SetLocalTime(presentTime);
What can I use in TinyCLR?

I found GHIElectronics.TinyCLR.Devices.Rtc
Do you have an example how to set a data/time and how to retrieve it back?

check this sample from @Bauland

1 Like

There are 2 time services. One that runs with the system. When the loose power the time will reset. The second is RTC which can run off a small battery. When the system is powered off, the RTC continues to run using its battery.

Which one do you need?

The first one. My board is custom made and has no battery, although it would be a good option for the next version. The controller communicates with a pc through CAN communication.
It will initially get the time from the pc and my idea is to let it synchronize with the pc from time to time and after a reset.

static void Test_RTC()
        {
            var rtc = RtcController.GetDefault();
            var m = new DateTime(2018, 11, 29, 11, 40, 26);

            var newDt = RtcDateTime.FromDateTime(m);


            while (true)
            {
                if (ldr1.Read() == GpioPinValue.Low)
                {
                    while (ldr1.Read() == GpioPinValue.Low) ;
                    rtc.SetTime(newDt);
                }

                if (rtc.IsValid)
                {
                    var n = rtc.GetTime();
                    System.Diagnostics.Debug.WriteLine("Now : " + n.Month + "/" + n.DayOfMonth + "/" + n.Year + ", Day of Week " + n.DayOfWeek + ", Time: " + n.Hour + ":" + n.Minute + ":" + n.Second);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("RTC isn't set yet");
                }

                Thread.Sleep(250);

            }
        }
1 Like

In the GHIElectronics.TinyCLR.Native package, take a look at GHIElectronics.TinyCLR.Native.SystemTime.SetTime(DateTime, int). After you call that DateTime.Now will return the updated time.

2 Likes