Android Question Async - Prefix code procedure question

MolsonB

Member
Licensed User
Longtime User
With InitializePrefix code, it begins with the message length (INT 4 bytes). 0x20 0x00 0x00 0x00 for example.

What happens if the data stream receives another prefix code before it reaches the end of the 0x20 (32) bytes? Will it start over again, or keep on going until it reaches 32 bytes no matter what. What if it doesn't get the 32 bytes, does it timeout after xxxx and give up?

Just trying to wrap my head around which is the best way to send serial data from a PIC to Bluetooth.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
What happens if the data stream receives another prefix code before it reaches the end of the 0x20 (32) bytes?
The data sent is queued. So as long as you are sending the correct number of bytes it will work properly.

It cannot know that 4 other bytes are a beginning of a new message unless the previous message ended.

It will wait until either the connection is dropped or the full message arrived.

For another possible approach: [class] AsyncStreamsText - Useful when working with streams of text
 
Upvote 0

MolsonB

Member
Licensed User
Longtime User
The joys of programming.... I'm pretty sure everyone of us has had night like this (or daily it seems).

I gave up on the prefix mode because the data would buffer and overwrite some of the transmission, my checksum's would never work, the tablet couldn't keep up with the baud rate, slow refresh rates, etc. It was just a mess, so I decided to try and write my own astream prefix code with lists and header bytes. It worked great when debugging 1 step at a time, but would have a meltdown when running normal speed.

Turns out my liquid cooler CPU was leaking onto my videocard, which was giving me funky results on the screen. WHICH in-turn, the CPU was starting to overheat causing the slow transmission and weird bottleneck issues. What a day :S

I went back and converted my code back to InitializePrefix. Seems to run much better now. I'll be streaming data non-stop, so I'm playing around trying to make the Service fail proof. Reconnect if it fails or detects any errors. The only weird thing I've found between debug mode vs Release mode, release mode I had to cast the int in the List(SerialData) to Chr. Weird.

B4X:
Debug Mode = If SerialData.Get(0) = 68 Then

Release Mode  =   If Chr(SerialData.Get(0)) = "D" Then
 
Upvote 0

MolsonB

Member
Licensed User
Longtime User
*Edit. Ignore. Converted the microcontroller to send ASCII numbers instead, and using AsyncStreamsText. Speeds seem fine on both ends.


You won't share how you did the InitializePrefix code will you? It's very fast and efficient, I just wish we could add our own Prefix to the front or back of the Prefix size to make it more unique. <int>,0,0,0 is too common in my serial stream. The bluetooth could connect mid stream, and catch that common prefix and error out. Right now I catch it in Asteam_Error and re-connect. Once I catch it at the right time after awhile, it syncs up and works perfect. Until you disconnect, walk away, come back and try to sync up again.

Anytime I try to write my own code, the buffer can't keep up and only processes every few seconds, hangs, processes the last one if it's not mixed up.
I'm sending all hex bits, as I don't want to convert to ASCII to save baud rate space and PIC controller time. But if it comes down to it, I can try and see how AsyncStreamsText works if I convert to send ASCII instead from the PIC.

The header I came up with Header(byte) + Size(byte) + ID(bit) + Payload + CheckSum(bit) 0xFEFFFEFF + 0x20000000 + 0x02 + Payload + 0x55
B4X:
Private Sub astreams_NewData (Buffer() As Byte)

    Dim Val As Int
    Bytes.AddAll(Buffer)
  
    For i=0 To Bytes.Size
        i=0      
        If Header Or Bytes.Size >= 9 Then     'header 4 bits + size 4 bits + id 1 bit
          
            If Bytes.Size >= 9 And Bytes.Get(0) = 0xFFFFFFFF And Bytes.Get(1) = 0xFFFFFFFE And Bytes.Get(2) = 0xFFFFFFFF And Bytes.Get(3) = 0xFFFFFFFE Then        'helps with signed character to unsigned which is what i send
                HeaderSize = 0
                For ii=3 To 0 Step - 1            'Next four bits are the size
                       HeaderSize = HeaderSize * 256 + Bit.And(Bytes.Get(4+ii), 255)
                Next
                HeaderSize = HeaderSize / 4        'PIC sends the number of bits, we are only saving the bytes
                Header = True
                Data.Initialize
                Data.Add(Bytes.Get(8))
                For ii=0 To 8        'Remove 9 header bits from buffer
                    Bytes.RemoveAt(0)
                Next
            End If
      
            If Header And Bytes.Size >=4 Then     'Take in 4 bits (1 byte) at a time
                Val = 0
                For ii=3 To 0 Step - 1
                       Val = Val * 256 + Bit.And(Bytes.Get(0+ii), 255)
                    Bytes.RemoveAt(0+ii)
                Next
                Data.Add(Val)
            Else If Header And Data.Size-1 >= HeaderSize Then    'Last bit is checksum
                Data.Add(Bytes.Get(0))  
                CallSubDelayed2(mTarget, mEventName & "_NewText", Data)  
                Header = False
                Bytes.RemoveAt(0)
            Else
                If Header Then
                    Exit                'Need more buffer
                Else
                    Bytes.RemoveAt(0)    'No header found, just remove
                End If
            End If
        Else
            Exit
        End If
    Next

End Sub
 
Last edited:
Upvote 0
Top