Simpliest "good" serial PC-Arduino connection?

Hi.

I would like to estabilish two-side serial connection between my PC and Arduino. It can be half-duplex, don't have to be realtime, but I don't want delays longer than 200ms.

  1. I want Arduino to have the ability to send information about pressed buttons at any time and also...
  2. I want my PC to be able to send some info (let's say some numbers and one string, labout 100-500 chars) to Arduino at any time.

I know the totally-basic-basics of the serial connection on the Arduino side and on the PC side (the program will be written in C#), but I've got some doubts:

  • i know there's a 128B buffer for Serial.read(), and I can check it's content with Serial.available(). But What if I want to send bigger string? Do I have to split it to 128B chunks, receive it one-by-one and concatenate in the Arduino code?

  • what happens with data, which are too big to fit into serial buffer? Are they lost, or are waiting in queue?

  • are data sent by PC split by definition? For example, I want to send three different things, so I call (from PC) something like SerialPort.Write("one"), SerialPort.Write("something"), SerialPort.Write(2) - how does Arduino know it's three different data pieces? Or maybe I have to make a protocol, like SerialPort.Write("one#something#4")?

  • If so, are there any good habits for that? Like splitting data with "\r\n" or similar?
    I assume, I will not use line-break INSIDE the data sent via serial, so I could use that.

  • what problems can I get, when I try to make two-side communication? Do I have to worry about lost data, when the PC and Arduino are trying to send data at the same time?

  • i know there's a 128B buffer for Serial.read(), and I can check it's content with Serial.available(). But What if I want to send bigger string? Do I have to split it to 128B chunks, receive it one-by-one and concatenate in the Arduino code?

Yes.

  • what happens with data, which are too big to fit into serial buffer? Are they lost, or are waiting in queue?

What happens to the extra water when you try to pour 5 gallons into a 1 gallon bucket?

  • are data sent by PC split by definition? For example, I want to send three different things, so I call (from PC) something like SerialPort.Write("one"), SerialPort.Write("something"), SerialPort.Write(2) - how does Arduino know it's three different data pieces? Or maybe I have to make a protocol, like SerialPort.Write("one#something#4")?

The Arduino gets a stream of bytes from the serial port. You need to put markers between discrete bits of data.

  • If so, are there any good habits for that? Like splitting data with "\r\n" or similar?
    I assume, I will not use line-break INSIDE the data sent via serial, so I could use that.

Any character that is not a normal character in the data stream being sent works as a separator. Carriage returns, pound signs, <, >, !, $, %, etc. are also good choices.

  • what problems can I get, when I try to make two-side communication? Do I have to worry about lost data, when the PC and Arduino are trying to send data at the same time?

Only if you care about lost data. Slow rates and short distances minimize data loss, but there is no guarantee that what was sent will be received. You can do things to make it easier to know if data was lost. For example, if you are sending a string, send the length first:

<16:This is a string>

On the Arduino, look for the start marker, the number of characters, and the end marker. When the string has been received, compare it's length against the number of characters expected. If they match, great. Otherwise, implement a way to have the PC send the data again.

  1. I want my PC to be able to send some info (let's say some numbers and one string, labout 100-500 chars) to Arduino at any time.

500 characters is a huge amount of data. What are you intending to convey with that string? If you are sending data to display on an LCD, even the biggest ones (20 x 4) can only display 80 characters.

What happens to the extra water when you try to pour 5 gallons into a 1 gallon bucket?

Unnecessary irony :slight_smile: I was asking about PC send buffer, but I think I'd have to code it manually.

Also, I just don't get your gallons... :slight_smile:

500 characters is a huge amount of data. What are you intending to convey with that string? If you are sending data to display on an LCD, even the biggest ones (20 x 4) can only display 80 characters.

But I sure can display parts of it, one by one, or just scroll the whole string in one row, not by lcd.ScrollDisplayX() but by incrementing offset, making substring from it and just writting it on the display.

The buffer can hold 127 characters, maximum. If data is sent to the Arduino when the buffer is full, the extra characters are simply discarded.

Just like the extra water that didn't fit in the 1 gallon bucket. Sorry you didn't like (or understand) my analogy.

But I sure can display parts of it

That you can. But, unless the purpose is simply a scrolling marque, by the time the last of the 500 characters has been displayed, will anyone remember what the 1st part of the message was?

I was just suggesting that 500 might (not will) be more than you really need to accommodate.

[edit]Sorry you didn't like (or understand) my analogy.[/edit]
I liked it and understood it. It was my little (and also unnecessary - sorry) joke about units, because in Poland we measure the volume (capacity?) of the liquids in liters :slight_smile:

unless the purpose is simply a scrolling marque, by the time the last of the 500 characters has been displayed, will anyone remember what the 1st part of the message was?

Of course, you're right. For now I don't have the clear sollution for that and I think I will not need more than 150-200 chars, but I want to "be prepared". The first stage of my little idea is to make it work, THEN make it user-friendly :slight_smile: