예제 #1
0
        public DonTime ReadTime()
        {
            port.Write("r;");
            string  reply = ReadReply();
            DonTime time  = new DonTime(reply);

            return(time);
        }
예제 #2
0
        private DonTime WriteTimeComponent(char symbol, int value)
        {
            port.Write(string.Format("w{0}{1};", symbol, value));
            string  reply = ReadReply();
            DonTime time  = new DonTime(reply);

            return(time);
        }
예제 #3
0
        static int Main(string[] args)
        {
            int errorlevel = 1;

            try {
                bool      sync                 = false;
                bool      prompt               = false;
                int       numTrials            = 1;
                DayOfWeek dayOfWeekToWrite     = DayOfWeek.Sunday;
                bool      shouldWriteDayOfWeek = false;
                int       hourToWrite          = -1;
                int       minuteToWrite        = -1;
                int       secondToWrite        = -1;

                if (args.Length > 0)
                {
                    foreach (string x in args)
                    {
                        string[] token = x.Split(':');
                        if (token.Length == 2)
                        {
                            switch (token[0])
                            {
                            case "trials":
                                if (!int.TryParse(token[1], out numTrials) || numTrials < 1)
                                {
                                    Console.WriteLine("Invalid number of trials specified: '{0}'", token[1]);
                                    return(1);
                                }
                                break;

                            case "sync":
                                sync = true;
                                break;

                            case "wd":
                                // I was using the following, but it is too strict:
                                //dayOfWeekToWrite = (DayOfWeek) Enum.Parse (typeof(DayOfWeek), token[1]);
                                dayOfWeekToWrite     = ParseDayOfWeek(token[1]);
                                shouldWriteDayOfWeek = true;
                                break;

                            case "wh":
                                hourToWrite = FilterValue(token[1], 0, 59);
                                break;

                            case "wm":
                                minuteToWrite = FilterValue(token[1], 0, 59);
                                break;

                            case "ws":
                                secondToWrite = FilterValue(token[1], 0, 59);
                                break;

                            default:
                                Console.WriteLine("Unknown option '{0}'", token[0]);
                                return(1);
                            }
                        }
                        else if (token.Length == 1)
                        {
                            switch (token[0])
                            {
                            case "sync":
                                sync = true;
                                break;

                            case "prompt":
                                prompt = true;
                                break;

                            default:
                                Console.WriteLine("Unknown option '{0}'", token[0]);
                                return(1);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Invalid command line argument '{0}'", x);
                            return(1);
                        }
                    }
                }

                const int DELAY_BETWEEN_TRIALS = 137;   // weird number to try to avoid "resonance" with clock display functions

                using (DonClock donclock = new DonClock("COM1")) {
                    if (shouldWriteDayOfWeek)
                    {
                        donclock.WriteDay(dayOfWeekToWrite);
                    }

                    if (hourToWrite >= 0 && hourToWrite <= 59)
                    {
                        donclock.WriteHour(hourToWrite);
                    }

                    if (minuteToWrite >= 0 && minuteToWrite <= 59)
                    {
                        donclock.WriteMinute(minuteToWrite);
                    }

                    if (secondToWrite >= 0 && secondToWrite <= 59)
                    {
                        donclock.WriteSecond(secondToWrite);
                    }

                    double   averageDriftInSeconds = 0.0;
                    double[] diffArray             = new double[numTrials];

                    for (int i = 0; i < numTrials; ++i)
                    {
                        if (i > 0)
                        {
                            System.Threading.Thread.Sleep(DELAY_BETWEEN_TRIALS);
                        }

                        DateTime now     = DateTime.Now;
                        DonTime  dontime = donclock.ReadTime();
                        DonTime  systime = new DonTime(DateTime.Now);

                        double diffInSeconds = 0.001 * (dontime.TotalMilliseconds - systime.TotalMilliseconds);
                        diffArray[i]          = diffInSeconds;
                        averageDriftInSeconds = diffInSeconds;      // will be overwritten with true average later, if numTrials > 0.

                        if (i == 0 || i == numTrials - 1)
                        {
                            if (i > 0)
                            {
                                Console.WriteLine();
                                Console.WriteLine();
                            }
                            Console.WriteLine("dontime  = {0}   {1} ms", dontime, dontime.TotalMilliseconds);
                            Console.WriteLine("systime  = {0}   {1} ms", systime, systime.TotalMilliseconds);
                            Console.WriteLine("diff = {0:0.000}", diffInSeconds);
                            Console.WriteLine();
                        }
                        else
                        {
                            Console.Write("\rTrial #{0}", (1 + i));
                        }
                    }

                    if (numTrials > 1)
                    {
                        double dev = StandardDeviation(diffArray, out averageDriftInSeconds);
                        Console.WriteLine("Average = {0:0.00000}, Standard Deviation = {1:0.00000}", averageDriftInSeconds, dev);
                    }

                    if (sync)
                    {
                        if (Math.Abs(averageDriftInSeconds) >= 0.001)
                        {
                            double  adjustment  = -averageDriftInSeconds;
                            DonTime updatedTime = donclock.AdjustTime(adjustment);
                            Console.WriteLine("Clock time was adjusted by {0:0.000} seconds.", adjustment);
                            Console.WriteLine("Clock reported updated time as {0}", updatedTime);
                        }
                        else
                        {
                            Console.WriteLine("No clock adjustment was needed.");
                        }
                    }

                    if (prompt)
                    {
                        Console.WriteLine("Entering raw prompt mode.  Enter a blank line to exit...");
                        while (true)
                        {
                            string line = Console.ReadLine();
                            if (string.IsNullOrEmpty(line))
                            {
                                break;
                            }
                            string response = donclock.SendRawCommand(line);
                            Console.WriteLine("{0}", response);
                        }
                    }

                    errorlevel = 0;
                }
            } catch (Exception e) {
                errorlevel = 2;
                Console.WriteLine("ERROR:  {0}", e.Message);
            }

            return(errorlevel);
        }