private bool StateMachine_ConnectionCheck(StepDefinition currentStep)
        {
            // it has been long enough, we need to test the connection
            if (CurrentConnection?.SendConnectionTest() == true)
            {
                if (IsConnected)
                {
                    return(StepReturn.RepeatStep);
                }

                IsConnected = true;
                RunConnectionUpdate();
            }
            else
            {
                if (IsConnected)
                {
                    IsConnected = false;
                    RunConnectionUpdate();
                }

                if (TargetMode != ConnectionMode.PersistentPort)
                {
                    currentStep.SkipDelayTime = true;
                    MachineFunctions.JumpToFirst(currentStep);
                }
                else
                {
                    // see if the port is still listed in the registry
                    if (ComPortInfo.GetDescriptions().FirstOrDefault(x => x.Port == PortName) != null)
                    {
                        return(StepReturn.RepeatStep);
                    }

                    // if the port is no longer there, we know the port was removed from the system
                    currentStep.SkipDelayTime = true;
                    MachineFunctions.JumpToFirst(currentStep);
                }

                return(StepReturn.JumpCommandUsed);
            }

            // this step should never move on automatically
            return(StepReturn.RepeatStep);
        }
        private bool StateMachine_Searching(StepDefinition currentStep)
        {
            // get list of current system com ports
            var descriptions = ComPortInfo.GetDescriptions();

            // clear out any old connections
            if (IsOpen)
            {
                IsConnected = false;
                CurrentConnection?.Disconnect();
            }

            RunConnectionUpdate();

            switch (TargetMode)
            {
            case ConnectionMode.AnyCom:
                CurrentConnection = AutoConnectComPort(
                    descriptions.Select(s => s.Port).ToList(),
                    ConnectionParameters);
                break;

            case ConnectionMode.SelectionRule:
                CurrentConnection = AutoConnectComPort(
                    descriptions.Where(TargetSelectionRule).Select(s => s.Port).ToList(),
                    ConnectionParameters);
                break;

            case ConnectionMode.PersistentPort:
                descriptions = descriptions.Where(TargetSelectionRule).ToList();
                if (descriptions.Count == 1)
                {
                    CurrentConnection = ConnectComPort(
                        descriptions.Select(s => s.Port).First(),
                        ConnectionParameters);

                    CurrentConnection?.Connect();
                    if (CurrentConnection?.IsConnected != true)
                    {
                        CurrentConnection?.Disconnect();
                        CurrentConnection = null;
                    }
                }
                else if (descriptions.Count > 1)
                {
                    MachineFunctions.JumpToLast(currentStep);
                    return(StepReturn.JumpCommandUsed);
                }
                break;

            default:
                break;
            }

            // if we didn't open a port, try again
            if (!IsOpen)
            {
                return(StepReturn.RepeatStep);
            }

            // we opened a port, so update the listeners
            ConnectionEventRegister();
            RunConnectionUpdate();

            // move to the next step
            return(StepReturn.ContinueToNext);
        }