检查SerialPort存在调制解调器?

时间:2015-06-26 03:22:37

标签: c#-2.0

在C#2005中,我使用的是诺基亚E63,我想查看SerialPort Exist Modem,虽然它运行时没有运行COM端口,但是看到我的代码

private void btnGetPort_Click(object sender, EventArgs e)
        {
            cmbPort.Items.Clear();
            foreach (string portName in SerialPort.GetPortNames())
            {
                try
                {
                    using (SerialPort sp = new SerialPort(portName, 9600))
                    {
                        if (!(sp.IsOpen)) sp.Open();
                        sp.DiscardInBuffer();
                        sp.DiscardOutBuffer();
                        sp.Write("AT\r\n");
                        System.Threading.Thread.Sleep(100);
                        string response = sp.ReadExisting();
                        if (response == "OK")
                        {
                            cmbPort.Items.Add(portName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    continue;
                }
            }
        }

0 个答案:

没有答案