我已经开发了 ac# 应用程序,用于使用 C# 的 GSMCOMM 库发送短信。但是我面临三天的问题是,当我尝试使用 gsmcomm objects.send message methode 发送消息时。有时它会给出电话异常未连接,有时它会给出异常端口未打开。我在下面分享我的代码:用于将 pc 连接到手机 gsm 调制解调器的代码。有时它会毫无例外地发送消息。
手机连接电脑的代码。
private bool ConnectPhone()
{
string conectionStr = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
clsFileLogger.VerifyLogFileDirectory();
clsFileLogger.WriteToLog("DB Connection: " + conectionStr);
conn = new SqlConnection(@conectionStr);
int port = Convert.ToInt32(ConfigurationSettings.AppSettings["port"]);
int baudRate = Convert.ToInt32(ConfigurationSettings.AppSettings["baudRate"]);
int timeout = Convert.ToInt32(ConfigurationSettings.AppSettings["timeout"]);
gsmComm = new GsmCommMain(port, baudRate, timeout);
try
{
Isconnected = false;
if (gsmComm.IsConnected() == false)
{
gsmComm.Open();
}
Isconnected = gsmComm.IsConnected();
clsFileLogger.WriteToLog("\nConnected with GSM Modam");
}
catch (Exception)
{
clsFileLogger.WriteToLog("\nUnable to open the port.");
}
return Isconnected;
}
和发送短信的代码
if (gsmComm.IsConnected() == false)
{
this.ConnectPhone();
}
pdu = new SmsSubmitPdu(strSMS, cellNO, "");
gsmComm.SendMessage(pdu);
catch (Exception ex)
{
throw ex;
}