reading data packets with AsyncStream

theos

Member
Licensed User
Longtime User
Dear Developers,
I would like to ask you a suggestion on how to proceed in my work, and propose/ask a new method for AsyncStream.

I am using a bluetooth device that sends out a lot of data packets continuously.
Each data packet is composed by a StartOfFrame (a couple of bytes 'AA') and 8 bytes of binary data.
I would like to use the AsyncStreams class to read them, instead of a
Timer.
Reading the manual it seems that I can't use the InitializePrefix function, because AA is not the right number of the following bytes.
I can't use neither the Initialize method since I am not sure that the first 2 bytes corresponds to 'AA' after the bluetooth connection.

So:
FIRST: I would like to ask you if there is a way to "sync" with the 'AA' bytes placing them at the start of the message (for example, taking out some bytes in excess from the serial channel before opening and initializing the stream).
SECOND: It would be useful to have an additional "general purpose" method for initializing the class, in which you can specify:
- the presence of a SOF (start of frame)
- the string eventually corresponding to the start of frame
- the presence of an EOF (end of frame)
- the string eventually corresponding to the end of frame
- the maximum number of chars that can be readed
These second option can be found in a lot of C++ classes used to manage serial communications, for instance the DataPacket class of "AsyncPro".
thank you in advance for your opinion

theos
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I will consider your suggestion.

For now my first suggested approach is:
- Create a global List object.
- Each time that there is new data, add the array to the list (with AddAll). Go over the bytes in the list:
- Look for the first A followed by A bytes.
- Check if there are additional 8 bytes in the list. If there are 8 bytes, then you have a complete message.
- Handle the message and remove all read bytes.
- If there aren't additional 8 bytes (or if you didn't find A followed by A) then don't do anything and wait for the next event.

I expect this solution to work quickly enough as your messages are short.
 
Top