在vb.net中使用gsm向多个收件人发送短信

时间:2015-09-17 02:55:03

标签: vb.net sms at-command

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

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)
        SMSPort = New SerialPort
        With SMSPort
            .PortName = COMMPORT
            .BaudRate = 115200
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One
            .Handshake = Handshake.RequestToSend
            .DtrEnable = True
            .RtsEnable = True
            .NewLine = vbCrLf
        End With
        'ReadThread = New Thread(AddressOf ReadPort)
    End Sub

    Public Sub SendSMS(ByVal CellNumber As String, ByVal SMSMessage As String)
        Dim MyMessage As String = Nothing
        If SMSMessage.Length <= 160 Then
            MyMessage = SMSMessage
        Else
            MyMessage = Mid(SMSMessage, 1, 160)
        End If
        If IsOpen = True Then
            SMSPort.WriteLine("AT+CMGS=" & CellNumber & vbCr)
            _ContSMS = False
            Thread.SpinWait(200)
            SMSPort.WriteLine(MyMessage & vbCrLf & Chr(26))
            _Continue = False
            Thread.SpinWait(200)
            RaiseEvent Sending(False)
        End If
    End Sub

    Private Sub ReadPort()
        Dim SerialIn As String = Nothing
        Dim RXBuffer(SMSPort.ReadBufferSize) As Byte
        Dim SMSMessage As String = Nothing
        Dim Strpos As Integer = 0
        Dim TmpStr As String = Nothing

        While SMSPort.IsOpen = True
            If (SMSPort.BytesToRead <> 0) And (
                SMSPort.IsOpen = True) Then
                While SMSPort.BytesToRead <> 0
                    SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize)
                    SerialIn =
                        SerialIn & System.Text.Encoding.ASCII.GetString(
                        RXBuffer)
                    If SerialIn.Contains(">") = True Then
                        _ContSMS = True
                    End If
                    If SerialIn.Contains("+CMGS:") = True Then
                        _Continue = True
                        RaiseEvent Sending(True)
                        _Wait = False
                        SerialIn = String.Empty
                        ReDim RXBuffer(SMSPort.ReadBufferSize)
                    End If
                End While
                RaiseEvent DataReceived(SerialIn)
                SerialIn = String.Empty
                ReDim RXBuffer(SMSPort.ReadBufferSize)
            End If
        End While
    End Sub

    Public ReadOnly Property IsOpen() As Boolean
        Get
            If SMSPort.IsOpen = True Then
                IsOpen = True
            Else
                IsOpen = False
            End If
        End Get
    End Property

    Public Sub Open()
        If IsOpen = False Then
            SMSPort.Open()
            'ReadThread.Start()
        End If
    End Sub

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

我尝试使用此代码在vb.net中使用gsm调制解调器发送短信。这工作正常但是当我尝试向多个收件人发送短信时,第一个收件人只接收短信而其他收件人没有响应。

这是发送短信的代码:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sms As SMSCOMMS
        sms = New SMSCOMMS("COM25")
        sms.Open()
        sms.SendSMS("+63*******", "123")
        Threading.Thread.SpinWait(200)
        sms.SendSMS("+63*******", "456")
        sms.Close()
    End Sub

0 个答案:

没有答案