G
Guest
Hello
I'm trying to read a stream of 4 byte messages in which the
4th byte is the checksum of the first 3 bytes:
Message= [byte#1, byte#2, byte#3, CRC8]
For this purpose I'm using
SerialPort.Read Method (Byte[], Int32, Int32)
(buffer=The byte array to write the input to.
offset=The offset in the buffer array to begin writing.
count=The number of bytes to read. )
to read 1-4 new bytes depending on wheter or not the read
operation is synchronized with the input stream. If the
read operation is unsynchronized, the read operation is shifted
1-3 bytes with respect to the input stream, and then
4 and 4 new bytes are read which is the normal situation when
the read operation is synchronized.
Example
Assume the 4 bytes
[n+1, n+2, n+3, n+4]
is read but the (valid) messages are located at
[n+2, n+3, n+4, n+5][n+6, n+7, n+8, n+9]
Then perform a shift and continue to read 4 and 4 new bytes.
This read algorithm is executed in a worker thread created by the
ThreadPool class.
This all works just fine for a couple of thousand read operations performed
at
9600 baud. In this time maximum 3 byte shifts are performed
to synchronize the read operation, and the read operation stays synchronized.
After a while however something wrong happens:
The read operation gets unsynchronized, and never manages to stay
synchronized again.
It almost seems as if the read operations are performed too slow, even
though the
baud rate is only 9600, and Win CE is running on a 500MHz CPU in a program
that
does nothing else than displaying the data read from the serial port in a
Windows
Form.
I first thought this was the case, and then called
serialPort.DiscardInBuffer()
frequently to ensure that the receive buffer did not experience a buffer
overflow.
However the same thing happened.
What could be the reason for this?
Is there any reason reading 4 and 4 bytes using SerialPort.Read Method
(Byte[], Int32, Int32)
should be (considerably) slower than reading 1 and 1 byte?
I'm trying to read a stream of 4 byte messages in which the
4th byte is the checksum of the first 3 bytes:
Message= [byte#1, byte#2, byte#3, CRC8]
For this purpose I'm using
SerialPort.Read Method (Byte[], Int32, Int32)
(buffer=The byte array to write the input to.
offset=The offset in the buffer array to begin writing.
count=The number of bytes to read. )
to read 1-4 new bytes depending on wheter or not the read
operation is synchronized with the input stream. If the
read operation is unsynchronized, the read operation is shifted
1-3 bytes with respect to the input stream, and then
4 and 4 new bytes are read which is the normal situation when
the read operation is synchronized.
Example
Assume the 4 bytes
[n+1, n+2, n+3, n+4]
is read but the (valid) messages are located at
[n+2, n+3, n+4, n+5][n+6, n+7, n+8, n+9]
Then perform a shift and continue to read 4 and 4 new bytes.
This read algorithm is executed in a worker thread created by the
ThreadPool class.
This all works just fine for a couple of thousand read operations performed
at
9600 baud. In this time maximum 3 byte shifts are performed
to synchronize the read operation, and the read operation stays synchronized.
After a while however something wrong happens:
The read operation gets unsynchronized, and never manages to stay
synchronized again.
It almost seems as if the read operations are performed too slow, even
though the
baud rate is only 9600, and Win CE is running on a 500MHz CPU in a program
that
does nothing else than displaying the data read from the serial port in a
Windows
Form.
I first thought this was the case, and then called
serialPort.DiscardInBuffer()
frequently to ensure that the receive buffer did not experience a buffer
overflow.
However the same thing happened.
What could be the reason for this?
Is there any reason reading 4 and 4 bytes using SerialPort.Read Method
(Byte[], Int32, Int32)
should be (considerably) slower than reading 1 and 1 byte?