2012-11-22 110 views
1

我想开发一个模块从我的应用程序发送短信。问题是,当我发送AT命令到连接的手机时,我的应用程序停止响应。我正在使用诺基亚6720C,并安装了Pc Suite。它也出现在COM端口中。通过USB GSM调制解调器发送短信

我的代码如下。我究竟做错了什么?

Imports System 
Imports System.IO.Ports 
Imports System.Threading 
Imports System.ComponentModel 

Public Class Form1 
Delegate Sub SetTextCallback(ByVal [text] As String) 
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click 

    With SerialPort1 
     .PortName = "COM14" 
     .DataBits = 8 
     .Parity = IO.Ports.Parity.None 
     .StopBits = StopBits.One 
    End With 

    SerialPort1.Open() 
    SerialPort1.Write("AT" & vbCr) 
    SerialPort1.Close() 
End Sub 

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived 
    ReceivedText(SerialPort1.ReadExisting()) 
End Sub 

Private Sub ReceivedText(ByVal [text] As String) 
    'compares the ID of the creating Thread to the ID of the calling Thread 
    If Me.TxtResponse.InvokeRequired Then 
     Dim x As New SetTextCallback(AddressOf ReceivedText) 
     Me.Invoke(x, New Object() {(text)}) 
    Else 
     Me.TxtResponse.Text &= [text] 
    End If 
End Sub 
+0

你在哪里定义'SerialPort1'? – Basic

+0

我在我的窗体设计视图上使用SerialPort1 .. – Malik

+0

谢谢。下一个问题,应用程序挂在哪一行?如果你在'SerialPort1.Open()'和“Step Over”每行代码上放置一个断点,你是否会到达'End Sub'?另外,你确定你有正确的COM端口吗? – Basic

回答

0

好,在AT命令结束时,你必须添加CHR(13)和CHR(10),您使用CHR(13)只(这是 “VBCR”),尝试用vbCrLf替换它(回车“Chr(13)”和换行“Chr(10)”)。我认为这是问题(命令字符串不完整)。

如果问题仍然驻留,请尝试以下操作来测试您的工作:

  1. 尝试使用VB.NET代码与GSM调制解调器(不是你的电话)进行通信。如果它工作正常,那么也许手机不接受AT命令。

  2. 尝试使用终端程序将AT命令发送到手机。终端程序可以像“超级终端”或“Maestro智能终端”(您可以在谷歌搜索)。

我希望这是有益的,祝你好运。

相关问题