serial string parsing

429 views
Skip to first unread message

Ede Rancz

unread,
Mar 25, 2018, 9:53:54 AM3/25/18
to Bonsai Users
Hi Goncalo and others, 

I have a SerialStringRead producing a sequence of letters (e or d) and numbers (numbers can be anything 0-9999, not zero padded). The sequence is produced by an arduino using Serial.println(), so values are followed by carriage return and new line (ASCII13 and 10).

Looks like this: d 0 9 0 0 12 e 120 9 0 where space represents cd+ln in the serial signal.
 
I would like to catch letters (then convert e to 1 and d to 0 to feed into another part of the code) and also catch numbers (then timestamp and save them). 
I suspect parse and condition are the things I am looking for (or input / property mapping?) but can't make it work. 

Any tips? 
Ede

Gonçalo Lopes

unread,
Mar 25, 2018, 8:29:26 PM3/25/18
to Ede Rancz, Bonsai Users
Hi Ede,

Can you give an example of what you tried with the Parse operator? When you say that "space represents cd+ln in the serial signal", do you mean that you use println to separate every single value? Are letters and numbers intermingled in the stream? It looks like the way the protocol is designed makes it much harder to parse, so the first think I would consider is whether it could be changed to make it easier. Can you use negative numbers to represent events, rather than letters? That would allow you to parse everything as signed integer values and simply check for negative and positive numbers to separate the two streams.

Otherwise, you may have to parse things manually inside a PythonTransform or ExpressionTransform, since I don't think Parse is flexible enough for this (it was designed for fixed protocol streams). You might be able to separate the two streams with a Condition using the Char.IsLetter(c) method to test the first character coming through and then applying Parse depending on the result.

However, some clarification and example attempt would be useful to clarify what is being assumed here.


--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/be4a8cf9-8e26-4fce-8fdf-be2738d6ad25%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ede Rancz

unread,
Mar 28, 2018, 7:15:29 PM3/28/18
to Bonsai Users
Hi Goncalo, 

On Monday, March 26, 2018 at 1:29:26 AM UTC+1, goncaloclopes wrote:
Hi Ede,

Can you give an example of what you tried with the Parse operator? When you say that "space represents cd+ln in the serial signal", do you mean that you use println to separate every single value?

yes 
 
Are letters and numbers intermingled in the stream?

yes 
 
It looks like the way the protocol is designed makes it much harder to parse, so the first think I would consider is whether it could be changed to make it easier. Can you use negative numbers to represent events, rather than letters? That would allow you to parse everything as signed integer values and simply check for negative and positive numbers to separate the two streams.

great idea! i did just that and I am halfway there. Basically my enable / disable signal is sorted with -1 and -2 made into a boolean with LessThan (see attached screenshot) 

I am struggling though how to handle the rest of the sequence. I basically would like a parallel stream which only contains the positive integer numbers. Now I can't have another SerialRead reading the same data and I don't seem to be able to connect anything (Like a GreaterThenOrEqual) in a way which would split the stream. I am sure the solution is easy, I just still find it hard to think in the Bonsai way. Let me know if you have any tips. 

Thanks,
Ede

 

Otherwise, you may have to parse things manually inside a PythonTransform or ExpressionTransform, since I don't think Parse is flexible enough for this (it was designed for fixed protocol streams). You might be able to separate the two streams with a Condition using the Char.IsLetter(c) method to test the first character coming through and then applying Parse depending on the result.

However, some clarification and example attempt would be useful to clarify what is being assumed here.

On 25 March 2018 at 14:53, Ede Rancz <eder...@gmail.com> wrote:
Hi Goncalo and others, 

I have a SerialStringRead producing a sequence of letters (e or d) and numbers (numbers can be anything 0-9999, not zero padded). The sequence is produced by an arduino using Serial.println(), so values are followed by carriage return and new line (ASCII13 and 10).

Looks like this: d 0 9 0 0 12 e 120 9 0 where space represents cd+ln in the serial signal.
 
I would like to catch letters (then convert e to 1 and d to 0 to feed into another part of the code) and also catch numbers (then timestamp and save them). 
I suspect parse and condition are the things I am looking for (or input / property mapping?) but can't make it work. 

Any tips? 
Ede

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
Worflow_pic.PNG

Gonçalo Lopes

unread,
Mar 30, 2018, 4:00:26 AM3/30/18
to Ede Rancz, Bonsai Users
Hi Ede,

You can solve this by creating a parallel processing branch. If you right-click on any operator in the toolbox, you should see the option "Create Branch" show up. Also, don't forget that LessThan or GreaterThan only transform the data into a sequence of True/False, but don't actually filter the data (i.e. even if the result of Parse is negative, you still get an output saying False).

If you actually want to drop invalid messages (as would be the case if you are trying to demix the two streams), you need to place the LessThan/GreatherThan inside a Condition node. You can do this by right-clicking the LessThan and selecting Group > Condition.

Hope this helps.

To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.

Gonçalo Lopes

unread,
Mar 30, 2018, 2:36:01 PM3/30/18
to João Frazão, Ede Rancz, Bonsai Users
Haha, you are completely correct, of course! Inductive programming at its best.

In fact, this may be even better in future versions of .NET where you don't even need to copy a string in order to split it.

On 30 March 2018 at 11:10, João Frazão <joao....@neuro.fchampalimaud.org> wrote:
Well Gonçalo I think parse is more flexlible than it looks at first time. You can also combine several Parse nodes in a bonsai workflow. So that you can solve more complex protocols. For instance You can parse the first part of the message as a integer followed by a string, where the string has the rest of the message. And then based on the value of the integer you can them parse the rest of the message using a branch where in each branch you use addicional parse nodes to parse the remaining string (the remaining of the message)

João Frazão

unread,
Apr 3, 2018, 9:45:59 AM4/3/18
to Gonçalo Lopes, Ede Rancz, Bonsai Users
Well Gonçalo I think parse is more flexlible than it looks at first time. You can also combine several Parse nodes in a bonsai workflow. So that you can solve more complex protocols. For instance You can parse the first part of the message as a integer followed by a string, where the string has the rest of the message. And then based on the value of the integer you can them parse the rest of the message using a branch where in each branch you use addicional parse nodes to parse the remaining string (the remaining of the message)
On Mar 30, 2018 9:00 AM, "Gonçalo Lopes" <goncal...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
Reply all
Reply to author
Forward
0 new messages