Beispiel #1
0
 private void TurnOffClose(List <TDK> tdk, bool overvolt = false,
                           bool oversmoke = false, bool overtemp = false)
 {
     foreach (var t in tdk)
     {
         _serTDK.Write("ADR " + t.Address + "\r\n");
         _serTDK.Write("OUT OFF\r\n");
         if (_serTDK.ReadLine() == "OK")
         {
             U.Logger.WriteLine($"Port #{t.Port} OFF: OKAY");
         }
         _serTDK.DiscardOutBuffer();
         _serTDK.DiscardInBuffer();
     }
     StartTimer();
     // wait and get updated measured value from TDKs for the GUI
     Wait(500);
     foreach (var tt in tdk)
     {
         string volt    = MeasureVoltage();
         string current = MeasureCurrent();
         _args = new GUIArgs(volt, current, tt.CycleCount, tt.Port, _cycleTimer,
                             closing: true, overvolt: overvolt, oversmoke: oversmoke, overtemp: overtemp);
         NewCoreCommand?.Invoke(this, new CoreCommand()
         {
             Type = U.CmdType.UpdateUI
         });
     }
     _serTDK.Close();
 }
        private void TurnOffClose(List <TDK> tdk)
        {
            foreach (var t in tdk)
            {
                _serTDK.Write("ADR " + t.Address + "\r\n");
                _serTDK.Write("OUT OFF\r\n");
                if (_serTDK.ReadLine() == "OK")
                {
                    Console.WriteLine($"Port #{t.Port} OFF: OKAY");
                }
                _serTDK.DiscardOutBuffer();
                _serTDK.DiscardInBuffer();
            }
            StartTimer();
            Wait(500);
            foreach (var tt in tdk)
            {
                _serTDK.Write("MV?\r\n");
                Wait(50); // lag in measured value
                string volt = _serTDK.ReadLine();

                _serTDK.Write("MC?\r\n");
                Wait(50);
                string current = _serTDK.ReadLine();
                _args = new GUIArgs(volt, current, tt.CycleCount, tt.Port, _cycleTimer);
                NewCoreCommand?.Invoke(this, new CoreCommand()
                {
                    Type = U.CmdType.UpdateUI
                });
            }
            _serTDK.Close();
        }
Beispiel #3
0
        public void StartCycling(StartCyclingArgs args)
        {
            _serTDK = new SerialPort();
            var tdk = args.TDK;

            if (tdk.Count < 1)
            {
                return;
            }
            U.Logger.WriteLine("Waiting for HW interlock");
            NewCoreCommand?.Invoke(this, new CoreCommand()
            {
                Type = U.CmdType.UpdateHeartBeatPacket
            });
            Thread.Sleep(2000); // wait for HW interlock to disengage
            OpenPorts();
            try {
                foreach (var t in tdk)
                {
                    try {
                        SetAddress(t);
                        SetCurrentVoltage(t);
                        t.Connected = true;
                    }
                    catch (TimeoutException exc) {
                        U.Logger.WriteLine($"TIMEOUT ON PORT #{t.Port}");
                        throw new Exception(exc.Message);
                    }
                }
                // start bias cycle
                foreach (var t in tdk)
                {
                    t.CycleCount++;
                    t.PastVoltages = new List <string>();
                }

                // save at a slower interval for China
                var saveTimeON  = Environment.MachineName.ToUpper() == "CH-J7TMTZ1" ? 5000 : U.ResultsSaveTimeON;
                var saveTimeOFF = Environment.MachineName.ToUpper() == "CH-J7TMTZ1" ? 60000 : U.ResultsSaveTimeOFF;

                _TDK        = tdk;
                _totalTimer = new Stopwatch();
                _totalTimer.Start();
                _voltageTimer = new Stopwatch();
                _voltageTimer.Start();
                DataError = false; // assume connection is good at start
                // Loop forever until we get a stop command from main thread
                while (true)
                {
                    _intoCycleTimer = new Stopwatch();
                    _intoCycleTimer.Start();

                    // BIAS ON
                    foreach (var t in tdk)
                    {
                        TurnON(t);
                    }
                    BIASON = true;
                    NewCoreCommand?.Invoke(this, new CoreCommand()
                    {
                        Type = U.CmdType.UpdateHeartBeatPacket
                    });
                    StartTimer();
                    StartResultsTimer();
                    _cycleTimer = DateTime.Now.AddMilliseconds(args.BiasOnTime);
                    while (_biasTimer.ElapsedMilliseconds < args.BiasOnTime &&
                           !STOP && !TEMPALARM && !SMOKEALARM)
                    {
                        if (_resultsTimer.ElapsedMilliseconds > saveTimeON)
                        {
                            SAVE = true;
                        }
                        foreach (var tt in tdk)
                        {
                            if (STOP || SMOKEALARM || TEMPALARM)
                            {
                                break;
                            }
                            SetAddress(tt);
                            _serTDK.DiscardInBuffer();

                            tt.Voltage = MeasureVoltage();
                            tt.Current = MeasureCurrent();
                            _args      = new GUIArgs(tt.Voltage, tt.Current, tt.CycleCount, tt.Port, _cycleTimer);
                            NewCoreCommand?.Invoke(this, new CoreCommand()
                            {
                                Type = U.CmdType.UpdateUI
                            });
                            if (SAVE)
                            {
                                SaveResultsBQ(tt);
                            }

                            if (double.Parse(tt.Voltage) > args.MaxOverVoltage)
                            {
                                STOP           = true;
                                OVERMAXVOLTAGE = true;
                                U.Logger.WriteLine($"{tt.SampleName} Overvoltaged!!");
                            }
                        }
                        if (STOP || SMOKEALARM || TEMPALARM)
                        {
                            break;
                        }
                        // if we have saved then restart timer
                        if (SAVE)
                        {
                            SAVE = false;
                            _resultsTimer.Restart();
                        }
                    }
                    if (STOP || SMOKEALARM || TEMPALARM)
                    {
                        break;
                    }
                    // BIAS OFF
                    TurnOff(tdk);
                    BIASON = false;
                    NewCoreCommand?.Invoke(this, new CoreCommand()
                    {
                        Type = U.CmdType.UpdateHeartBeatPacket
                    });
                    StartTimer();
                    StartResultsTimer();
                    _cycleTimer = DateTime.Now.AddMilliseconds(args.BiasOffTime);
                    while (_biasTimer.ElapsedMilliseconds < args.BiasOffTime &&
                           !STOP && !TEMPALARM && !SMOKEALARM)
                    {
                        if (_resultsTimer.ElapsedMilliseconds > saveTimeOFF)
                        {
                            SAVE = true;
                        }
                        foreach (var tt in tdk)
                        {
                            if (STOP || SMOKEALARM || TEMPALARM)
                            {
                                break;
                            }
                            tt.Voltage = MeasureVoltage();
                            tt.Current = MeasureCurrent();
                            _args      = new GUIArgs(tt.Voltage, tt.Current, tt.CycleCount, tt.Port, _cycleTimer);
                            NewCoreCommand?.Invoke(this, new CoreCommand()
                            {
                                Type = U.CmdType.UpdateUI
                            });
                            if (SAVE)
                            {
                                SaveResultsBQ(tt);
                            }
                        }
                        if (STOP || SMOKEALARM || TEMPALARM)
                        {
                            break;
                        }

                        // if we have saved then restart timer
                        if (SAVE)
                        {
                            SAVE = false;
                            _resultsTimer.Restart();
                        }
                    }
                    if (STOP || SMOKEALARM || TEMPALARM)
                    {
                        break;
                    }

                    // completed a bias on/off cycle
                    foreach (var ttt in tdk)
                    {
                        ttt.CycleCount++;
                    }
                }
            }
            catch (Exception exc) {
                U.Logger.WriteLine($"{exc}");
                BIASON         = false;
                STOP           = false;
                SMOKEALARM     = false;
                TEMPALARM      = false;
                GUISTOP        = false;
                EMSSTOP        = false;
                OVERMAXVOLTAGE = false;
                TurnOffClose(tdk);
                U.Logger.WriteLine($"TDK closing with exception");
            }
            var cmd = EMSSTOP ? "EMSSTOP" : SMOKEALARM ? "SMOKEALARM" : GUISTOP ? "GUI Stop" : OVERMAXVOLTAGE ? "Over Max Voltage" : "TEMPALARM";

            U.Logger.WriteLine($"Encountered {cmd} while Cycling!");
            BIASON  = false;
            STOP    = false;
            EMSSTOP = false;
            GUISTOP = false;
            TurnOffClose(tdk, OVERMAXVOLTAGE, SMOKEALARM, TEMPALARM);
            OVERMAXVOLTAGE = false;
            SMOKEALARM     = false;
            TEMPALARM      = false;
            U.Logger.WriteLine($"TDK Closing Normally");
        }
        public void StartCycling(StartCyclingArgs args)
        {
            _serTDK = new SerialPort();
            OpenPorts();
            var tdk = args.TDK;

            try {
                foreach (var t in tdk)
                {
                    try {
                        SetAddress(t);
                        SetCurrentVoltage(t);
                        t.Connected = true;
                    }
                    catch (TimeoutException exc) {
                        Console.WriteLine($"TIMEOUT ON PORT #{t.Port}");
                        throw new Exception(exc.Message);
                    }
                }
                _TDK        = tdk;
                _resultsDir = args.ResultsDirectory;
                _totalTimer = new Stopwatch();
                _totalTimer.Start();

                // Loop forever until we get a stop command from main thread
                while (true)
                {
                    _intoCycleTimer = new Stopwatch();
                    _intoCycleTimer.Start();

                    // BIAS ON
                    foreach (var t in tdk)
                    {
                        TurnON(t);
                    }
                    BIASON = true;
                    NewCoreCommand?.Invoke(this, new CoreCommand()
                    {
                        Type = U.CmdType.UpdateHeartBeatPacket
                    });
                    StartTimer();
                    StartResultsTimer();
                    _cycleTimer = DateTime.Now.AddMilliseconds(args.BiasOnTime);
                    while (_timer.ElapsedMilliseconds < args.BiasOnTime &&
                           !STOP && !TEMPALARM && !SMOKEALARM)
                    {
                        if (_resultsTimer.ElapsedMilliseconds > 1000)
                        {
                            SAVE = true;
                        }
                        foreach (var tt in tdk)
                        {
                            SetAddress(tt);

                            _serTDK.Write("MV?\r\n");
                            Wait(50); // lag in measured value
                            tt.Voltage = _serTDK.ReadLine();

                            _serTDK.Write("MC?\r\n");
                            Wait(50);
                            tt.Current = _serTDK.ReadLine();
                            _args      = new GUIArgs(tt.Voltage, tt.Current, tt.CycleCount, tt.Port, _cycleTimer);
                            NewCoreCommand?.Invoke(this, new CoreCommand()
                            {
                                Type = U.CmdType.UpdateUI
                            });
                            if (SAVE)
                            {
                                SaveResults(tt);
                            }
                        }
                        // if we have saved then restart timer
                        if (SAVE)
                        {
                            SAVE = false;
                            _resultsTimer.Restart();
                        }
                    }
                    if (STOP || SMOKEALARM || TEMPALARM)
                    {
                        break;
                    }
                    // BIAS OFF
                    TurnOff(tdk);
                    BIASON = false;
                    NewCoreCommand?.Invoke(this, new CoreCommand()
                    {
                        Type = U.CmdType.UpdateHeartBeatPacket
                    });
                    StartTimer();
                    StartResultsTimer();
                    _cycleTimer = DateTime.Now.AddMilliseconds(args.BiasOffTime);
                    while (_timer.ElapsedMilliseconds < args.BiasOffTime &&
                           !STOP && !TEMPALARM && !SMOKEALARM)
                    {
                        if (_resultsTimer.ElapsedMilliseconds > 1000)
                        {
                            SAVE = true;
                        }
                        foreach (var tt in tdk)
                        {
                            _serTDK.Write("MV?\r\n");
                            Wait(50); // lag in measured value
                            tt.Voltage = _serTDK.ReadLine();

                            _serTDK.Write("MC?\r\n");
                            Wait(50);
                            tt.Current = _serTDK.ReadLine();
                            _args      = new GUIArgs(tt.Voltage, tt.Current, tt.CycleCount, tt.Port, _cycleTimer);
                            NewCoreCommand?.Invoke(this, new CoreCommand()
                            {
                                Type = U.CmdType.UpdateUI
                            });
                            if (SAVE)
                            {
                                SaveResults(tt);
                            }
                        }

                        // if we have saved then restart timer
                        if (SAVE)
                        {
                            SAVE = false;
                            _resultsTimer.Restart();
                        }
                    }
                    if (STOP || SMOKEALARM || TEMPALARM)
                    {
                        break;
                    }

                    // completed a bias on/off cycle
                    foreach (var ttt in tdk)
                    {
                        ttt.CycleCount++;
                    }
                }
            }
            catch (Exception exc) {
                Console.WriteLine($"{exc}");
                TurnOffClose(tdk);
                STOP       = false;
                SMOKEALARM = false;
                TEMPALARM  = false;
            }
            STOP       = false;
            SMOKEALARM = false;
            TEMPALARM  = false;
            TurnOffClose(tdk);
        }