Concatenating serial buffers

mjcoon

Well-Known Member
Licensed User
I've been asking questions about serial input and arrays and am now seeking clarification of the process of adding subsequently-arrived bytes to those already received.

It is handy to have the array size set exactly right for the bytes in the serial buffer. But when I get more and wish to add them I find that the ArrayCopy() function needs a target array size large enough for both sets of bytes.

Does this mean that, because if I re-Dim the original array I lose its contents, I have to create a new array of the total size needed, do two ArrayCopy operations to load the two sequences of bytes, and then assign the new array back into the old buffer so that the rest of the code can continue as if all bytes had arrived together?

It seems a bit like hard work for both me and for the device, though admittedly only a few tens of bytes are involved at any one time.

Regards, Mike.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It seems a bit like hard work for both me and for the device, though admittedly only a few tens of bytes are involved at any one time.
Another solution is to create one large array and one small array.
Read the data from the serial port with the small array and copy the data to the large array.
Use another global variable to store the last position in the large array.

Make sure to declare both arrays types as byte.
 
Top