我用c# .But的GSMCOMM库开发了一个发送短信的C#应用程序,我三天来一直面临的问题是,当我尝试用gsmcomm的objects.send消息方法.Sometimes发送消息时,它给出了手机未连接的异常,有时它给出了异常端口未打开。我在下面分享我的代码:代码连接pc到手机gsm modem.And有时它发送的消息没有给任何异常。
用于将电话连接到pc的代码。
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;
}和用于发送SMS的代码
if (gsmComm.IsConnected() == false)
{
this.ConnectPhone();
}
pdu = new SmsSubmitPdu(strSMS, cellNO, "");
gsmComm.SendMessage(pdu);
catch (Exception ex)
{
throw ex;
}发布于 2013-07-08 22:14:27
试试这些指南(对我很有帮助):http://www.codeproject.com/Articles/325731/Bulk-SMS-Sender http://www.codeproject.com/Articles/20420/How-To-Send-and-Receive-SMS-using-GSM-Modem
但似乎com端口打开的问题并不在代码中。尝试使用Teraterm app之类的工具测试您的端口。当你开始运行你的应用程序时,请确保端口没有打开(在之前的启动之后,它可能仍然是打开的)。
发布于 2015-12-02 23:58:12
当你使用gsmcomm时..首先,在vb.net的comboBox i expert中列出您的comPorts。您可以阅读此代码并将其转换为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将sub添加到您的项目中,如下所示
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 Subhttps://stackoverflow.com/questions/16522442
复制相似问题