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

📄 nokiaplugin.vb

📁 vc实现串口收发短信
💻 VB
📖 第 1 页 / 共 3 页
字号:
        End Function

        ''' <summary>
        ''' Send a SMS
        ''' </summary>
        ''' <param name="toSend"></param>
        ''' <returns>Message references of SMS. If content is bigger than capacity of one SMS , it will be splited to one more SMSs.</returns>
        ''' <remarks></remarks>
        Public Function SendSMS(ByVal toSend As PhoneControllerSDK.SMSSend) As Integer() Implements PhoneControllerSDK.IPhoneController.SendSMS
            'Make SMS

            'Try
            Dim PDUCode As String() = ProduceSMS(toSend.DestinationNumber, toSend.RequireStatusReport, toSend.ValidPeriod, toSend.Content, ServiceCenterNumber)

            Dim SMSIndex(PDUCode.Length - 1) As Integer 'Record the index of SMS

            Dim i As Integer
            For Each PDU As String In PDUCode
                Dim LenForAT As Integer = CInt((PDU.Length - Val("&H" & Mid(PDU, 1, 2)) * 2 - 2) / 2)     'Calculate PDU Length for AT command
                Dim Rsp As String = mATCmd.SendCommand("AT+CMGS=" & LenForAT, ">", PDU & Chr(&H1A))

                If ATCommandBase.CheckResponse(Rsp) = False Then
                    Return New Integer() {-1}
                End If
                Dim m As Match = regexCMGSW.Match(Rsp)
                SMSIndex(i) = CInt(m.Groups("Index").Value)
                i += 1
            Next
            Return SMSIndex
        End Function

        ''' <summary>
        ''' Set or get Service center number
        ''' </summary>
        ''' <value></value>
        ''' <remarks></remarks>
        Public Property ServiceCenterNumber() As String Implements PhoneControllerSDK.IPhoneController.ServiceCenterNumber
            Get
                Return mServiceCenterNumber
            End Get
            Set(ByVal value As String)
                mServiceCenterNumber = value
            End Set
        End Property

        ''' <summary>
        ''' Using AT command to get service center number 
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetServiceCenterNumber() As String Implements PhoneControllerSDK.IPhoneController.GetServiceCenterNumber
            Dim regex As Regex = New Regex( _
            "\+CSCA:\s""(?<NUM>(\w|\+)*)"",(?<TYPE>\d*)", _
            RegexOptions.None _
)
            Dim rst As String = mATCmd.SendCommand("AT+CSCA?")
            Dim m As Match = regex.Match(rst)
            If m.Success Then
                Return m.Groups("NUM").Value
            Else
                Throw New ApplicationException("Can't get service center number")
            End If
        End Function

        ''' <summary>
        ''' Set character set
        ''' </summary>
        ''' <param name="characterSet"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function SetCharacterSet(ByVal characterSet As PhoneControllerSDK.CharactorSet) As Boolean Implements PhoneControllerSDK.IPhoneController.SetCharacterSet
            currentCharactorSet = characterSet
            Select Case characterSet
                Case CharactorSet.ASCII
                    Return ATCommandBase.CheckResponse(mATCmd.SendCommand("AT+CSCS=""GSM"""))
                Case CharactorSet.UCS2
                    Return ATCommandBase.CheckResponse(mATCmd.SendCommand("AT+CSCS=""UCS2"""))
            End Select

        End Function

        ''' <summary>
        ''' Set phone to standard
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function SetStandard() As Boolean Implements PhoneControllerSDK.IPhoneController.SetStandard
            If ATCommandBase.CheckResponse(mATCmd.SendCommand("ATE0")) = False Then
                Throw New Exception("Can't execute ATE0")
            End If

            Dim modelID As String = GetModelID()
            If SelectSMSStorage(SMSStorage.MobileEquipment, SMSStorage.MobileEquipment, SMSStorage.MobileTerminal) = False Then
                Throw New Exception("Can't set SMS Storage")
            End If

            If SetCharacterSet(CharactorSet.UCS2) = False Then
                Throw New Exception("Can't set character set")
            End If

            If ATCommandBase.CheckResponse(mATCmd.SendCommand("AT+CMEE=1")) = False Then
                Throw New Exception("Can't set Error code")
            End If
            Return True
        End Function

        ''' <summary>
        ''' Return supported Model
        ''' </summary>
        ''' <value></value>
        ''' <remarks></remarks>
        Public ReadOnly Property SupportedModel() As String() Implements PhoneControllerSDK.IPhoneController.SupportedModel
            Get
                Return New String() {"Nokia 6610", "Nokia 6108", "Nokia 6021"}
            End Get
        End Property

        ''' <summary>
        ''' Write a SMS to memory
        ''' </summary>
        ''' <param name="toWrite"></param>
        ''' <returns>Index of write SMS. If content is longer than capacity of one SMS, it will be splited to one more SMSs.</returns>
        ''' <remarks></remarks>
        Public Function WriteSMS(ByVal status As ATSMSStatus, ByVal toWrite As PhoneControllerSDK.SMSSend) As Integer() Implements PhoneControllerSDK.IPhoneController.WriteSMS
            'Make SMS
            'Try
            Dim PDUCode As String() = ProduceSMS(toWrite.DestinationNumber, toWrite.RequireStatusReport, toWrite.ValidPeriod, toWrite.Content, ServiceCenterNumber)

            Dim SMSIndex(PDUCode.Length - 1) As Integer 'Record the index of SMS

            Dim i As Integer
            For Each PDU As String In PDUCode
                Dim LenForAT As Integer = CInt((PDU.Length - Val("&H" & Mid(PDU, 1, 2)) * 2 - 2) / 2)     'Calculate PDU Length for AT command
                Dim Rsp As String = String.Empty
                Select Case status
                    Case ATSMSStatus.REC_READ
                        Rsp = mATCmd.SendCommand("AT+CMGW=" & LenForAT & ",1", ">", PDU & Chr(&H1A))
                    Case ATSMSStatus.REC_UNREAD
                        Rsp = mATCmd.SendCommand("AT+CMGW=" & LenForAT & ",0", ">", PDU & Chr(&H1A))
                    Case ATSMSStatus.STO_SENT
                        Rsp = mATCmd.SendCommand("AT+CMGW=" & LenForAT & ",3", ">", PDU & Chr(&H1A))
                    Case ATSMSStatus.STO_UNSENT
                        Rsp = mATCmd.SendCommand("AT+CMGW=" & LenForAT & ",2", ">", PDU & Chr(&H1A))
                End Select

                If ATCommandBase.CheckResponse(Rsp) = False Then
                    Return New Integer() {-1}
                End If
                Dim m As Match = regexCMGSW.Match(Rsp)
                SMSIndex(i) = CInt(m.Groups("Index").Value)
                i += 1
            Next
            Return SMSIndex

        End Function
#End Region

#Region "Phone control"

#Region "Other help functions"
        Private Function DecodeUnicode(ByVal strUnicode As String) As String
            Dim Code As New System.Text.StringBuilder
            Dim tstr As String
            For i As Integer = 0 To strUnicode.Length - 4 Step 4
                tstr = strUnicode.Substring(i, 4)
                Code.Append(ChrW(CInt(Val("&H" & tstr))))
            Next
            Return Code.ToString
        End Function

        Private Function EncodeUnicode(ByVal str As String) As String
            Dim b As Byte() = System.Text.Encoding.BigEndianUnicode.GetBytes(str)
            Dim s As New System.Text.StringBuilder
            For i As Integer = 0 To b.Length - 1
                s.Append(Format(CInt(b(i)), "X2"))
            Next
            Return s.ToString
        End Function
#End Region

#Region "Others"
        Private Structure AT_SMS_Response
            Public Status As ATSMSStatus
            Public PDUCode As String
            Public Index As Integer
        End Structure

        Private regexCMGR As Regex = New Regex( _
        "\+CMGR:\s*(?<stat>\d*),,(?<length>\d*)\r\n\s*(?<PDU>\w*)", _
        RegexOptions.IgnoreCase _
        Or RegexOptions.Multiline _
        Or RegexOptions.IgnorePatternWhitespace _
        Or RegexOptions.Compiled _
        )

        Private regexCMGL As Regex = New Regex( _
    "\+CMGL:\s*(?<index>\d*),(?<stat>\d*),(?<alpah>\d*),(?<length>\d" _
    + "*)\r\n\s*(?<PDU>\w*)", _
    RegexOptions.IgnoreCase _
    Or RegexOptions.CultureInvariant _
    Or RegexOptions.IgnorePatternWhitespace _
    Or RegexOptions.Compiled _
    )

        Private regexCMGSW As Regex = New Regex( _
                "\+CMG(S|W):\s*(?<Index>\d+)", _
                RegexOptions.IgnoreCase _
                Or RegexOptions.Multiline _
                Or RegexOptions.IgnorePatternWhitespace _
                Or RegexOptions.Compiled _
                )

#End Region
#End Region

        Public Function CancelCurrentAction() As Boolean Implements PhoneControllerSDK.IPhoneController.CancelCurrentAction
            mCancel = True
            mATCmd.CancelCurrentAction()
        End Function

        Public ReadOnly Property TotalEntriesCount() As Integer Implements PhoneControllerSDK.IPhoneController.TotalEntriesCount
            Get
                Return mEntiresTotal
            End Get
        End Property

        Public ReadOnly Property TotalReadCount() As Integer Implements PhoneControllerSDK.IPhoneController.TotalReadCount
            Get
                Return mEntiresRead
            End Get
        End Property

        Public Function GetSMSStorageStatus() As PhoneControllerSDK.SMSStorageStatus Implements PhoneControllerSDK.IPhoneController.GetSMSStorageStatus
            Dim regex As Regex = New Regex( _
            "\+CPMS:\s*""(?<MEM1>\w*)"",(?<M1Used>\d+),(?<M1Total>\d+),""(?<MEM2>\w*)"",(?<M2Used>\d+),(?<M2Total>\d+),""(?<MEM3>\w*)"",(?<M3Used>\d+),(?<M3Total>\d+)", _
            RegexOptions.None _
)
            Dim rst As String = mATCmd.SendCommand("AT+CPMS?")
            Dim m As Match = regex.Match(rst)
            If m.Success Then

            Else
                Throw New ApplicationException("Can't get SMS storage")
            End If
        End Function

        Private Sub HeartBeat()
            On Error Resume Next
            mExitHeartBeat = False
            Do Until mExitHeartBeat
                Thread.Sleep(30000)
                mATCmd.SendCommand("AT")
            Loop
        End Sub
    End Class
End Namespace

⌨️ 快捷键说明

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