0
\$\begingroup\$

This might seem a bit simple, but when I'm reading data sent from over RS-232 to be read by my computer, is it reasonable to expect that if the data was sent as a single packet, that the packet will stay whole when I go to read it?

For example, If I send a 32-byte packet of data over the serial port, and this arrives into the serial buffer on my computer, could I safely assume that the data arrives simultaneously? I feel as if, even if the data was sent as a packet, there is a chance that only half of the data might be in the buffer on my computer before I go to read it. Because packets have a certain data "width", I imagine it could create a sort of race condition where the rest of the packet has to arrive before I finish reading the buffer. Especially if the idea of "packet" is interpreted more loosely to mean multiple separate pieces of data that are sent shortly after each other (shortly being something along the lines of for-looping through the data to send and calling the serial write method on it sequentially), as opposed to a single 32-byte data struct which would contain the data.

Thanks in advance for any help you can provide.

\$\endgroup\$
3
  • \$\begingroup\$ That totally depends on the receive stack of your OS (if any). You should probably not rely on it if possible \$\endgroup\$
    – PlasmaHH
    Jan 23, 2017 at 20:15
  • \$\begingroup\$ Read about checksum. That could apply to what you are asking about. And it all depends on how fast and large your buffer is. \$\endgroup\$
    – 12Lappie
    Jan 23, 2017 at 20:19
  • 1
    \$\begingroup\$ Related: electronics.stackexchange.com/questions/186254/… \$\endgroup\$
    – Eugene Sh.
    Jan 23, 2017 at 20:46

1 Answer 1

3
\$\begingroup\$

The data is not packetised on the wire (unless you're using USB-serial), so there is absolutely no guarantee that the data will stay together in any particular way.

If you "drain" the input and wait for incoming bytes to stop, then you can be fairly sure you've got all of it that was sent, and you can work backwards from the end. Unless one of the bytes in the middle was lost or corrupted.

Most sensible protocols use "markers" to indicate start or end of data. This is why most serial protocols look like ASCII text which ends lines with CR/LF.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.