예제 #1
0
        public bool SyncTime()
        {
            DateTime time;
            DateTime previous = DateTime.Now;

            while ((time = DateTime.Now) == previous)
            {
                volatileTemp = 0;
            }                                                                // Spin until the second rollover (up to 1 second)
            // ...now set the time.

            if (OmApi.OM_FAILED(OmApi.OmSetTime(deviceId, OmApi.OmDateTimePack(time))))
            {
                return(false);
            }
            timeDifference = TimeSpan.Zero;
            hasChanged     = true;
            om.OnChanged(new OmDeviceEventArgs(this));
            return(true);
        }
예제 #2
0
        public bool SetInterval(DateTime start, DateTime stop)
        {
            bool failed = false;

            failed |= OmApi.OM_FAILED(OmApi.OmSetDelays(deviceId, OmApi.OmDateTimePack(start), OmApi.OmDateTimePack(stop)));
            if (!failed)
            {
                failed |= OmApi.OM_FAILED(OmApi.OmCommit(deviceId));
            }

//validData = false;
            if (!failed)
            {
                this.startTime = start;
                this.stopTime  = stop;
            }

            hasChanged = true;
            om.OnChanged(new OmDeviceEventArgs(this));
            return(!failed);
        }
예제 #3
0
        public static volatile int volatileTemp = 0;    // Junk to prevent code elimination

        public bool SyncTime()
        {
            uint newTime = 0;       // last received timestamp (packed)
            int  retries = 12;

            do
            {
                DateTime previous = DateTime.Now;
                DateTime time;
                Console.WriteLine("TIMESYNC-DEBUG: Waiting for roll-over from: " + previous);
                while ((time = DateTime.Now).Second == previous.Second)
                {
                    volatileTemp++;
                }                                                                            // Busy spin until the second rollover (up to 1 second)

                // ...now set the time (always to the nearest second)
                DateTime setTime = new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second);
                Console.WriteLine("TIMESYNC-DEBUG: Setting time: " + setTime);
                if (OmApi.OM_FAILED(OmApi.OmSetTime((int)deviceId, OmApi.OmDateTimePack(setTime))))
                {
                    Console.WriteLine("TIMESYNC: Failed to write time.");
                    continue;
                }

                // Verify that the clock was set as expected
                if (OmApi.OM_FAILED(OmApi.OmGetTime((int)deviceId, out newTime)))
                {
                    Console.WriteLine("TIMESYNC: Failed to read time.");
                    continue;
                }
                DateTime newDateTime = OmApi.OmDateTimeUnpack(newTime);
                timeDifference = newDateTime - setTime;
                Console.WriteLine("TIMESYNC-DEBUG: Received time: " + newDateTime + ", delta: " + timeDifference.TotalMilliseconds + " ms");
                if (Math.Abs(timeDifference.TotalMilliseconds) > 3000)
                {
                    Console.WriteLine("TIMESYNC: Time was not within range: " + (int)timeDifference.TotalSeconds);
                    continue;
                }
                Console.WriteLine("TIMESYNC-DEBUG: Set time ok, checking for ticks...");
                break;  // everything ok
            } while (--retries > 0);
            if (retries <= 0)
            {
                Console.WriteLine("TIMESYNC: Failed to set time successfully");
                return(false);
            }

            hasChanged = true;
            om.OnChanged(new OmDeviceEventArgs(this));

            // Verify that the clock is ticking
            DateTime checkStart = DateTime.Now;

            for (; ;)
            {
                if (OmApi.OM_FAILED(OmApi.OmGetTime((int)deviceId, out uint currentTime)))
                {
                    Console.WriteLine("TIMESYNC: Failed to read time while checking change.");
                    return(false);   // Failed to read time
                }
                if (currentTime > newTime && ((DateTime.Now - OmApi.OmDateTimeUnpack(currentTime)).TotalMilliseconds < 5000))
                {
                    break;          // The clock is ticking and within a few seconds of now
                }
                var td = DateTime.Now - checkStart;
                if (Math.Abs(td.TotalMilliseconds) > 4000)
                {
                    Console.WriteLine("TIMESYNC: Time is not ticking on device after " + (int)td.TotalSeconds + " sec.");
                    return(false);   // The clock is not ticking
                }
            }

            return(true);
        }