查找C#GSM调制解调器端口 [英] Find gsm modem port in c#
本文介绍了查找C#GSM调制解调器端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要遍历可用的端口:找到
System.IO.Ports.SerialPort.GetPortNames()
如果一个端口被GSM调制解调器。
的任何想法,请
I want to loop through the available ports: System.IO.Ports.SerialPort.GetPortNames() to find if a port is used by a gsm modem. Any idea please.
推荐答案
我在我的应用程序所做的一个类似的任务:
What I did in my application for one similar task:
-
要检查调制解调器连接到特定的端口,您可以发送AT命令到这个端口。如果我们发现目前的COM端口调制解调器
下面这个函数返回true:
To check that a modem is connected to particular port you can send AT command into this port. This function below returns true if we found a modem on the current COM port:
private bool CheckExistingModemOnComPort(SerialPort serialPort)
{
if ((serialPort == null) || !serialPort.IsOpen)
return false;
// Commands for modem checking
string[] modemCommands = new string[] { "AT", // Check connected modem. After 'AT' command some modems autobaud their speed.
"ATQ0" }; // Switch on confirmations
serialPort.DtrEnable = true; // Set Data Terminal Ready (DTR) signal
serialPort.RtsEnable = true; // Set Request to Send (RTS) signal
string answer = "";
bool retOk = false;
for (int rtsInd = 0; rtsInd < 2; rtsInd++)
{
foreach (string command in modemCommands)
{
serialPort.Write(command + serialPort.NewLine);
retOk = false;
answer = "";
int timeout = (command == "AT") ? 10 : 20;
// Waiting for response 1-2 sec
for (int i = 0; i < timeout; i++)
{
Thread.Sleep(100);
answer += serialPort.ReadExisting();
if (answer.IndexOf("OK") >= 0)
{
retOk = true;
break;
}
}
}
// If got responses, we found a modem
if (retOk)
return true;
// Trying to execute the commands without RTS
serialPort.RtsEnable = false;
}
return false;
}
在下一阶段,我们可以收集来自调制解调器的一些数据。
我用下面的命令:
On the next stage we can collect some data from the modem. I used the following commands:
- ATQ0 - 关于确认开关(接收对每个请求确定)
- ATE0 - 回声
- ATI - 获得调制解调器细节
- ATI3 - 获得更多的调制解调器的详细信息(不是所有的调制解调器支持该命令)
开关
- ATQ0 - switch on confirmations (receive OK on each request)
- ATE0 - switch on echo
- ATI - get modem details
- ATI3 - get extended modem details (not all modems supports this command)
这篇关于查找C#GSM调制解调器端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文