Ejemplo n.º 1
0
        /// <summary>
        /// 获取串口设备的名称
        /// </summary>
        private bool GetSerialPortName()
        {
            bool _return = false;

            //---获取当前设备的端口
            string[] argNames = SerialPort.GetPortNames();
            string[] argInfo  = SystemHardware.GetSerialPort();
            if ((argNames == null) || (argNames.Length == 0))
            {
                if (this.defaSerialIndexMemu != null)
                {
                    this.defaSerialIndexMemu.Clear();
                }
                if (this.defaultSerialInfoMemu != null)
                {
                    this.defaultSerialInfoMemu.Clear();
                }
            }
            else
            {
                this.defaSerialIndexMemu = this.GetSerialPortIndex(argNames);
                if ((this.defaSerialIndexMemu != null) && (this.defaSerialIndexMemu.Count > 0))
                {
                    this.defaultSerialInfoMemu = this.GetSerialPortInfo(argInfo);
                }
                _return = true;
            }
            return(_return);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取系统所有的串行设备
        /// </summary>
        /// <returns></returns>
        public static string[] GetSerialPort()
        {
            List <string> spList = new List <string>();
            Regex         reg    = new Regex(@"COM\d+");

            string[] strArr = SystemHardware.GetHarewareInfo(HardwareEnum.Win32_PnPEntity, "Name");
            foreach (string s in strArr)
            {
                if (reg.IsMatch(s))
                {
                    spList.Add(s);
                }
            }

            return(spList.ToArray());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 设备插入
        /// </summary>
        /// <param name="cbb"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override int InsertDevice()
        {
            int _return   = 0;
            int portIndex = -1;
            //---获取当前设备存在的通信端口
            List <byte> addNames = this.GetSerialPortIndex(SerialPort.GetPortNames());

            //---检查设备端口
            if ((addNames == null) || (addNames.Count == 0))
            {
                if (this.defaSerialIndexMemu != null)
                {
                    this.defaSerialIndexMemu.Clear();
                }
                if (this.defaultSerialInfoMemu != null)
                {
                    this.defaultSerialInfoMemu.Clear();
                }
                if (this.mCCommComBox != null)
                {
                    //---异步调用
                    if (this.mCCommComBox.InvokeRequired)
                    {
                        this.mCCommComBox.BeginInvoke((EventHandler)
                                                      //cbb.Invoke((EventHandler)
                                                      (delegate
                        {
                            this.mCCommComBox.Items.Clear();
                            this.mCCommComBox.SelectedIndex = -1;
                        }));
                    }
                    else
                    {
                        this.mCCommComBox.Items.Clear();
                        this.mCCommComBox.SelectedIndex = -1;
                    }
                }
                _return = 1;
                this.defaultSerialMsg = "获取设备失败,请插入设备!\r\n";
            }
            else
            {
                int i = 0;
                this.defaultSerialMsg = "";
                //---遍历是哪个设备插入
                for (i = 0; i < addNames.Count; i++)
                {
                    if ((this.defaSerialIndexMemu != null) && (this.defaSerialIndexMemu.Count > 0))
                    {
                        //---查询是哪个设备插入
                        portIndex = this.defaSerialIndexMemu.IndexOf(addNames[i]);
                    }
                    //---UI显示插入的设备
                    if (portIndex < 0)
                    {
                        this.defaultSerialMsg += "COM" + addNames[i].ToString() + "设备插入!\r\n";
                    }
                }

                portIndex = -1;

                List <byte> addDevice = new List <byte>();

                //---获取当前选择的端口
                if (this.mCCommComBox != null)
                {
                    //---异步调用
                    if (this.mCCommComBox.InvokeRequired)
                    {
                        this.mCCommComBox.Invoke((EventHandler)
                                                 (delegate
                        {
                            portIndex = this.mCCommComBox.SelectedIndex;
                        }));
                    }
                    else
                    {
                        portIndex = this.mCCommComBox.SelectedIndex;
                    }
                }

                if ((this.defaSerialIndexMemu.Count != 0) && (portIndex >= 0))
                {
                    portIndex = this.defaSerialIndexMemu[portIndex];
                }

                this.defaSerialIndexMemu = new List <byte>();
                this.defaSerialIndexMemu.AddRange(addNames.ToArray());

                if (this.defaSerialIndexMemu.Count != 0)
                {
                    if (portIndex < 0)
                    {
                        portIndex = 0;
                    }
                    else
                    {
                        portIndex = this.defaSerialIndexMemu.IndexOf((byte)portIndex);
                    }
                }

                if (this.mCCommComBox != null)
                {
                    //---异步调用
                    if (this.mCCommComBox.InvokeRequired)
                    {
                        this.mCCommComBox.BeginInvoke((EventHandler)
                                                      //cbb.Invoke((EventHandler)
                                                      (delegate
                        {
                            this.mCCommComBox.Items.Clear();
                            for (i = 0; i < this.defaSerialIndexMemu.Count; i++)
                            {
                                this.mCCommComBox.Items.Add("COM" + this.defaSerialIndexMemu[i].ToString());
                            }
                            this.mCCommComBox.SelectedIndex = portIndex;
                        }));
                    }
                    else
                    {
                        this.mCCommComBox.Items.Clear();
                        for (i = 0; i < this.defaSerialIndexMemu.Count; i++)
                        {
                            this.mCCommComBox.Items.Add("COM" + this.defaSerialIndexMemu[i].ToString());
                        }
                        this.mCCommComBox.SelectedIndex = portIndex;
                    }
                }
            }
            //---获取驱动信息
            this.defaultSerialInfoMemu = this.GetSerialPortInfo(SystemHardware.GetSerialPort());
            if ((this.defaultSerialInfoMemu != null) && (this.defaultSerialInfoMemu.Count > 0) && (this.defaultSerialInfoMemu.Count > portIndex))
            {
                this.defaultSerialInfo = this.defaultSerialInfoMemu[portIndex];
            }

            if (this.mCCommRichTextBox != null)
            {
                CRichTextBoxPlus.AppendTextInfoTopWithDataTime(this.mCCommRichTextBox, this.defaultSerialMsg, (_return == 0?Color.Black:Color.Red), false);
            }
            return(_return);
        }