1

I'm currently reading the serial port via .BaseStream.Read(), but I noticed this in the documentation:

Because the SerialPort class buffers data, and the stream contained in the BaseStream property does not, the two might conflict about how many bytes are available to read. The BytesToRead property can indicate that there are bytes to read, but these bytes might not be accessible to the stream contained in the BaseStream property because they have been buffered to the SerialPort class.

While I don't use BytesToRead, the latter part of the above concerns me a bit, as the wording suggests that data can be "missed" when reading the BaseStream directly, i.e. it is no loner in the stream and is now in the SerialPort's buffer. So should I be using SerialPort.Read() instead?

1 Answer 1

1

BytesToRead adds together the streams's BytesToRead and the CachedBytesToRead. It is definitely the case that you will miss data if you buffer into SerialPort and then try to read the BasStream.

But the Open() function does not buffer any data, as you can see from the code. Only the Read functions buffer.

So, as long as you don't actually use any of the Read functions on SerialPort, it should be safe to use BaseStream alone. Just don't mix and match. It's a shame that SerialStream is not exposed as an API, and we need to go through SerialPort to do this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.