⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cshortmessage.vb

📁 实现Text模式下短信的发送接收。有些较老的手机不支持PDU模式
💻 VB
字号:
Public Class CShortMessage   '短消息类
    Inherits CPhone

    '数据成员
    Public Structure SMS '短消息定义
        Public MessageType As String    '类型:IM=接收的信息 OM=发出的短信 DM=草稿 MT=全部
        Public Status As String         '状态:1.REC READ已读 2.REC UNREAD未读 3.STO SENT发出的 4.未发出的
        Public Number As String    '电话号码
        Public Content As String        '内容
        Public Position As Integer
    End Structure
    Public SMessage() As SMS '短消息数组

    Private Function ExecCmd(ByVal cmd As String) As String
        Phone.Write(cmd & vbCrLf)
        Phone.Read()
        Dim result As String = Phone.InputStreamString
        If InStr(result, "ERROR") Then result = "ERROR"
        Return result
    End Function

    '函数
    Public Function GetShortMessage(ByVal MessageType As String) As SMS
        If Phone.IsOpen = False Then Throw New Exception("Port isn't open")
        '检查手机状态
        If InStr(ExecCmd("AT"), "OK") = 0 Then Throw New Exception("Can't Init Phone")
        '初始化到短信状态
        Dim cmd, result As String                                       '发送到手机的命令
        ExecCmd("AT+cscs=""UCS2""")
        result = ExecCmd("AT+CPMS=""" & MessageType & """")
        If result = "ERROR" Then Throw New Exception("Can't set store position")
        '接受短信
        result = ExecCmd("AT+CMGL")
        Dim ss, tS As String                                            'ss临时变量,tS是总的信息
        Do While (InStr(ss, "OK") <= 0 And InStr(ss, "ERROR") <= 0)
            'phone.Write(Console.ReadLine() & vbCrLf)
            Phone.Read()
            ss = Phone.InputStreamString
            If ss.Length <> 0 Then tS += ss
        Loop

        '读取信息
        Dim i, NumberOfSMS As Integer
        Dim sp() As String = tS.Split(vbCr)
        Dim head(1) As String                                           '短信头
        Dim content(1) As String                                        '短信内容
        For i = 1 To sp.Length - 1                                      '分离头和信息内容;并将断开的内容重组
            If InStr(sp(i), "+CMGL") > 0 Then
                ReDim Preserve content(NumberOfSMS)
                ReDim Preserve head(NumberOfSMS)
                head(NumberOfSMS) = sp(i)
                NumberOfSMS += 1
            Else
                If sp(i) <> vbNullString And InStr(sp(i), "OK") = 0 Then content(NumberOfSMS - 1) += sp(i)
            End If
        Next

        '处理头
        ReDim SMessage(NumberOfSMS - 1)
        For i = 0 To head.Length - 1
            Dim pp() As String = head(i).Split(" ") '分割头,分别储存
            SMessage(i).Position = Val(pp(1))
            SMessage(i).Status = Mid(pp(2), 2) + Mid(pp(3), 1, pp(3).Length - 1)
            SMessage(i).Number = pp(4)
        Next

        '将UNICODE码编码成汉字
        For i = 0 To content.Length - 1
            Dim TEMP() As Char = content(i).ToCharArray
            Dim re, tstring As String
            re = ""         '储存清理过的UNICODE码,便于操作
            tstring = ""
            '此循环处理有时候返回的chr(10)换行符
            For Each tstring In TEMP
                NumberOfSMS = Asc(tstring)
                If NumberOfSMS <> 10 Then re = re + tstring
            Next
            content(i) = re
            Dim c() As String   '临时变量
            ReDim c(content(i).Length / 4)
            Dim j As Integer
            For j = 0 To content(i).Length \ 4 - 1
                Dim d() As Char = content(i).ToCharArray(j * 4, 4)
                c(j) = "&H" & CType(d, String)
                c(j) = ChrW(Val(c(j)))
                SMessage(i).Content += c(j)     '将最后的结果返回
            Next
        Next
    End Function
    Public Function SendMessage(ByVal PhoneNumber As String, ByVal Msg As String)
        Dim i As Integer
        phone.Write("AT+CSCS=""UCS2""" & vbCrLf)
        '编码:编为Unicode码
        Dim temp() As Char
        temp = Msg.ToCharArray
        Dim t As Char, UMsg As String, HexCode As String 'UMsg为编写后的Unicode
        For Each t In temp
            HexCode = Hex(AscW(t))
            If HexCode.Length = 2 Then HexCode = "00" + HexCode
            UMsg = UMsg + HexCode
        Next
        ExecCmd("ATE0") '关闭回显

        Dim NumOfSms As Integer = UMsg.Length / 4 \ 134
        Dim MsgToSend As String
        For i = 0 To NumOfSms
            Do
                Dim TEST As String = ExecCmd("AT")
                Console.WriteLine("try to at" & TEST)
                If InStr(TEST, "OK") Then Exit Do
            Loop
            ExecCmd("AT+CMGW=""" & PhoneNumber & """")
            MsgToSend = Mid$(UMsg, i * 134 * 4 + 1, 134 * 4)

            Dim n As Integer = (MsgToSend.Length \ 80)
            Dim sTemp As String, j As Integer
            For j = 0 To n
                sTemp = Mid(MsgToSend, j * 80 + 1, 80)
                phone.Write(sTemp & vbCrLf)
            Next
            phone.Write(Chr(26)) '结束符
            Dim timeout As Integer = 0
            '等待直到timeout
            Dim Continue As Boolean = True
            Do Until Continue = False
                phone.Read()
                '查找返回的数据
                Dim sInput As String = phone.InputStreamString
                Dim pos As Integer = InStr(sInput, ":")
                Dim result As Integer = Val(Mid(sInput, pos + 1))
                If result <> 0 Then
                    phone.Write("AT+CMSS=" & result & vbCrLf)
                    Debug.WriteLine("trying to send" & result)
                    Dim sendTimeOut As Integer = 0
                    Do
                        phone.Read()
                        Dim sReturn As String = phone.InputStreamString
                        If InStr(sReturn, "OK") Then
                            Debug.WriteLine("Msg " & result & "sent")
                            Continue = False
                            Exit Do
                        End If
                        If InStr(sReturn, "ERROR") Then
                            phone.Write("AT+CMSS=" & result)
                            sendTimeOut += 1
                            Debug.WriteLine(sendTimeOut)
                        End If
                        If sendTimeOut = 2 Then Throw New Exception("Can't send message, ID=" & result)
                    Loop
                Else
                    timeout += 1
                    Debug.WriteLine("Timeout=" & timeout)
                    If timeout = 50 Then Throw New Exception("Can't send message, time out")
                End If
            Loop
        Next
    End Function

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -