SMS发送应用程序在VB.net中

时间:2013-11-18 14:35:01

标签: vb.net com sms

我正在尝试使用vb.net 2008开发一个应用程序,以便从我的电脑发送短信到电话号码。我已经使用USB线(连接COM3端口)将我的诺基亚手机连接到我的电脑。鉴于我编写的波纹管代码应该工作但是消息没有被发送,我将我的应用程序作为死锁条件:

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

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim ports As String() = SerialPort.GetPortNames

            Dim port As String

            For Each port In ports
                ComboBox1.Items.Add(port)

            Next port

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
        Try
            With SerialPort1
                .PortName = ComboBox1.Text
                .BaudRate = 9600
                .Parity = Parity.None
                .StopBits = StopBits.One
                .DataBits = 8
                .Handshake = Handshake.RequestToSend
                .DtrEnable = True
                .RtsEnable = True
                .NewLine = vbCrLf
                .Open()
                MsgBox("Connected !", MsgBoxStyle.Information)

            End With

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        Try
            If SerialPort1.IsOpen Then
                With SerialPort1
                    .Write("AT" & vbCrLf)
                    .Write("AT+CMGF=1" & vbCrLf)
                    .Write("AT+CMGS=" & Chr(34) & TextBox1.Text & Chr(34) & vbCrLf)
                    .Write(RichTextBox1.Text & Chr(26))

                    .Close()
                    MsgBox("Message Sent!", MsgBoxStyle.Information)


                End With

            Else
                MsgBox("Error on port")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

End Class

2 个答案:

答案 0 :(得分:1)

Public Function sendMsg(ByVal port As SerialPort, ByVal PhoneNo As String, ByVal Message As String) As Boolean
        Dim isSend As Boolean = False

        Try

            Dim recievedData As String = ExecCommand(port,"AT", 300, "No phone connected")
            recievedData = ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format.")
            Dim command As String = "AT+CMGS=""" & PhoneNo & """"
            recievedData = ExecCommand(port,command, 300, "Failed to accept phoneNo")
            command = Message & Char.ConvertFromUtf32(26) & vbCr
            recievedData = ExecCommand(port,command, 3000, "Failed to send message") '3 seconds
            If recievedData.EndsWith(vbCrLf & "OK" & vbCrLf) Then
                isSend = True
            ElseIf recievedData.Contains("ERROR") Then
                isSend = False
            End If
            Return isSend
        Catch ex As Exception
            Throw ex
        End Try

    End Function

的execCommand

Public Function ExecCommand(ByVal port As SerialPort, ByVal command As String, ByVal responseTimeout As Integer, ByVal errorMessage As String) As String
        Try

            port.DiscardOutBuffer()
            port.DiscardInBuffer()
            receiveNow.Reset()
            port.Write(command & vbCr)

            Dim input As String = ReadResponse(port, responseTimeout)
            If (input.Length = 0) OrElse (((Not input.EndsWith(vbCrLf & "> "))) AndAlso ((Not input.EndsWith(vbCrLf & "OK" & vbCrLf)))) Then
                Throw New ApplicationException("No success message was received.")
            End If
            Return input
        Catch ex As Exception
            Throw ex
        End Try
    End Function

答案 1 :(得分:0)

您也可以尝试这种编码..

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

Public Class Form1
    'connect your mobile/GSM modem to PC,
    'then go in device manager and check under ports which COM port has been slected
    'if say com1 is there then put com2 in following statement
    Dim SMSEngine As New SMSCOMMS("COM17")
    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

End Class
Public Class SMSCOMMS
    Private WithEvents SMSPort As SerialPort
    Private SMSThread As Thread
    Private ReadThread As Thread
    Shared _Continue As Boolean = False
    Shared _ContSMS As Boolean = False
    Private _Wait As Boolean = False
    Shared _ReadPort As Boolean = False
    Public Event Sending(ByVal Done As Boolean)
    Public Event DataReceived(ByVal Message As String)

    Public Sub New(ByRef COMMPORT As String)
        'initialize all values
        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
    End Sub
    Public Function SendSMS() As Boolean
        Application.DoEvents()

        If SMSPort.IsOpen = True Then
            'sending AT commands
            SMSPort.WriteLine("AT")
            Thread.Sleep(500)
            SMSPort.WriteLine("AT+CMGF=1" & vbCrLf) 'set command message format to text mode(1)
            Thread.Sleep(500)

            '    SMSPort.WriteLine("AT+CSCA=""+919822078000""" & vbCrLf) 'set service center address (which varies for service providers (idea, airtel))
            SMSPort.WriteLine("AT+CMGS=" & """" & Form1.TextBox1.Text & """" & vbCrLf) ' enter the mobile number whom you want to send the SMS
            _ContSMS = False
            Thread.Sleep(500)

            SMSPort.WriteLine(Form1.TextBox2.Text & Chr(26)) 'SMS sending(specify the labl and the text box)
            MessageBox.Show(":send")
            SMSPort.Close()
        End If
    End Function

    Public Sub Open()
        If Not (SMSPort.IsOpen = True) Then
            SMSPort.Open()
        End If
    End Sub

    Public Sub Close()
        If SMSPort.IsOpen = True Then
            SMSPort.Close()
        End If
    End Sub
End Class

您必须将手机与PC套件或蓝牙连接或使用USB线连接。 之后,您必须根据您的端口号检查端口号并更改编码部分。

Dim SMSEngine As New SMSCOMMS("COM17")

这对我有用..