110,536
社区成员
发帖
与我相关
我的任务
分享
public bool IsCommPortValid(int port)
{
try
{
SerialPort sp = new SerialPort("COM"+port.ToString());
sp.Close();
}
catch
{
return false;
}
}
string port = null;
port = Console.ReadLine();
SerialPort sp1 = new SerialPort("COM1");
sp1.Open();
SerialPort sp = new SerialPort("COM" + port);
try
{
sp.Open();
sp.Close();
Console.WriteLine("端口尚未打开");
}
catch (InvalidOperationException e)
{
Console.WriteLine("端口已打开");
}
catch(Exception ee)
{
Console.WriteLine( new Exception(ee.Message));
}
string port = null;
port = Console.ReadLine();
SerialPort sp = new SerialPort("COM" + port);
try
{
sp.Open();
Thread.Sleep(1000);
sp.Close();
Console.WriteLine("端口尚未打开");
}
catch (InvalidOperationException e)
{
Console.WriteLine("端口已打开");
}
catch(Exception ee)
{
Console.WriteLine( new Exception(ee.Message));
}