C#——串口可用性检查

        /// <summary>
        /// 检查当前串口是否可用
        /// </summary>
        /// <param name="strSerialNo">串口号</param>
        /// <returns></returns>
        public bool CheckPort(string strSerialNo)
        {
            try
            {   
                _sp = new SerialPort(strSerialNo, 9600, Parity.None, 8, StopBits.One);
                _sp.Open();
                if (_sp != null)
                {
                    _sp.Close();
                    _sp = null;
                }
                return true;
            }
            catch
            {
                MessageBox.Show("当前串口已被占用,请切换串口", "警告提示");
                return false;
            }
        }

猜你喜欢

转载自blog.csdn.net/qq_20799821/article/details/108520134