gpt4 book ai didi

c# - 通过C#的GSMCOMM库发送短信

转载 作者:太空宇宙 更新时间:2023-11-03 19:16:09 25 4
gpt4 key购买 nike

我已经开发了一个 C# 应用程序,用于使用 C# 的 GSMCOMM 库发送短信。但是我三天来面临的问题是,当我尝试使用 gsmcomm 对象发送消息时。发送消息方法。有时它会给出异常该电话未连接,有时它会给出异常端口未打开。我在下面分享我的代码:用于将 pc 连接到手机 gsm 调制解调器的代码。有时它会毫无异常(exception)地发送消息。
手机连接电脑的代码。

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;
}

最佳答案

当您使用 gsmcomm .. 首先,在组合框中列出您的 comPorts我精通 vb.net .. 你可以阅读这段代码并将其翻译成 C#1)在您的表单中创建一个组合框,并在 form_load 中编写此代码

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each prt In My.Computer.Ports.SerialPortNames
comboBox1.Items.Add(prt)
Next
End Sub

在您的 from 的全局范围内,编写此代码

            Public Property mymodem As GsmCommMain

向您的项目添加一个子项,如下所示

   Private Sub connect()
Try
Cursor.Current = Cursors.WaitCursor
If comboBox1.Text = "" Then Return
If IsNothing(mymodem) Then mymodem = New GsmCommMain(comboBox1.Text)
If Not mymodem.IsOpen Then mymodem.Open()
Cursor.Current = Cursors.Default
Catch ex As Exception
richTextBox1.AppendText(ex.Message & vbCrLf) 'i add a richtextbox to my form for show exceptions and my produced declaration
End Try
End Sub

之后放一个手机号码的文本框.. 将其命名为 txttel还为 textMessage 放置了一个文本框 .. 将其命名为 txtMSG放一个按钮给你发消息..命名为 btnsend剩下的代码将是这样的..

  Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
If String.IsNullOrEmpty(txtMSG.Text.Trim) Then Return
SendSMS()

End Sub


Private Sub SendSMS()
Try
If Not mymodem.IsOpen Then connect()
Dim pdu As New SmsSubmitPdu(txtMSG.Text.Trim & vbCr, txtTel.Text)
mymodem.SendMessage(pdu)
richTextBox1.AppendText("your message sent successfully")
Catch ex As Exception
richTextBox1.AppendText(ex.Message)
End Try
End Sub

最后一定要关闭你的端口..就像这样

  Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Not IsNothing(mymodem) AndAlso mymodem.IsOpen Then
mymodem.Close()
End If
End Sub

关于c# - 通过C#的GSMCOMM库发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16522442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com