示例#1
0
        private static void ObserverThread()
        {
            try
            {
                var device = new USBM();

                if (!device.OpenDevice())
                {
                    throw new Exception("Could not open device.");
                }

                while (true)
                {
                    var volts = device.GetMeasuredValue();

                    if (volts == 0.0f)
                    {
                        throw new Exception("Value read from device was zero. This indicates the device may not be working. Restarting to try to recover.");
                    }

                    var ppmPerVolt = 197.5f; // Experimentally determined - looks perfectly linear.
                    var ppm        = (int)(volts * ppmPerVolt);

                    Measurements.Inc();
                    Volts.Set(volts);
                    Ppm.Set(ppm);

                    Console.WriteLine($"{volts:#00.000} V\t\t{ppm} PPM");

                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
            catch (Exception ex)
            {
                // Oh no! This is fatal error.
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Will restart after sleeping for a while.");
                Thread.Sleep(TimeSpan.FromSeconds(30));
                Process.GetCurrentProcess().Kill();
            }
        }