Exemple #1
0
        public static string GetOneCom()
        {
            if (SerialPortTran.IsTheDevice(FirstCom))
            {
                return(FirstCom);
            }

            {
                string[] sValues = SerialPort.GetPortNames(); // keyCom.GetValueNames();
                foreach (string sValue in sValues)
                {
                    if (sValue != FirstCom)
                    {
                        try
                        {
                            bool b = SerialPortTran.IsTheDevice(sValue);
                            if (b)
                            {
                                FirstCom = sValue;
                                return(sValue);
                            }
                        }
                        catch (Exception ex) {
                            _tracing.Error(ex, "failed to try connect and send test ." + sValue);
                        }
                    }
                }
            }
            return("");
        }
Exemple #2
0
        public static Dictionary <string, string> GetComList()
        {
            Dictionary <string, string> result = new Dictionary <string, string>();

            RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");

            if (keyCom != null)
            {
                string[] sSubKeys = keyCom.GetValueNames();
                foreach (string sName in sSubKeys)
                {
                    string sValue = (string)keyCom.GetValue(sName);
                    try {
                        bool b = SerialPortTran.IsTheDevice(sValue);
                        if (b)
                        {
                            result.Add(sValue, sName);
                        }
                    }
                    catch (Exception ex)
                    {
                        _tracing.Error(ex, "failed to GetComList ." + sValue);
                    }
                }
            }
            return(result);
        }
Exemple #3
0
        public static bool IsTheDevice(string port)
        {
            SerialPortTran spt;

            try
            {
                SerialPortFixer.Execute(port);
                spt = new SerialPortTran(port, false);
                spt.SPWrite(Utils.HexToByte("681600000A"));
                string result = "";
                Thread.Sleep(100);
                result = spt.spReadOnce();
                spt.sp.Close();

                if (result.StartsWith("6817"))
                {
                    return(true);
                }
            }
            catch
            {
                return(false);
            }
            finally
            {
            }

            return(false);
        }
Exemple #4
0
        private static Dictionary <string, string> test2()
        {
            Dictionary <string, string> result = new Dictionary <string, string>();

            string[] spname = SerialPort.GetPortNames();



            if (spname.Length > 0)
            {
                foreach (string spName in spname)
                {
                    result.Add(spName, spName);//得到所有串口号
                    try
                    {
                        bool b = SerialPortTran.IsTheDevice(spName);
                    }
                    catch { }
                }
            }
            return(result);
        }