Home | Download | Library | About | Blog

How to access mobile phone using MBUS protocol

Some times I do crazy things, this time I tried to use Visaul Basic to access my cell phone phone book using MBUS protocol. There are a lot of programs out there which can do this and a lot more. This is just a sample code to show the basics of implementing MBUS using Visual Basic. The best source for information about MBUS protocol is THE MBUS/FBUS PAGES
This is a sample code, don't expect too much. I accept no responsibility about problems you may cause when you use this sample code. Download Code


Dim AckString, PCAckString, SeqNum

Private Sub Form_Activate()
    ' First, set your COMM control properties for MBUS
    MSComm1.Settings = "9600,o,8,1"
    MSComm1.InputMode = comInputModeBinary
    MSComm1.RTSEnable = True
    MSComm1.PortOpen = True

    'These are headers for ACK messages
    AckString = Chr(&H1F) + Chr(&H1D) + Chr(&H0) + Chr(&H7F)
    PCAckString = Chr(&H1F) + Chr(&H0) + Chr(&H1D) + Chr(&H7F)

    'I did not find any resource about how to start a Sequence Number
    'I had to send a dummy message in order to start with my number
    SeqNum = 1
    temp = ReadPhoneBook(250, Nam, Number)

    'This loop will read first 100 items from phone book
    For i = 1 To 100
        DoEvents
        SeqNum = SeqNum + 1
        Success = ReadPhoneBook(i, Nam, Number)
        If Nam <> "" Then
            List1.AddItem Nam & " " & Number
        Else
            Exit For
        End If
    Next
End Sub

Function ReadPhoneBook(Index, Nam, Number)
    'Create the message for reading the phone book
    SendBuffer = Chr(&H1F) + Chr(&H0) + Chr(&H1D) + Chr(&H40) _
        + Chr(&H0) + Chr(&H7) + Chr(&H0) + Chr(&H1) _
        + Chr(&H1F) + Chr(&H1) + Chr(&H4) + Chr(&H86) + Chr(Index)
    SendBuffer = SendBuffer + Chr(SeqNum) ' Sequence Number
    OutBuffer = SendBuffer + calcCheckSum(SendBuffer) ' Add checksum
    MSComm1.Output = OutBuffer

    ReadPhoneBook = False

    StartTime = Timer        ' For timeout
    Do
        DoEvents
        B = MSComm1.Input
        If Len(B) <> 0 Then
            B = StrConv(B, vbUnicode)
            'For i = 1 To Len(B)
            ' Text1.Text = Text1.Text & " " & Hex(Asc(Mid(B, i, 1)))
            'Next

            Buffer = Buffer & B
            Ind = InStr(Buffer, AckString)       ' Look for ACK from phone
            If Ind <> 0 Then Buffer = Mid(Buffer, Ind + 6)
                ' Look for phone book entry message
                If Left(Buffer, 4) = Chr(&H1F) + Chr(&H1D) + Chr(&H0) + Chr(&H40) Then
                    If Len(Buffer) > 15 Then
                    NameStart = InStr(15, Buffer, Chr(0))
                    NameEnd = InStr(NameStart + 1, Buffer, Chr(0))
                    If NameStart <> 0 And NameEnd <> 0 And Len(Buffer) = NameEnd + 2 Then
                        NameStart = NameStart + 1
                        Number = Mid(Buffer, 15, NameStart - 16)
                        Nam = Mid(Buffer, NameStart, InStr(NameStart, Buffer, Chr(0)) - NameStart)
                        ReadPhoneBook = True
                        ' Send ACK to phone
                        Seq = Asc(Mid(Buffer, NameEnd + 1, 1))
                        MSComm1.Output = PCAckString & Chr(Seq) & calcCheckSum(PCAckString & Chr(Seq))
                        ' Delay between each message
                        sleep 0.5
                        Exit Do
                    End If
                End If
            End If
        End If
        If Timer - StartTime > 0.5 Then
            Buffer = ""
            Text1.Text = Text1.Text & " TIMEOUT " & vbCrLf
            Exit Do
        End If
    Loop
    MSComm1.InBufferCount = 0
    'Text1.Text = Text1.Text & vbCrLf
End Function

'This function calculates the checksum byte
Function calcCheckSum(SendBuffer)
    CheckSum = Asc(Left(SendBuffer, 1))
    For i = 2 To Len(SendBuffer)
        CheckSum = CheckSum Xor Asc(Mid(SendBuffer, i, 1))
    Next
    calcCheckSum = Chr(CheckSum)
End Function

'This creates a delay
Sub sleep(numSecs)
    StartTime = Timer
    Do
        DoEvents
    Loop Until Timer - StartTime >= numSecs
End Sub



Home - Download - Library - About - Blog

Copyright 1999 - 2024, MazSoft.com