From: Thanushka on
Hi,

I am receiving a serial data frame stream to Matlab where the frames are 5 bytes long and the 1st byte is a SOF character of value 0x03. I am feeding this stream to a column matrix to be saved.
How can I ensure that the 1st saved element always is an SOF character?
Please help as I am a novice in Matlab and cant find a way to do this. Thanks for your time.
From: Walter Roberson on
Thanushka wrote:

> I am receiving a serial data frame stream to Matlab where the frames are
> 5 bytes long and the 1st byte is a SOF character of value 0x03. I am
> feeding this stream to a column matrix to be saved.
> How can I ensure that the 1st saved element always is an SOF character?
> Please help as I am a novice in Matlab and cant find a way to do this.

SOFpos = find(TheStream == char(3)); %where they are now

Then, if
diff(SOFpos) ~= 3
is true anywhere, you have some corrupted packet and can index back in
to SOFpos to figure out where and whether it is a packet too long or too
short.
From: Thanushka on
Walter Roberson <roberson(a)hushmail.com> wrote in message <PUaMn.71917$0B5.35999(a)newsfe05.iad>...
> Thanushka wrote:
>
> > I am receiving a serial data frame stream to Matlab where the frames are
> > 5 bytes long and the 1st byte is a SOF character of value 0x03. I am
> > feeding this stream to a column matrix to be saved.
> > How can I ensure that the 1st saved element always is an SOF character?
> > Please help as I am a novice in Matlab and cant find a way to do this.
>
> SOFpos = find(TheStream == char(3)); %where they are now
>
> Then, if
> diff(SOFpos) ~= 3
> is true anywhere, you have some corrupted packet and can index back in
> to SOFpos to figure out where and whether it is a packet too long or too
> short.

Ummmm......forgive me but can you please explain the above process? I'm not sure I understand what happens from these functions....

Thanks a lot for your time.
From: Walter Roberson on
Thanushka wrote:
> Walter Roberson <roberson(a)hushmail.com> wrote in message

>> SOFpos = find(TheStream == char(3)); %where they are now

>> Then, if
>> diff(SOFpos) ~= 3
>> is true anywhere, you have some corrupted packet and can index back in
>> to SOFpos to figure out where and whether it is a packet too long or
>> too short.

> Ummmm......forgive me but can you please explain the above process? I'm
> not sure I understand what happens from these functions....

The diff() should have been ~= 5 instead of ~= 3.


char(3) returns the character with numeric value 3.

TheStream == char(3) compares every character in TheStream to character
3 and returns false (0) where it does not match and true (1) where it
does match.

find() returns the indices of the elements that are non-zero, and thus
here would return the indices of where TheStream is the SOF character.

diff() takes the second element minus the first, the third minus the
second, the fourth minus the third, and so on to the end. In this
context it would thus be taking the differences of the indices of the
SOF characters. Where-ever the difference in indices is not 5, you have
a problem, so check for 5 using ~= 5 .
From: Thanushka on
Walter Roberson <roberson(a)hushmail.com> wrote in message <jUfMn.18429$7d5.118(a)newsfe17.iad>...
> Thanushka wrote:
> > Walter Roberson <roberson(a)hushmail.com> wrote in message
>
> >> SOFpos = find(TheStream == char(3)); %where they are now
>
> >> Then, if
> >> diff(SOFpos) ~= 3
> >> is true anywhere, you have some corrupted packet and can index back in
> >> to SOFpos to figure out where and whether it is a packet too long or
> >> too short.
>
> > Ummmm......forgive me but can you please explain the above process? I'm
> > not sure I understand what happens from these functions....
>
> The diff() should have been ~= 5 instead of ~= 3.
>
>
> char(3) returns the character with numeric value 3.
>
> TheStream == char(3) compares every character in TheStream to character
> 3 and returns false (0) where it does not match and true (1) where it
> does match.
>
> find() returns the indices of the elements that are non-zero, and thus
> here would return the indices of where TheStream is the SOF character.
>
> diff() takes the second element minus the first, the third minus the
> second, the fourth minus the third, and so on to the end. In this
> context it would thus be taking the differences of the indices of the
> SOF characters. Where-ever the difference in indices is not 5, you have
> a problem, so check for 5 using ~= 5 .

Thank you very much...!!!