Пример #1
0
 private void ProcessEventMessage(Message msg)
 {
     if (_currentChannel.Id == msg.Channel)
     {
         SlackMessage smsg = new SlackMessage();
         smsg.LoadMessage(this, msg);
         _currentMessages.Add(smsg);
         OnMessagesUpdated?.Invoke(msg);
     }
 }
Пример #2
0
        /// <summary>
        /// Connect instruments defined in the config file
        /// </summary>
        /// <returns></returns>
        public async Task ConnectInstruments()
        {
            IProgress <string> ProgressMessageUpdate = new Progress <string>(msg =>
            {
                OnMessagesUpdated?.Invoke(this, msg);
            });

            await Task.Run(() =>
            {
                List <Exception> exceptions = new List <Exception>();

                ProgressMessageUpdate.Report("Connecting KEYSIGHT 8164B ...");

                // connect keysight 8164B
#if !FAKE_ME
                try
                {
                    if (TunableLaser.GetDescription().Contains("8164B"))
                    {
                        ProgressMessageUpdate.Report($"{TunableLaser} was found.");
                    }
                }
                catch (Exception ex)
                {
                    ProgressMessageUpdate.Report($"Unabel to find {TunableLaser}, {ex.Message}");
                    exceptions.Add(ex);
                }
#else
                ProgressMessageUpdate.Report($"{TunableLaser} was found.");
#endif
                int i = 1;
                foreach (var sm in this.SourceMeter)
                {
#if !FAKE_ME
                    // connect keithley 2400
                    try
                    {
                        ProgressMessageUpdate.Report("Connecting KEITHLEY 2400 ...");

                        if (sm.GetDescription().Contains("2400"))
                        {
                            ProgressMessageUpdate.Report($"{sm} was found.");
                            sm.SetDisplayTextState(1, true);
                            sm.SetDisplayTextMessage(1, $"PLC CH {i}");
                        }
                    }
                    catch (Exception ex)
                    {
                        ProgressMessageUpdate.Report($"Unabel to find {sm}, {ex.Message}");
                        exceptions.Add(ex);
                    }

                    i++;
#else
                    ProgressMessageUpdate.Report($"{sm} was found.");
#endif
                }

                if (exceptions.Count > 0)
                {
                    throw new AggregateException(exceptions);
                }
            });
        }
Пример #3
0
        /// <summary>
        /// Start to sweep the specified channel asynchronously
        /// </summary>
        /// <param name="Channel"></param>
        /// <param name="DataReadCallback"></param>
        /// <returns></returns>
        private async Task SweepAsync(Action <double, List <double> > DataReadCallback)
        {
            DateTime startTime = DateTime.Now;

            try
            {
                OnMessagesUpdated?.Invoke(this, $"Start to sweep from {Config.SweepStart}nm to {Config.SweepEnd}nm with step {Config.SweepStep}nm ...");

                IProgress <double> progressChanged = new Progress <double>(prog =>
                {
                    OnSweepingProgressChanged?.Invoke(this, prog);
                });

                IProgress <string> ProgressMessageUpdate = new Progress <string>(msg =>
                {
                    OnMessagesUpdated?.Invoke(this, msg);
                });

                // reset the cancellation token source
                ctsSweep = new CancellationTokenSource();



                await Task.Run(() =>
                {
                    // set the optical power
                    TunableLaser.SetPower(this.Config.OpticalPower);

                    // turn on the laser
                    TunableLaser.SetOutput(true);
                    if (TunableLaser.GetOutput() == false)
                    {
                        throw new Exception("Unable to turn on the laser.");
                    }


                    for (int i = 0; i < Config.MaxChannel; i++)
                    {
                        SourceMeter[i].SetDataElement(Keithley2400.EnumDataStringElements.CURR);
                        SourceMeter[i].SetMeasurementFunc(Keithley2400.EnumMeasFunc.ONCURR);
                        SourceMeter[i].SetSourceMode(Keithley2400.EnumSourceMode.VOLT);
                        SourceMeter[i].SetComplianceCurrent(Keithley2400.EnumComplianceLIMIT.REAL, 0.01);
                        // SourceMeter.SetMeasRangeOfAmps(Keithley2400.EnumMeasRangeAmps.R1MA);
                        SourceMeter[i].SetMeasRangeOfAmps(Keithley2400.EnumMeasRangeAmps.R1MA);
                        SourceMeter[i].SetVoltageSourceLevel(0);
                        SourceMeter[i].SetOutputState(true);
                        Thread.Sleep(100);
                        SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR);
                        Thread.Sleep(100);
                        SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR);
                        Thread.Sleep(100);
                        SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR);
                        Thread.Sleep(100);
                        SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR);
                        Thread.Sleep(100);
                        SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR);
                    }


                    for (var lambda = Config.SweepStart; lambda <= Config.SweepEnd; lambda += Config.SweepStep)
                    {
                        lambda = Math.Round(lambda, 3);

                        //TODO Add data fetching process here
                        TunableLaser.SetWavelenght(lambda);

                        Thread.Sleep(30);


                        List <double> intensityList = new List <double>();

                        for (int i = 0; i < Config.MaxChannel; i++)
                        {
                            var intensity = SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR).Current;
                            intensity    *= 1000; // convert A to mA
                            intensityList.Add(Math.Abs(intensity));
                        }

                        // add the point to collection on UI thread
                        System.Windows.Application.Current.Dispatcher.Invoke(() =>
                        {
                            DataReadCallback.Invoke(lambda, intensityList);
                        });

                        // calculate the progress in percent
                        var prog = (lambda - Config.SweepStart) / (Config.SweepEnd - Config.SweepStart);
                        progressChanged.Report(prog);


                        // check if the task should be canceled
                        if (ctsSweep.Token.IsCancellationRequested)
                        {
                            ProgressMessageUpdate.Report("The sweeping process was canceled.");
                            throw new OperationCanceledException("The sweeping process was canceled by user.");
                        }
                    }
                });
            }
            catch (AggregateException ae)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var ex in ae.Flatten().InnerExceptions)
                {
                    sb.Append(ex.Message);
                    sb.Append("\r\n");
                }

                throw new Exception(sb.ToString());
            }
            finally
            {
#if !FAKE_ME
                // turn off the laser
                TunableLaser.SetOutput(false);

                for (int i = 0; i < Config.MaxChannel; i++)
                {
                    // turn off 2400
                    SourceMeter[i].SetOutputState(false);
                }
#endif

                OnMessagesUpdated?.Invoke(this, $"The sweeping process costs {(DateTime.Now - startTime).TotalSeconds}s");
            }
        }
Пример #4
0
        public async Task FindInstruments()
        {
            IProgress <string> ProgressMessageUpdate = new Progress <string>(msg =>
            {
                OnMessagesUpdated?.Invoke(this, msg);
            });

            await Task.Run(() =>
            {
                ProgressMessageUpdate.Report("Start to finding instruments ...");

                Board gpibBoard = new Board(Config.GPIBBoardNumber);

                // get all addresses
                gpibBoard.SendInterfaceClear();
                var addresses = gpibBoard.FindListeners();
                gpibBoard.SetRemoteWithLockout(addresses);

                ProgressMessageUpdate.Report($"{addresses.Count} instruments were found.");
                ProgressMessageUpdate.Report("Checking model of the instruments ...");

                // query device description from each address
                foreach (Address address in addresses)
                {
                    ProgressMessageUpdate.Report($"Querying the description of the instrument at {address} ...");

                    var dev = new Device(Config.GPIBBoardNumber, address.PrimaryAddress, address.SecondaryAddress);
                    dev.Write("*IDN?");
                    var ret = dev.ReadString();

                    ProgressMessageUpdate.Report($"{ret.TrimEnd('\n')}");

                    if (ret.Contains("8164B"))
                    {
                        this.TunableLaser = new Keysight8164B(Config.GPIBBoardNumber, address);
                        ProgressMessageUpdate.Report($"Keysight 8164B was found at {address}.");
                    }
                    else if (ret.Contains("2400"))
                    {
                        //this.SourceMeter = new Keithley2400(Config.GPIBBoardNumber, address);
                        ProgressMessageUpdate.Report($"Keithley 2400 was found at {address}.");
                    }
                }

#if FAKE_ME
                this.TunableLaser = new Keysight8164B(0, new Address(1));
                ProgressMessageUpdate.Report($"[DEBUG] Keysight 8164B was added.");
#endif

                List <InvalidOperationException> exceptions = new List <InvalidOperationException>();

                // check if all instruments were found
                if (this.SourceMeter == null)
                {
                    exceptions.Add(new InvalidOperationException("Unable to find Keithley 2400."));
                }
                else if (this.TunableLaser == null)
                {
                    exceptions.Add(new InvalidOperationException("Unable to find Keysight 81645B."));
                }

                if (exceptions.Count > 0)
                {
                    throw new AggregateException(exceptions);
                }
            });
        }