private void LASERA_laserReceivingData(LaserData ld)
 {
     this.Invoke(m_DelegateGotData, ld);
 }
        private void laserReceivingData(LaserData ld)
        {
            if (LaserAquireOpen)
            {
                try
                {
                    if (radFA.Checked)
                    {
                        _current.ForwardAz = ld._azimuth;
                    }
                    else
                    {
                        _current.BackwardAz = ld._azimuth;
                    }

                    _current.SlopeAngle = ld._inclination;
                    _current.SlopeDistance = TtUtils.ConvertDistance(ld._slope, _MetaData.uomDistance, TtUtils.UnitToUomDistance(ld._slope_unit));
                    
                    
                    SetTextBoxes();

                    if (ValuesSet != null)
                    {
                        ValuesSet();
                    }
                }
                catch (Exception ex)
                {
                    TtUtils.WriteError(ex.Message, "TravInfoCtrl:laserReceivingData", ex.StackTrace);
                }

                LaserAquireOpen = false;
            }
        }
        private bool ParseSentence(string sentence, ref LaserData toFill)
        {
            if (!IsValidSentence(sentence))
            {
                if (InvalidStringFound != null && !_cancelled)
                {
                    InvalidStringFound();
                }
                return false;
            }

            return ParseLaserData(sentence, ref toFill);
        }
        private bool ParseLaserData(string sentence, ref LaserData toFill)
        {
            string[] tokens = sentence.Substring(0, sentence.Length - 3).Split(',');

            LaserData ld = new LaserData();

            try
            {
                if (tokens[1] != String.Empty)
                    ld._horizontal_vector_message = tokens[1];
                if (tokens[2] != String.Empty)
                    ld._horizontal_dist = Convert.ToDouble(tokens[2]);
                if (tokens[3] != String.Empty)
                    ld._horizontal_dist_unit = TtUtils.ConvertUnit(tokens[3]); //(Unit)Enum.Parse(typeof(Unit), tokens[3], true);
                if (tokens[4] != String.Empty)
                    ld._azimuth = Convert.ToDouble(tokens[4]);
                if (tokens[6] != String.Empty)
                    ld._inclination = Convert.ToDouble(tokens[6]);
                if (tokens[8] != String.Empty)
                    ld._slope = Convert.ToDouble(tokens[8]);
                if (tokens[9] != String.Empty)
                    ld._slope_unit = TtUtils.ConvertUnit(tokens[9]); //(Unit)Enum.Parse(typeof(Unit), tokens[9], true);

                ld.goodData = true;
                toFill = ld;
            }
            catch
            {
                return false;
            }

            return true;
        }
        //Laser Connection and Read
        private void LaserWorker(object o)
        {
            try
            {
                _port = new SerialPort(Port, Rate);
                _port.Parity = Parity.None;
                _port.StopBits = StopBits.One;
                _port.DataBits = 8;
                _port.Handshake = Handshake.None;
                _port.ReadTimeout = Values.Settings.DeviceOptions.LaserTimeout;

                //if (!TtUtils.SerialPortTester.SerialPortTest(Port))
                //    throw new TimeoutException("SerialPortTest Failed");

                _port.Open();

                #region Trigger Event
                //Trigger Event
                if (LaserStarted != null)
                {
                    LaserStarted();
                }
                #endregion

                while (true)
                {
                    if (_cancelled)
                    {
                        CloseLaserPort();
                        #region Trigger Event
                        //Trigger Event
                        if (LaserEnded != null)
                        {
                            LaserEnded();
                        }
                        #endregion
                        return;
                    }

                    string sentence = _port.ReadLine();

                    sentence = sentence.Trim();

                    LaserData ld = new LaserData();
                    if (ParseSentence(sentence, ref ld))
                    {
                        #region Trigger BurstReceived Event
                        if (DataReceived != null && ld != null && !_cancelled && ld.goodData)
                        {
                            DataReceived(ld);
                            //_cancelled = true;
                        }
                        #endregion
                    }
                }
            }
            #region Exceptions
            catch (TimeoutException toEx)
            {
                TtUtils.WriteError(toEx.Message, "LaserAccess:LaserWorker-toEx", toEx.StackTrace);

                #region trigger Error event
                Error = LaserErrorType.LaserTimeout;
                if (LaserError != null)
                    LaserError();
                #endregion
                CloseLaserPort();
            }
            catch (System.IO.IOException ioEx)
            {
                TtUtils.WriteError(ioEx.Message, "LaserAccess:LaserWorker-ioEx", ioEx.StackTrace);

                #region trigger Error event
                Error = LaserErrorType.ComNotExist;
                if (LaserError != null)
                    LaserError();
                #endregion
                CloseLaserPort();
            }
            catch (Exception ex)
            {
                TtUtils.WriteError(ex.Message, "LaserAccess:LaserWorker-ex", ex.StackTrace);

                #region trigger Error event
                Error = LaserErrorType.UnknownError;
                if (LaserError != null)
                    LaserError();
                #endregion
                CloseLaserPort();
            }
            #endregion
        }
        private void LaserA_DataReceived(LaserData data)
        {
            this.GuiInvoke(() =>
                {
                    txtLaserOutput.Text = "Laser received- " + data.ToString();
                    System.Threading.Thread.Sleep(1000);
                    txtLaserOutput.Text = "Laser Successful";
                    CloseLaser();
                });

            AutoClosingMessageBox.Show("Laser Configured", "Laser Configuration", 1000);
        }