Suppose I get from the network a sequence of bytes in the following order:
X X X X = X X X X = X X X X = … and so on
you can notice immediately the pattern (a message is a sequence of 5 bytes: “X X X X =
X can be any number except ‘=’ of course)
Now, the transmission occur periodically.
In some point my application is starting and getting the bytes sequence
What is the best algorithm for taking those bytes and framing them to 5 bytes messages?
There are two problems:
1. How do you find the first message? I guess I need to drop some bytes till I get ‘=’ right?
2. How the application suppose to handle a pause of transimmision and a new start.
The simplest way is to use a blocking read (which is the most common because its the simplest) to read 5 bytes at a time.
Normally, you wouldn’t assume you have junk data at the start, and just read everything. If you have a header, I would read that too.