From: WU Wai on
Hallo everyone,

I am using MATLAB/Simulink/Stateflow... to build model to process serial
data, the model should be compiled (Real-Time Workshop) and runs on
real-time processor later. But I am not quite sure which is the most
appropriate way to realize it.

The situation is following:

By every sample time there are, say, 20 bytes data consisting of fixed
datagrams coming in from the serial interface, and each datagram is 10
bytes long, meanwhile the first two bytes of each datagram are fixed and
known. The problem is, the beginning byte of the datagram could be
anyone of the 20 received bytes at each sample time. So it is necessary
to determine the position of the beginning two bytes of each datagram.

I suppose using c-code s-function could be the most powerful/flexible to
realize the search of the two specific bytes, I could do this, but
obviously not everyone likes to confront c code (well, I made this model
for someone else), from my opinion c s-function is not easy to
understand for s-function-unexperienced user as well. So is there any
other way to do the same job, like using embedded MATLAB function, or
even Stateflow?

Thanks in advance.
Wai
From: Walter Roberson on
WU Wai wrote:

> The situation is following:
>
> By every sample time there are, say, 20 bytes data consisting of fixed
> datagrams coming in from the serial interface, and each datagram is 10
> bytes long, meanwhile the first two bytes of each datagram are fixed and
> known. The problem is, the beginning byte of the datagram could be
> anyone of the 20 received bytes at each sample time. So it is necessary
> to determine the position of the beginning two bytes of each datagram.
>
> I suppose using c-code s-function could be the most powerful/flexible to
> realize the search of the two specific bytes, I could do this, but
> obviously not everyone likes to confront c code (well, I made this model
> for someone else), from my opinion c s-function is not easy to
> understand for s-function-unexperienced user as well. So is there any
> other way to do the same job, like using embedded MATLAB function, or
> even Stateflow?

You have a problem that there could be 0 to 2 complete datagrams to be
located. 0 if the first key byte is from offset 12 onward (because there would
not be 10 complete bytes of datagram); 2 if the datagram is recognized
starting from offset 1 (in which case the datagram starts at offset 11); and 1
complete datagram otherwise.

Simulink is for functions in which each signal is the _same_ size.


You have an potential second problem that the 2 key bytes might *happen* to
occur as payload bytes in the datagram and thus be matched in error.
From: WU Wai on
On 28.07.2010 22:59, Walter Roberson wrote:
> WU Wai wrote:
>
>> The situation is following:
>>
>> By every sample time there are, say, 20 bytes data consisting of fixed
>> datagrams coming in from the serial interface, and each datagram is 10
>> bytes long, meanwhile the first two bytes of each datagram are fixed
>> and known. The problem is, the beginning byte of the datagram could be
>> anyone of the 20 received bytes at each sample time. So it is
>> necessary to determine the position of the beginning two bytes of each
>> datagram.
>>
>> I suppose using c-code s-function could be the most powerful/flexible
>> to realize the search of the two specific bytes, I could do this, but
>> obviously not everyone likes to confront c code (well, I made this
>> model for someone else), from my opinion c s-function is not easy to
>> understand for s-function-unexperienced user as well. So is there any
>> other way to do the same job, like using embedded MATLAB function, or
>> even Stateflow?
>
> You have a problem that there could be 0 to 2 complete datagrams to be
> located. 0 if the first key byte is from offset 12 onward (because there
> would not be 10 complete bytes of datagram); 2 if the datagram is
> recognized starting from offset 1 (in which case the datagram starts at
> offset 11); and 1 complete datagram otherwise.
>
> Simulink is for functions in which each signal is the _same_ size.
>
>
> You have an potential second problem that the 2 key bytes might *happen*
> to occur as payload bytes in the datagram and thus be matched in error.

Thanks for replying.

It doesn't matter whether there is(are) complete datagram(s). As first
step I need to do is to check the existence(s) of the two key bytes, and
at each time step the key byte(s) should be observed at least once among
the 20 received bytes.
When I could observe the two same bytes one after another repeatedly
(e.g. 15 times), and they always come in in the same distance (i.e. 10
bytes), then I may conclude that the two bytes must be the key bytes,
that means also the input data is OK and now I know where the use data
are (they come in after the two key bytes) and may do the rest data
processing.

The question is, whether I could realize this without using s-function?
From: Walter Roberson on
WU Wai wrote:

> It doesn't matter whether there is(are) complete datagram(s). As first
> step I need to do is to check the existence(s) of the two key bytes, and
> at each time step the key byte(s) should be observed at least once among
> the 20 received bytes.
> When I could observe the two same bytes one after another repeatedly
> (e.g. 15 times), and they always come in in the same distance (i.e. 10
> bytes), then I may conclude that the two bytes must be the key bytes,
> that means also the input data is OK and now I know where the use data
> are (they come in after the two key bytes) and may do the rest data
> processing.
>
> The question is, whether I could realize this without using s-function?

Unless I've programmed the protocol myself, I never trust that key
header bytes *cannot* occur in the payload.

I showed how to do robust header detection in a posting a few weeks ago,
http://www.mathworks.com/matlabcentral/newsreader/view_thread/286187#760227

If you are _sure_ that the two bytes occur only in the header and
no-where else, then finding them is fairly easy, such as with strmatch,
and the only trick might be in confining yourself to just the first
match using only functions available in embedded matlab. But a for-loop
is easy enough:

foundit = false;
for K = 1 : 19
if Data(K) == FirstByte & Data(K+1) == SecondByte
foundit = true;
break;
end
end