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

📄 sms.vb

📁 vc实现串口收发短信
💻 VB
📖 第 1 页 / 共 4 页
字号:
'==========================================================
'                    SMS,EMS Decoder
'                      2004-9-24
'1.Description
'   This class decode a SMS or EMS PDU code to a certain
'class. You can use it in your software to read SMSs and
'EMSs. All of this is done under GSM 03.40. I tested it
'on my SIEMENS M55 and NOKIA 8xxx and it works well.
'2.Useage
'   If you know what type of PDU code, you can create a
'new instance of class like DIM s as SMS(myPDUCode)
'When instance is created, you read its public variable
'to get what you want.
'   When TP_DCS=0, PDU code is coded from 7bit
'charactor (see GSM 03.38), use shared function
'Deocde7Bit to decode it.
'   When TP_DCS=8, PDU code is coded from Unicode
'charactor (see GSM 03.38), use shared funtion
'DecodeUnicode to decode it.
'3.Bugs
'   So far in my tests I found none.
'4.When you use it
'   You can freely use it or modify it in your program,
'but when you find bugs or improved it please publish it
'or send one copy to me. Thanks
'5.About me
'   I am writing a program which can list folders and
'files in SIEMENS M55 mobile phone. It can also read
'and send SMS,EMS. Some documents are hard to find on
'internet, but I keep on my mind to study it and finally
'I found it is full of interests.
'    I like freedom, so I exchange my ideas with all of
'the world. It is so happy that you can use my classes!
'   In the end, sorry for my poor English.
'6.Contact me
'   Email:hesicong@mail.sc.cninfo.net
'   QQ:38288890
'   Homepage:http://dream-world.nease.net (Chinese)
'   Thanks for using it!
'                       ----By HESICONG
'
'Revision:
'   2004-10-29:
'       Fix bug in decode "@" charactor.
'       Add functions in "decode 7 bit code to english" region


Imports System.text

Namespace SMS
    Namespace Decoder
        ''' -----------------------------------------------------------------------------
        ''' Project	 : SMS Solution
        ''' Class	 : SMS.Decoder.SMSBase
        ''' 
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' 	[AMDXP2500]	2005-7-25	Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public MustInherit Class SMSBase
            'Note all of following various with TP_ can be found in GSM 03.40
            Public SCAddressLength As Byte  'Service Center Address length
            Public SCAddressType As Byte    'Service Center Type[See GSM 03.40]
            Public SCAddressValue As String 'Service Center number
            Public FirstOctet As Byte       'See GSM 03.40

            Public TP_PID As Byte
            Public TP_DCS As Byte
            Public TP_UDL As Byte
            Public TP_UD As String
            Public Text As String
            Public Type As SMSType
            Public UserData As String

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Public Enum SMSType
                SMS_RECEIVED = 0
                SMS_STATUS_REPORT = 2
                SMS_SUBMIT = 1
                EMS_RECEIVED = 64 'It is "Reserved" on my phone??
                EMS_SUBMIT = 65
                UNKNOWN = -1
            End Enum

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <param name="PDUCode"></param>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Public MustOverride Sub GetOrignalData(ByVal PDUCode As String)


            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' Get a byte from PDU string
            ''' </summary>
            ''' <param name="PDUCode"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function GetByte(ByRef PDUCode As String) As Byte
                Dim r As Byte = CByte(Val("&H" + Mid(PDUCode, 1, 2)))
                PDUCode = Mid(PDUCode, 3)
                Return r
            End Function

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' Get a string of certain length
            ''' </summary>
            ''' <param name="PDUCode"></param>
            ''' <param name="Length"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function GetString(ByRef PDUCode As String, ByVal Length As Integer) As String
                Dim r As String = Mid(PDUCode, 1, Length)
                PDUCode = Mid(PDUCode, Length + 1)
                Return r
            End Function
            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' Get date from SCTS format
            ''' </summary>
            ''' <param name="SCTS"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function GetDate(ByRef SCTS As String) As Date
                Dim year, month, day, hour, minute, second, timezone As Integer

                year = CInt(Val(Swap(GetString(SCTS, 2))) + 2000)
                month = CInt(Val(Swap(GetString(SCTS, 2))))
                day = CInt(Val(Swap(GetString(SCTS, 2))))
                hour = CInt(Val(Swap(GetString(SCTS, 2))))
                minute = CInt(Val(Swap(GetString(SCTS, 2))))
                second = CInt(Val(Swap(GetString(SCTS, 2))))
                timezone = CInt(Val(Swap(GetString(SCTS, 2))))

                Dim result As New Date(year, month, day, hour, minute, second)
                Return result
            End Function

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' Swap two bit
            ''' </summary>
            ''' <param name="TwoBitStr"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function Swap(ByRef TwoBitStr As String) As String
                Dim c() As Char = TwoBitStr.ToCharArray
                Dim t As Char
                t = c(0)
                c(0) = c(1)
                c(1) = t
                Return (c(0) + c(1)).ToString
            End Function

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' Get phone address
            ''' </summary>
            ''' <param name="Address"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function GetAddress(ByRef Address As String) As String
                Dim tmpChar As Char() = Address.ToCharArray
                Dim i As Integer, result As String = String.Empty
                For i = 0 To tmpChar.GetUpperBound(0) Step 2
                    result += Swap(tmpChar(i) + tmpChar(i + 1))
                Next
                If InStr(result, "F") > 0 Then result = Mid(result, 1, result.Length - 1)
                Return result
            End Function

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <param name="PDUCode"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function GetSMSType(ByVal PDUCode As String) As SMSBase.SMSType
                'Get first october
                Dim FirstOctet As Byte
                Dim L As Integer = SMSBase.GetByte(PDUCode)
                SMSBase.GetByte(PDUCode)
                SMSBase.GetString(PDUCode, (L - 1) * 2)
                FirstOctet = SMSBase.GetByte(PDUCode)
                'Get base code. Use last 2 bit and whether there's a header as remark
                Dim t1 As Integer = FirstOctet And 3 '00000011
                Dim t2 As Integer = FirstOctet And 64 '01000000
                'Do some special task
                If t1 = 3 And t2 = 64 Then Return SMSBase.SMSType.EMS_SUBMIT
                '#########################################
                'Can I use some easier code to handle?
                '#########################################

                Select Case t1 + t2
                    Case 0
                        Return SMSType.SMS_RECEIVED
                    Case 2
                        Return SMSType.SMS_STATUS_REPORT
                    Case 1
                        Return SMSType.SMS_SUBMIT
                    Case 64
                        Return SMSType.EMS_RECEIVED
                    Case 65
                        Return SMSType.EMS_SUBMIT
                    Case Else
                        Return SMSType.UNKNOWN
                End Select
            End Function

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' Decode a unicode string
            ''' </summary>
            ''' <param name="strUnicode"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function DecodeUnicode(ByVal strUnicode As String) As String
                Dim Code As String = String.Empty

                Dim j As Integer
                Dim c() As String   'temp
                ReDim c(CInt(strUnicode.Length / 4))     '2 Byte a Unicode char
                Dim tstr As String

                For j = 0 To strUnicode.Length - 1 Step 4
                    tstr = strUnicode.Substring(j, 4)
                    Code += ChrW(CInt(Val("&H" & tstr)))
                Next
                Return Code
            End Function

#Region "'Deocde 7Bit to english"
            '2004-10-29:Region added
            'Fixed decode "@" charactor
            'I use a new method, it is easy to understand but looks much longer than before.

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <param name="HexString"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function InvertHexString(ByVal HexString As String) As String
                'For example:
                '123456
                '===>
                '563412
                Dim Result As New StringBuilder
                Dim i As Integer
                For i = HexString.Length - 2 To 0 Step -2
                    Result.Append(HexString.Substring(i, 2))
                Next
                Return Result.ToString
            End Function

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <param name="Dec"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function ByteToBinary(ByVal Dec As Byte) As String
                Dim Result As String = String.Empty
                Dim Temp As Byte = Dec
                Do
                    Result = (Temp Mod 2) & Result
                    If Temp = 1 Or Temp = 0 Then Exit Do
                    Temp = CByte(Temp \ 2)
                Loop
                Result = Result.PadLeft(8, CChar("0"))
                Return Result
            End Function

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' 
            ''' </summary>
            ''' <param name="Binary"></param>
            ''' <returns></returns>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            ''' 	[AMDXP2500]	2005-7-25	Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Shared Function BinaryToInt(ByVal Binary As String) As Integer
                Dim Result As Integer
                Dim i As Integer
                For i = 0 To Binary.Length - 1
                    Result = Result + CInt(Val(Binary.Substring(Binary.Length - i - 1, 1)) * 2 ^ i)
                Next
                Return Result
            End Function

⌨️ 快捷键说明

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