Android Question HC-06 Bluetooth Async Buffer

RichardHirst

Member
Licensed User
Longtime User
Hello Erel

I have a WIT-motion device which give Roll/Pitch/Yaw data via bluetooth HC-06.

11 bytes, start with 0X55 with second byte as data type 0x53 is RPY. Byte 11 is CRC. This is not Prefix mode so I have used a ring buffer to find the data block.

This code working but seems very slow, can you suggest a better method to 'find' the data in the buffer stream.

Also when you 'read' or copy from the Async Buffer, does this clear the Async Buffer..?


B4X:
Sub AStream_NewData (Buffer() As Byte)
    
    Dim i As Int = 0

    usLength = Buffer.Length
    usRxLength = usRxLength + usLength

    Bit.ArrayCopy(Buffer, 0, RxBuffer, usRxLength, usLength)

    Do While usRxLength >= 11
            
        If (Not ((RxBuffer(0) = 0x55) And (Bit.And(RxBuffer(1), 0x50) = 0x50))) Then
            'Move Byte to the front un till Byte(0) = 0x55 and Byte(1) is Above 0x50
            For i = 1 To usRxLength - 1
                RxBuffer(i - 1) = RxBuffer(i)
            Next
            usRxLength = usRxLength - 1
            Continue
        End If
                
        If Bit.And(RxBuffer(0) + RxBuffer(1) + RxBuffer(2) + RxBuffer(3) + RxBuffer(4) + RxBuffer(5) _
                + RxBuffer(6) + RxBuffer(7) + RxBuffer(8) + RxBuffer(9), 0xfF) = RxBuffer(10) Then
                
            If RxBuffer(1) = 0x53 Then
                Dim Roll As Double = (((RxBuffer(3) * 256) + RxBuffer(2)) / 32768) * 180
                Dim Pitch As Double = (((RxBuffer(5) * 256) + RxBuffer(4)) / 32768) * 180
                Dim Yaw As Double = (((RxBuffer(7) * 256) + RxBuffer(6)) / 32768) * 180
                Dim Temperature As Double =  ((RxBuffer(9) * 256) + RxBuffer(8)) / 340 + 36.53

                Log($"T = $1.0{Temperature}"$ & TAB & TAB & $"R = $1.2{Roll}"$ & TAB & TAB & $"P = $1.2{Pitch}"$ & TAB & TAB & $"Y = $1.2{Yaw}"$)
            End If

        End If
            
        ' Remove 'Used' Data from Ring Buffer
        For i = 11 To usRxLength - 1
            RxBuffer(i - 11) = RxBuffer(i)
        Next
        usRxLength = usRxLength - 11
    Loop
End Sub

Many Thanks

Richard
 

RichardHirst

Member
Licensed User
Longtime User
So this is working a lot better.

B4X:
Sub AStream_NewData (Buffer() As Byte)

    bb.Append(Buffer)

    Do While bb.Length >= 11

        DataIndex = bb.IndexOf2(Array As Byte(0x55, 0x53), 0)
        
        If DataIndex = -1 Then
            bb.Clear
        Else
            RxBuffer = bb.SubArray2(DataIndex, DataIndex + 11)
            
            If Bit.And(RxBuffer(0) + RxBuffer(1) + RxBuffer(2) + RxBuffer(3) + RxBuffer(4) + RxBuffer(5) _
                    + RxBuffer(6) + RxBuffer(7) + RxBuffer(8) + RxBuffer(9), 0xfF) = RxBuffer(10) Then
                    
                Dim Roll As Double = (((RxBuffer(3) * 256) + RxBuffer(2)) / 32768) * 180
                Dim Pitch As Double = (((RxBuffer(5) * 256) + RxBuffer(4)) / 32768) * 180
                Dim Yaw As Double = (((RxBuffer(7) * 256) + RxBuffer(6)) / 32768) * 180
        '        Dim Temperature As Double =  ((RxBuffer(9) * 256) + RxBuffer(8)) / 340 + 36.53

                lblRoll.Text = NumberFormat2(Roll, 1, 2, 2, False)
                lblPitch.Text = NumberFormat2(Pitch, 1, 2, 2, False)
                lblYaw.Text = NumberFormat2(Yaw, 1, 2, 2, False)
                
            End If
            
            bb.Remove(DataIndex, DataIndex + 11)
        End If
    Loop
End Sub

Thank You for all your Help.

Richard
 
Upvote 0
Top