Exemplo n.º 1
0
        /// <summary>
        /// Connect to the specific serial port of the stepper. If no
        /// availale serial port exists, nothing will happen.
        /// </summary>
        /// <param name="index">Index of the serial port to connect to.</param>
        private void _ConnectStepper(int index)
        {
            // Check if there is available serial port
            if (_SerialPortList == null || _SerialPortList.Length == 0)
            {
                return;
            }

            // Ensure the previous stepper stopped
            _DisconnectStepper();

            // Select the specific serial port
            try
            {
                _Stepper = new StepperController(_SerialPortList[index]);
            }
            catch (Exception e)
            {
                _ConsolePrintMessage("private void _ConnectStepper(int): " + e.Message, MessageLevel.Error);
                return;
            }

            // Set the baud rate
            _Stepper.BaudRate = _StepperBaudRate;

            // Append handlers to the events
            _Stepper.RotationStarted  -= _StepperRotationStartedEventHandler;
            _Stepper.RotationStarted  += _StepperRotationStartedEventHandler;
            _Stepper.RotationFinished -= _StepperRotationFinishedEventHandler;
            _Stepper.RotationFinished += _StepperRotationFinishedEventHandler;
            _Stepper.OperationFailed  -= _StepperOperationFailedEventHandler;
            _Stepper.OperationFailed  += _StepperOperationFailedEventHandler;

            // Connect to the stepper port
            _Stepper.Open();

            // CONSOLE: Connect stepper message
            _ConsolePrintMessage("Successfully connect to the stepper on " + _Stepper.PortName + ".", MessageLevel.Info);
        }