B4J Question A little help with AStream_NewData

I am new to B4J and I am having trouble understanding AStream_NewData. I understand what it is for and I have it working (I think). But I don't know why and thus I cannot trust it.
Say at time T0 I send 33 bytes of data from a microcontroller to a PC via a USB-Serial port operating at 57.6K. Sub AStream_NewData picks it up, reads the 33 bytes and creates a desired text string from those bytes. In "Sub AStream_NewData" I used no end of line, I had no prefix, nothing to end the NewData. Two seconds later at T2 I again send the same 33 bytes via the USB-Serial port to the PC. I get the same 33 bytes and the same string. Why????? Why isn't it 66 bytes? Why didn't AStream_NewData just keep creating a longer byte array? Why did it "reset" to start byte collection over?
Now, lets say I want to "force" a reset after exactly 33 bytes. How can I terminate the incoming byte array immediately after 33 bytes.
Now, say I want to end the NewData array when a particular symbol comes in. Say when a "!" is received. How do I do that? I cannot use the traditional CRLF because I am using byte codes 10 and 13 for other purposes.
And lets say I know I am sending exactly 33 bytes. Can I just receive the first incoming byte and then go to "sleep" until such time that I can be sure that all 33 bytes have been received?
So what automatically terminates the NewData byte string (if anything)?
Okay, as I understand the matter (and this may be totally wrong) I would need to attach a 4 byte number to use AStream in prefix mode. So, if I will be sending 33 bytes I would need to send 0x00+0x00+0x00+0x21 (in hex), right. Or do I? Can I just add a prefix 0x21 and then send the 33 bytes?
Finally, I could use the help of an Alpha tester. If you are a maker, use 830 point boards, and would like to use either your PC and/or Android device as readout let me know.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why????? Why isn't it 66 bytes? Why didn't AStream_NewData just keep creating a longer byte array? Why did it "reset" to start byte collection over?
Because this is how it is designed to work. In non-prefix mode, it doesn't buffer anything. It raises an event with the new data whenever such data is available.

You can very easily collect the data yourself. For raw bytes it is best to use B4XBytesBuilder from B4XCollections.
If you are working with text data then use AsyncStreamsText. You can change the end of message character to whatever you like.

If the message size is constant then use B4XBytesBuilder. Should be simple.
 
Upvote 1
Top