Parsing Sensor Data using matlab in real time

I need to parse a serial string coming from a microcontroller using a byte stuffing. This is an example of the inputbuffer:
FIRST SENSOR START: 2-2
SECOND SENSOR START: 1-1
ESC: 12
2-2-6samples(FIRST SENSOR)-1-1-60samples(SECOND SENSOR)-2-2-..etc...
I need to parse this signal using matlab in real time but I've got some problems: 1) since I'm using the byte stuffing technique, the size of the message for each sensor could change so is hard to define a parse function based on the number of samples received 2)I'm not able to parse this data in real time without using a for loop, at the moment I'm using this function:
payload(1:1750,1) = fread(WaveNeck,1750);
accstart=find(payload==2)
loading the index of the number 2-2 and doing the same thing for 1-1 using these index to separate the message but this is not working and is a slow code. Moreover if a 1 is in the message the system gives wrong results.
I was thinking to move to out = regexp(str,expression,outkey) is it a goo idea?? There are other useful functions for a realtime study?
Is there any way to find elements of an array followed by an other? in my case all 1 followed by 1?

3 Comments

I do not understand what you are trying to convey with
2-2-6samples(FIRST SENSOR)-1-1-60samples(SECOND SENSOR)-2-2-..etc...
Are you sure you want to be searching for ==2 and not for =='2' ?
What is the value that you are avoiding writing into the stream? Which byte stuffing protocol are you using? Is char(12) the escape from the protocol?
I'm trying to convey samples from different sensors using a different sampling rate (300Hz and 3000Hz). I create this protocol to communicate the samples using two start flag composed by two bytes (2-2 and 1-1) . To avoid misunderstanding in the message every 1 and 2 are preceded by 12(Escape byte) so that 1- 1 and 2-2 are only start flag. which is the difference between ==2 and =='2'?
Have a look at
disp(char(48:57))
disp('0123456789' + 0)
disp(char(2))
disp(['Here is quoted 2: |', '2', '|'])
disp(['Here is char(2): |', char(2), '|'])
The question becomes whether you are dealing with bytes whose binary value is decimal 2, or if you are dealing with bytes whose character is '2' which is represented as decimal 50
It looks to me like perhaps you are working in binary, that your protocol is
char(2) char(2) char(6) ... stuffed binary values for first sensor
char(1) char(1) char(60) ... stuffed binary values for second sensor
with the stuffing protocol being to replace char(2) char(2) with char(12) char(2) char(2) and to replace char(1) char(1) with char(12) char(1) char(1) -- with individual occurrences of char(1) or char(2) or char(1) followed by char(2) not altered, only pairs char(1) char(1) and char(2) char(2), and with the stuffed values being uncounted (in number) and assumed to continue until the escape sequence was encountered.
and possibly your entire header is in text
'FIRST SENSOR START: 2-2' char(10)
'SECOND SENSOR START: 1-1' char(10)
'ESC: 12' char(10)
per perhaps char(13) char(10) instead of just char(10)
After which there would be a stream of 8 bit binary?
Is there an end-of-samples sequence, after which you would go back to the text intro? Or is there an end-of-transmission sequence?
What would happen if char(2) char(2) was followed by char(60) instead of char(6) ?
Do you control this protocol?

Sign in to comment.

Answers (0)

Asked:

on 30 Jan 2016

Commented:

on 30 Jan 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!