示例#1
0
        private static bool TryDetectDevicePort(out string devicePortName)
        {
            string[] portNames = SerialPort.GetPortNames();
            foreach (string portName in portNames)
            {
                string version;
                using (SerialPort port = OpenPort(portName))
                {
                    try
                    {
                        port.Open();
                    }
                    catch
                    {
                        continue;
                    }

                    using (GmcReader reader = new GmcReader(port))
                    {
                        using (GmcWriter writer = new GmcWriter(port))
                        {
                            writer.WriteVersionRequest();

                            try
                            {
                                version = reader.ReadVersion();
                            }
                            catch (DeviceUnresponsiveException)
                            {
                                continue;
                            }
                        }
                    }

                    port.Close();
                }

                if (version.StartsWith("GMC"))
                {
                    devicePortName = portName;
                    return true;
                }
            }

            devicePortName = null;
            return false;
        }