//private void UpdateCycleCounter_test()
        //{
        //    if (DataSample.FeedbackAngle1 >= 180 && DataSample.FeedbackAngle1 <= 360)
        //        DataSample.ChartResetFlag = true;
        //    else if (DataSample.FeedbackAngle1 > 0 && DataSample.FeedbackAngle1 < 180)
        //    {
        //        if (DataSample.ChartResetFlag == true)
        //        {
        //            DataSample.ChartResetFlag = false;
        //            DataSample.CycleCounter++;
        //            if (DataSample.CycleCounter == 3)
        //            {
        //                this.Invoke(this.ThreadCallbackUpdateUI, new Object[] { });
        //                DataSample.CycleCounter = 0;
        //            }

        //            DataSample.spinShaperData.List_PointCollection.Add(DataSample.spinShaperData.PointCollection);
        //            DataSample.spinShaperData.CalculateTestPeformanceParameters();
        //            DataSample.spinShaperData.PointCollection.Clear();
        //        }
        //    }
        //}

        private void OnSerialReadComplete(object sender, SerialReadCompleteEventArgs args)
        {
            if (chart1.InvokeRequired)
            {
                chart1.BeginInvoke(new MethodInvoker(() => OnSerialReadComplete(sender, args)));
                return;
            }

            Plotter(args.Torque, args.Angle);
            Plotter2(args.Torque, args.Angle);
        }
        public objServoResult ReadFeedBack_OnDataReceived()
        {
            GlobalVariables.SerialPortBusyLeft = true;
            while (GlobalVariables.leftSerialPort.BytesToRead != 0)
            {
                if (SerialReadInProgress == false)
                {
                    if (CurrentByte != 0x06)
                    {
                        if (GlobalVariables.leftSerialPort.BytesToRead != 0)
                        {
                            CurrentByte = (byte)GlobalVariables.leftSerialPort.ReadByte();
                            Result.IsSuccess = true;
                            continue;
                        }
                        else TimeOutCounter++;

                        if (TimeOutCounter >= 30000)
                        {
                            Result.ErrorCode = 3;
                            Result.IsSuccess = false;
                            GlobalVariables.SerialPortBusyLeft = false;
                            return Result;
                        }
                    }
                    else
                    {
                        SerialReadInProgress = true;
                        Result.IsSuccess = true;
                    }
                }
                else
                {
                    CurrentByte = (byte)GlobalVariables.leftSerialPort.ReadByte();
                    ByteStream.WriteByte(CurrentByte);
                    if (ByteStream.Length == 72)
                    {
                        Result = SerialMotorControl.ReadFeedback_OnDataReceived(ByteStream, FeedbackParams, ReadSuccess, currentByte1, streamArray, currentSpeed, currentPulse, currentTorque, currentContactStatus, ref ErrorCode, TimeOutCounter, ServoResult, sb, ByteString);
                        SerialReadInProgress = false;
                        ByteStream = new MemoryStream();

                        if ((new System.Collections.BitArray(new byte[] { currentContactStatus[7] }))[2] == false)
                            eStopCounter++;
                        else eStopCounter = 0;

                        if (Result.IsSuccess == false)
                        {

                        }
                        else if (eStopCounter >= 2)     //The status of SPD2 is given by bit 3 of byte 8 of the contact statuc byte array
                        {
                            Result.IsSuccess = false;
                            Result.ErrorCode = 5;
                            Result.ErrorMessage = "Emergency Stop Activated!";
                            eStopCounter = 0;
                        }
                        else
                        {
                            FeedbackTorque = (FeedbackParams[2] / 100 / 100) * CalibrationSettings.Default.RatedMotorTorque * CalibrationSettings.Default.GearRatio;
                            if (FeedbackTorque >= 1000)
                                FeedbackTorque = previousTorque;
                            previousTorque = FeedbackTorque;

                            FeedbackSpeed = FeedbackParams[0] / (CalibrationSettings.Default.GearRatio * 10);
                            FeedbackPower = Math.Abs(FeedbackTorque * FeedbackSpeed * 2 * Math.PI / 60);
                            FeedbackPulse = FeedbackParams[1];      //Feedback Pulse Position in Current Cycle
                            FeedbackPulseAbs = FeedbackParams[4];   //Absolute Feedback Pulse Position
                            FeedbackContactStatus = FeedbackParams[3];
                            FeedbackAngle1 = CalculateAngle();

                            DataSampleCalculations();

                            //Clear the buffer if it starts to fill up
                            if (GlobalVariables.leftSerialPort.BytesToRead > 219)
                            {
                                if (BufferOverFlowEvent != null)
                                {
                                    BufferOverFlowEventArgs args = new BufferOverFlowEventArgs(GlobalVariables.leftSerialPort.BytesToRead);
                                    BufferOverFlowEvent(this, args);
                                }
                                GlobalVariables.leftSerialPort.DiscardInBuffer();
                            }

                            if (SerialReadCompleteEvent != null)
                            {
                                SerialReadCompleteEventArgs args = new SerialReadCompleteEventArgs(FeedbackTorque, FeedbackAngle1);
                                SerialReadCompleteEvent(this, args);
                            }
                        }

                        GlobalVariables.SerialPortBusyLeft = false;
                        return Result;
                    }
                    else
                    {
                        Result.IsSuccess = true;
                    }
                }
            }

            GlobalVariables.SerialPortBusyLeft = false;
            return Result;
        }