Indicating the end of a data stream

The second and third examples in Serial Input Basics may give you some ideas.

...R