c#检测串口是否被占用,c#检测端口是否被占用,一台电脑最多可以创建多少个串口

检测端口是否被占用_1

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows.Forms;


    /// <summary>
        /// 判断指定端口号是否被占用
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        internal static Boolean IsPortOccupedFun2(Int32 port)
        {
            Boolean result = false;
            try
            {
                System.Net.NetworkInformation.IPGlobalProperties iproperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
                System.Net.IPEndPoint[] ipEndPoints = iproperties.GetActiveTcpListeners();
                foreach (var item in ipEndPoints)
                {
                    if (item.Port == port)
                    {
                        result = true;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }

检测端口是否被占用_2

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows.Forms;


       /// <summary>
        /// 判断端口是否被占用
        /// </summary>
        /// <param name="port"></param>
        /// <returns>true 占用,false未被占用</returns>
        internal static Boolean IsPortOccupedFun4(int port)
        {
            bool flag = false;
            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] ipendpoints = null;
            ipendpoints = properties.GetActiveTcpListeners();
            foreach (IPEndPoint ipendpoint in ipendpoints)
            {
                if (ipendpoint.Port == port)
                {
                    flag = true;
                    break;
                }
            }
            ipendpoints = null;
            properties = null;
            return flag;
        }

检测端口是否被占用_3

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows.Forms;

        /// <summary>
        /// 判断指定端口是否被占用
        /// </summary>
        /// <param name="port">指定端口</param>
        /// <returns></returns>
        internal static Boolean IsPortOccupedFun3(Int32 port)
        {
            System.Net.NetworkInformation.IPGlobalProperties iproperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
            System.Net.IPEndPoint[] ipEndPoints = iproperties.GetActiveTcpListeners();
            foreach (var item in ipEndPoints)
            {
                if (item.Port == port)
                {
                    return true;
                }
            }
            return false;
        }

判断端口是否被占用,如果占用返回一个新的端口

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows.Forms;


        /// <summary>
        /// 如果端口被占用就返回一个新的端口
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        internal static int UserThisPort(int port)
        {
            bool portState = true;
            int flag = 0;
            if (IsPortOccupedFun3(port))//占用
            {
                while (portState)
                {
                    if (flag < 20000)
                    {
                        port = port + 1;
                        flag = flag + 1;
                        if (IsPortOccupedFun3(port))
                        {
                            portState = true;
                        }
                        else
                        {
                            portState = false;
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有找到端口", "提示", MessageBoxButtons.OK);
                    }
                }
                return port;
            }
            else
            {
                return port;
            }
        }

判断指定串口是否被占用,如果占用返回一个新的串口

使用string COMNO= CheckCOM(501,20);//501需要判断的串口,如果占用尝试使用新的串口的次数(向后顺延多少个串口),


      #region 串口是否被占用

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="portName">COM</param>
        /// <returns>true打开成功</returns>
        private Boolean OpenSerialPort(int portNO)
        {
            string portName = "COM" + portNO.ToString();
            return this.mOpenSerialPort(portName, 0x1c200, 8, StopBits.One, Parity.None);
        }

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="portName"></param>
        /// <param name="BaudRate"></param>
        /// <param name="DataBits"></param>
        /// <param name="StopBits"></param>
        /// <param name="Parity"></param>
        /// <returns></returns>
        private Boolean mOpenSerialPort(string portName, int BaudRate, int DataBits, StopBits StopBits, Parity Parity)
        {
            try
            {
                this.serialport = new SerialPort(portName);
                this.serialport.BaudRate = BaudRate;
                this.serialport.DataBits = DataBits;
                this.serialport.StopBits = StopBits;
                this.serialport.Parity = Parity;
                this.serialport.NewLine = "\r\n";
                this.serialport.ReadTimeout = 0xFE0;
                this.serialport.Open();
                if (this.serialport.IsOpen)//获取串口状态但是不打开
                {
                    //MessageBox.Show("串口打开成功", "提示", MessageBoxButtons.OK);
                    return true;
                }
                //MessageBox.Show("串口打开失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;//失败
            }
            catch
            {
                //MessageBox.Show("串口打开失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;//发生了错误
            }
        }


        /// <summary>
        /// 从comPort_int(包括)开始获取一个串口,如果comPort_int被占用,则向后尝试最多testTimes次来获取新的未被占用串口
        /// </summary>
        /// <param name="comPort_int">起始串口</param>
        /// <param name="testTimes">最大尝试次数</param>
        /// <returns></returns>
        private  string CheckCOM(int comPort_int, int testTimes = 30)
        {
            bool COMConnectionState = true;//串口打开状态 
            int flag = 1;//当前尝试次数

            if (OpenSerialPort(comPort_int))//True开启端口没有被占用
            {
                return comPort_int.ToString();

            }
            else
            {
                while (COMConnectionState)
                {
                    if (flag <= testTimes)
                    {
                        comPort_int = comPort_int + 1;
                        flag = flag + 1;
                        if (OpenSerialPort(comPort_int))
                        {
                            COMConnectionState = false;
                        }
                        else
                        {
                            COMConnectionState = true;
                        }
                    }
                    else
                    {
                        return "没有在最大尝试次数内没有获取到串口号";
                    }
                }
                return comPort_int.ToString();
            }

        }




        #endregion


不懂评论私信,Enjoy!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值