Serial Port Communication with PC

I guess the Arduino Nano when connected to a PC shows up as a Virtual COM Port on the PC this is probably USB CDC isn't it.

My question is if I send a packet of size 30 bytes from a PC app over this virtual COM Port am I guaranteed to receive those 30bytes as a single chunk on the ARduino ... or is it a possibility that the underlying PC layer might split that up and send it in smaller chunks...

If I do receive it as a single packet will it also work for a bigger packet of say 100bytes.

What about the other way around? I mean if I right 30 bytes from the Arduino and want to receive it on the PC side.

I am asking this because then based on the answers the software running on the PC or the ARduino may have to assemble chunks to form a bigger packet or if I am going to receive a packet as a single chunk then I do not need to do this extra bit of code..

Bytes via serial line are sending as is, no in packets. Receiver have to be ready all the time to accept bytes sent via the line. Nothing is guaranteed. On the Arduino side there is a mechanism based on interrupt which ensures an action at each incoming data. There is also cyclic buffer which is storing the data which wait for processing. However there are problems. The interrupt can be missed if the rate is too high or MCU is too busy and also the data in buffer can be overwitten if buffer is small or MCU is busy to read out it.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

If you use start- and end-markers (as you should - see the 3rd example) the Arduino code will keep gathering data until it has the complete message.

...R

There is no guarantee that they arrive as one chunk. So write you code accordingly.

Budvar10: Bytes via serial line are sending as is, no in packets.

Sorry for the confusion in the term packets.. Basically the PC software is sending data in packets of 20bytes at ever 50ms intervals....

Robin2: Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

If you use start- and end-markers (as you should - see the 3rd example) the Arduino code will keep gathering data until it has the complete message.

...R

Thanks this looks good!

sterretje: There is no guarantee that they arrive as one chunk. So write you code accordingly.

Yes, this it what I wanted to know... Many Thanks!

ARdentLogic: I guess the Arduino Nano when connected to a PC shows up as a Virtual COM Port on the PC this is probably USB CDC isn't it.

My question is if I send a packet of size 30 bytes from a PC app over this virtual COM Port am I guaranteed to receive those 30bytes as a single chunk on the ARduino ... or is it a possibility that the underlying PC layer might split that up and send it in smaller chunks...

This is the simple story:

Serial is not sending in packets like Ethernet, Serial is sending byte by byte and when sending from PC to Arduino (or the other way round) you can be sure that there will be no significant pausing between bytes.

When sending with 8N1 protocol, the transmission is 10 bits per byte (one start-bit, 8 data-bits, one stop-bit), so it simply depends on the BAUD rate how quickly the data are transmitted.

With 9600 BAUD you are transferring 960 bytes per second, you can roughly calculate (1 byte per millisecond at 9600 baud.

With 115200 BAUD you are are transferringf 11520 bytes per second.

No significant pausing between the single bits. No significant pausing between the single bytes. And no "packet transfer" at all.

The full story is a bit more complicated, as the data are transferred across USB and the data are buffered in the sender and as well in the receiver. You don't have to care about as a newbie.

You better care about what's happening on your own send and receive buffers on hte Arduino instead of thinking about how the transfer of bits is actually taking place.