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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets a list of COMPortInfo objects. This contains a port name and description of every com port in the system, including ones in use.
        /// </summary>
        /// <returns></returns>
        public static List <ComPortInfo> GetDescriptions()
        {
            var comPortInfoList = new List <ComPortInfo>();

            var options         = ProcessConnection.ProcessConnectionOptions();
            var connectionScope = ProcessConnection.ConnectionScope(Environment.MachineName, options, @"\root\CIMV2");

            var objectQuery     = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"");
            var comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery);

            using (comPortSearcher)
            {
                foreach (var obj in comPortSearcher.Get())
                {
                    if (obj["Caption"]?.ToString().Contains("COM") != true)
                    {
                        continue;
                    }

                    var comPortInfo = new ComPortInfo
                    {
                        Manufacturer = obj["Manufacturer"]?.ToString(),
                        Caption      = obj["Caption"]?.ToString(),
                        DeviceId     = obj["DeviceID"]?.ToString(),
                        Description  = obj["Description"]?.ToString(),
                        Name         = obj["Name"]?.ToString()
                    };

                    comPortInfo.Caption = obj["Caption"]?.ToString();

                    var tempString = comPortInfo.Caption.Substring(comPortInfo.Caption.IndexOf("(", StringComparison.Ordinal) + 1);
                    tempString       = tempString.Substring(0, tempString.IndexOf(")", StringComparison.Ordinal));
                    comPortInfo.Port = tempString;

                    if (comPortInfo.DeviceId?.Contains("VID") == true)
                    {
                        comPortInfo.Vid = comPortInfo.DeviceId.Substring(comPortInfo.DeviceId.IndexOf("VID", StringComparison.Ordinal) + 4, 4);
                    }

                    if (comPortInfo.DeviceId?.Contains("PID") == true)
                    {
                        comPortInfo.Pid = comPortInfo.DeviceId.Substring(comPortInfo.DeviceId.IndexOf("PID", StringComparison.Ordinal) + 4, 4);
                    }

                    if (comPortInfo.Manufacturer?.Contains("FTDI") == true)
                    {
                        tempString = comPortInfo.DeviceId?.Substring(comPortInfo.DeviceId.IndexOf("PID", StringComparison.Ordinal) + 9);
                        if (tempString != null)
                        {
                            var tempInt = tempString.IndexOf("\\", StringComparison.Ordinal);
                            if (tempInt > 0)
                            {
                                tempString         = tempString.Substring(0, tempInt);
                                comPortInfo.Serial = tempString;
                            }
                        }
                    }

                    comPortInfoList.Add(comPortInfo);
                }
            }

            return(comPortInfoList.OrderBy(x => x.Description).ToList());
        }
        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);
        }