Visual Basic.NET 7.x (2002/03)Visual Basic 9 (2008)Visual Basic 8 (2005)Visual Basic 6Visual Studio 2005Windows XP中级Visual StudioWindows.NETVisual Basic
VB.NET AT 命令发送短信






4.81/5 (23投票s)
本文允许您使用 GSM 调制解调器或通过 USB 连接到计算机的手机向任何移动设备发送 SMS
引言
这段代码允许通过 GSM 调制解调器或任何 GSM 兼容手机从电脑向任何手机发送短信。
背景
基本上,我使用的是 AT 命令,即注意力命令,用于计算机和调制解调器之间的通信。
微软 Windows 提供了一个名为 Hyperterminal 的软件,可以通过以下路径找到:
开始 -> 程序 -> 附件 -> 通讯 -> 超终端
AT 命令可以在这里输入。超终端直接与连接的调制解调器/手机通信并指示调制解调器。它也会返回 OK 或错误等响应。但是,如果我们需要在执行特定操作后发送短信的应用程序,那么超终端就无用了,因为它需要手动输入。您需要选择连接调制解调器/手机的 COM 端口(端口可以是调制解调器的串行端口或手机的 USB 端口)。
给定的代码就是这样做的。如果您有任何疑问,可以在下面的论坛上联系我。
Using the Code
首先,连接调制解调器或手机。转到设备管理器,并在“端口”选项下检查您的调制解调器/手机连接到哪个 COM 端口(例如 COM3、COM4 等)。
//
// if COM3 is shown in device manager then put COM4 in foll statement
//
Dim SMSEngine As New SMSCOMMS("COM4")
'the port needs to be initialised
SMSPort = New SerialPort
With SMSPort
.PortName = COMMPORT
.BaudRate = 19200
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
.Handshake = Handshake.RequestToSend
.DtrEnable = True
.RtsEnable = True
.NewLine = vbCrLf
End With
'this is the set of AT commands to be written on serial port
SMSPort.WriteLine("AT")
'set command message format to text mode(1)
SMSPort.WriteLine("AT+CMGF=1" & vbCrLf)
'set service center address (which varies for service providers (idea, airtel))
SMSPort.WriteLine("AT+CSCA=""+919822078000""" & vbCrLf)
' enter the mobile number whom you want to send the SMS
SMSPort.WriteLine("AT+CMGS= + TextBox1.text + " & vbCrLf)
_ContSMS = False
SMSPort.WriteLine("+ TextBox1.text +" & vbCrLf & Chr(26)) 'SMS sending
Dim i As Integer
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
SMSEngine.Open() 'open the port
SMSEngine.SendSMS() 'send the SMS
End Sub
代码中定义了 SMSCOMMS 类。我只用诺基亚手机(如诺基亚 5310、诺基亚 3500 等)测试过此代码。我还尝试从爱立信发送短信到联通,效果很好。下载代码,它简单易懂,并附有注释。
关注点
移动通信编程、串口接口、.NET 中的 TAPI 编程