From: Thanushka on
"Thanushka " <galazz442(a)gmail.com> wrote in message <httf2q$qs$1(a)fred.mathworks.com>...
> 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...!!!

Sorry but I maybe did not clarify enough. I don't get any corrupted packets at all. The SOF character appears every five characters without fail. The problem I have is to have the first SOF character I receive as the first element of the column vector. Mind that the SOF character 0x03 can also appear in the middle of a dataframe. Can I achieve this with serial port settings itself or do i have to use an alogorithm to check the data stream??
Thanks in advance for your time.
From: Walter Roberson on
Thanushka wrote:

> Sorry but I maybe did not clarify enough. I don't get any corrupted
> packets at all. The SOF character appears every five characters without
> fail. The problem I have is to have the first SOF character I receive as
> the first element of the column vector. Mind that the SOF character 0x03
> can also appear in the middle of a dataframe. Can I achieve this with
> serial port settings itself or do i have to use an alogorithm to check
> the data stream??

I think I might understand now: is this a question about how to synchronize to
the stream, when the synchronization character (SOF) can also occur in the packet?

If so, then start with the first character you have and proceed to the newer
characters. When you find an SOF character, remember its position and keep
going. When you find a second SOF character and the previous SOF character was
5 bytes previous, then the previous SOF character marked the beginning of a
packet and the current character marks the beginning of another packet, but if
the second SOF character is not 5 bytes after the first one, then remember its
position instead and keep going. Eventually you will either get to the end of
your data or you will have identified the start of a packet; then as the
packet delivery is considered reliable, back up in groups of 5 until you get
to within 5 characters of the beginning of your data; if that new position is
not an SOF character than you do _not_ have reliable packet delivery, but if
it is indeed SOF then that new position within 5 of the beginning is the start
of the first full packet that you have in your data.

Something like,

mod( find(diff(find(Characters == char(3))) == 5, 1, 'first') - 1 , 5) + 1