Strings merged recieved from BLE are getting merged

Hi, I am trying to get some string values into Android app via BLE from an Arduino device. My problem is that if I add the date and time stamp after receiving the strings, the entire row is being mixed up. I followed a topic from app inventor (https://groups.google.com/d/topic/mitappinventortest/mgWzEXB7gYE/discussion) to solve the issue. It is working well (I mean no merging of string values, they are nicely printed) when date and time stamp is not included in the blocks. But f I include the time stamp blocks, text is getting merged (please see below the text file contents below, I am not able to upload more than two links, so pasting the content here)

I am attaching here my blocks.

.

Text content is as following:

Feb 23, 20206:28:32 pmR264
V272,R272
V26Feb 23, 20206:28:36 pm0,R267
V260,R262
VFeb 23, 20206:28:39 pm262,R266
V260,R271
Feb 23, 20206:28:43 pm
V257,R266
V269,R26Feb 23, 20206:28:47 pm6
V259,R271
V262,RFeb 23, 20206:28:51 pm264
V257,R263
V258Feb 23, 20206:28:55 pm,R272

Notice that time stamping is not added to each of the strings received from the bluetooth device. Please let me know, how to solve this?

The transmitting code seems to be using \n (New line) to separate pairs of readings,
but the BLE software layer is delivering the data in fragments.

To reassemble the fragments, keep a global text variable inputBuffer,
initially empty text, and
in the for each item in list StringValues loop
set inputBuffer to JOIN(inputBuffer, item), (this undoes the fragmentation eventually)
then check if inputBuffer CONTAINS a \n in it. (See your text builtin blocks)
If \n is there, then
set local variable bufferParts to (split inputBuffer at first \n)
set local variable readingPair to select item 1 of local variable bufferParts
emit local variable readingPair to output per taste
set inputBuffer to item 2 of local variable bufferParts
else
do nothing with the inputBuffer, it has no complete message yet.

Thanks, ABG.

It seems working. Please see the attached blocks image to let me know, if I implemented your instructions correctly. Thanks a lot.

You threw away the reading pair after the \n split.
That was the part you should have logged.

If you Download Block as Image on specific blocks, those become edittable in AI2,
unlike your screen shot.

Hi ABG,

Thanks for correcting the mistake. I uploaded the blocks after taking your corrections into account.
I got the output as following. Please let me know, where I am doing wrong.

Feb 24, 2020,4:57:46 pm,V254,R258
51,R256
Feb 24, 2020,4:57:50 pm,V255,R259
V260,R255
Feb 24, 2020,4:57:55 pm,V266,R256
Feb 24, 2020,4:57:59 pm,V264,R255
58
Feb 24, 2020,4:58:03 pm,V257,R253
R255
Feb 24, 2020,4:58:07 pm,V255,R254
4,R256
Feb 24, 2020,4:58:09 pm,V255,R256
254,R254
Feb 24, 2020,4:58:13 pm,V254,R256

These are the editable blocks. Thanks for the tip.

Here’s a revised version.
Changes:
Expect possibility of multiple readings in buffer, so do them one at a time until exhausted.
Split at first \n
while loop instead of if/then, to get all whole readings
Added trailing \n to output

BTW, your date format internal comma will complicate your reading of your log later.

BLE_fragments.aia (159.7 KB)

global filename global inputBuffer global reading

All blocks are draggable.

It occurred to me that the reassembly and parsing loops don’t need to be nested.
Here’s a better version …

BLE_fragments.aia (159.6 KB)

Now, I am getting the following output. Just to be sure, I am uploading my blocks here

Feb 24, 2020,7:45:05 pm,V269,R255
254
Feb 24, 2020,7:45:09 pm,V266,R256
,R254
Feb 24, 2020,7:45:11 pm,V251,R254
55,R255
Feb 24, 2020,7:45:15 pm,V259,R258
V258,R255

.

split is not the same as split at first.

Thanks a lot, Abraham. It’s working finally.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.