Split a string and store the separate snipsel

Hi Guys,
I have a problem and cannot find a solution … but you determine:
In my app I receive some measured values as numbers collected in a string via Bluetooth from an Arduino.
These respective numbers are separated in the string with “:” and can be 1, 2, 3 or 4 digits long.
Example:
223:334:412 or 98:1223:9 or 332:1:49 etc.
How can I split the string and store the individual numbers separately in different variables etc.?

Best wishes and regards from Germany
Carsten

Use the “split text at” block in the text palette
will split at the colon (:) and make a list of the elements

blocks (12)

Hi and may thanks for reply,
I have already tried all of this functions and then get the result (as sample):
[“432”, “99”, “1002”] in and from this moment on I don’t know what to do …
Sorry, I haven’t worked with lists etc. yet.
I just have to store the 432 in a varibale “a”, store the 99 in a varibale “b”, and store the 1002 in a variable “c”.
How does it work ?

Learn about using lists:

General Tutorials

Hi again,
thanks for the hint … I had already read the tutorial … but I have no idea how I have to implement this in order to save from [“432”, “99”, “1002”] into individual variables . (“I have a knot in my head”).
The problem is, that the numbers can have different numbers of digits … and I can’t get that solved at all.
Do you have a tip for me?

When you “split text at” you actually receive a list [“432”, “99”, “1002”] . See in my example how to work with that list . The digits in the numbers won’t be a problem since you split at :

1 Like

WOW !!! that looks very good…i will test it this evening…1000 TANKS to you!!!

1 Like

why using individual variables? just use the select list item blocks to get the values you need...
Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

Hi … it works wonderfully … but sometimes this error message comes up

.
image

The excess length can possibly also be caused by incorrect values from Arduino … I will leave it like this until everything is ready.
Thanks again and good night!!!

hmm good work :+1: :smiley: :+1:

1 Like

Hello again,
pity … it works so well, but then I increased the quantity of numbers from 3 to about 11 (see red number series) … and then the crash comes from the list.
How can I solve the problem ?



Do you have another list in your app somewhere ?

Hello…no, this the only one in the app.

This is a BlueTooth Delimiter Required problem.
Your list text is being clipped by the Clock.

(boiler plate):

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.

Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check
Is the BlueTooth Client still Connected?
Is Bytes Available > 0?
IF Bytes Available > 0 THEN
set message var to BT.ReceiveText(-1)

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Wow, Okay, there are a lot of changes that I have to program now … I will then tell you the result … thanks

Here I am,
Many thanks to everyone who showed me how to solve the problem.
It works great now and I can increase the number of variables at any time ..... actually very easy if you understand it.
Enclosed the information:
image
image
image


and here the function in arduino to send data.
It is important to signal the end of the line at the end of the data record.

image

Thanks again and see you soon :slight_smile:
Carsten

1 Like

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