From a8114e95fa012b33b0f0d299311235e304d00cf8 Mon Sep 17 00:00:00 2001 From: mike w Date: Sun, 11 Sep 2016 16:48:19 -0400 Subject: [PATCH] Updated Troubleshooting (markdown) --- Troubleshooting.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Troubleshooting.md b/Troubleshooting.md index 2161333..076d1cc 100644 --- a/Troubleshooting.md +++ b/Troubleshooting.md @@ -16,4 +16,17 @@ You don't; see previous question. `/dev/tty*` is the Linux kernel's driver inte ## I am using an Arduino Uno, Sparkfun Pro Micro, or other Arduino and "if (Serial)" doesn't work. -Some Arduinos use the DTR line to determine serial channel readiness. In your Android code, call `setDTR(true);` \ No newline at end of file +Some Arduinos use the DTR line to determine serial channel readiness. In your Android code, call `setDTR(true);` + + +## When reading a message, why don't all bytes arrive at the same time? + +The protocol layer does not guarantee all bytes will arrive in a single message. In fact the protocol layer doesn't have any knowledge of what your "message" is — it is just an interface for sending a serial stream of bytes. + +For example, to receive a 100 byte string, you might read 64 bytes, then 36 bytes, instead of a single message of 100 bytes. + +You need to account for this problem when designing your protocol. Some common techniques: + +* Fixed length messages: If you a message is always going to be 100 bytes, just keep reading until you have all 100. +* Length-prefixed messages: Prefix every message with a fixed-length `size` value; your message is complete after you've read `size` more bytes. +* Newline-terminated messages: Read until you see a `\n` (or any other "terminal" character). \ No newline at end of file