B4J Question Asyncstreams NewData Arrives in 2 packets

tman

Member
Licensed User
Longtime User
In my project I use asyncstreams to send and recieive data. The incoming received data is only 4 bytes long. However, sometimes the 4 bytes are received in 2 "packets" such as 1 byte - followed by a 140 millisec delay then the remaining 3 bytes. The reason the delay occurs is the bytes are sent thru a radio system and the communication is not always perfect. When the 4 bytes are received all at once my app works perfectly. When the 4 bytes are separated by a slight delay I have a problem. The NewData event subroutine data buffer only gives me the the first packet. Is there a way for the NewData to wait 200 ms for the remaining bytes before the event is raised? Note: I cannot use prefix mode since I have no control over the device that is sending the 4 bytes.
 

barx

Well-Known Member
Licensed User
Longtime User
Hmmmmmm, wow. I was literally about to write pretty much the same question. I am creating a project, connecting to a device through rs232 serial. The data received by b4j should be a 51 byte packet but it always comes in 32 bytes then 19. Was wondering what the cause is and if possible to get it all at once. I believe our answers will be the same
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Here is what I have done so far to get around it and it seems to work for me.

B4X:
Private PVData(51) As Byte
Private PVDataCounter As Int = 0

B4X:
Sub astream_NewData (buffer() As Byte)
   
'Stack up the buffer until we have all bytes (51)
    For i = 0 To buffer.Length - 1
        PVData(PVDataCounter) = buffer(i)
        PVDataCounter = PVDataCounter + 1
    Next
           
    If PVDataCounter = 51 Then '51 is the amount of bytes I'm expecting
        PVDataCounter = 0
        'ProcessData
       
    End If
   
End Sub

No Idea if this is the right way to go about it, but it seemed the cleanest method to me and seems to work in my situation.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…