serial communication...does the char wait to be received?

Maybe a strange question:

I have two arduinos...one transmitter and one receiver using software serial library..

the transmitter does something like:

char myIDChar = '!';

void loop() {

mySerial.write(myIDChar);
delay(10000);
//do other stuff
}

and the receiver:

void loop(){
if (mySerial.available()) {
    char myID = mySerial.read();
    Serial.println("!, on");
}
}

The question is: if the transmitter send the char while the receiver "do other stuff", what happens to the char it sent ONCE? will it receives it in the next it'll run mySerial.read()? or it is lost?


What i want actually to do with the receiver is: read once the char the the transmitter sends, and Serial print this char loop doing other stuff, once the transmitter stop sending (even after 10 seconds) i want to serial.print("i stop receiving");

is there any better way to do this?

Characters received by the Serial interface are put into a 64 byte buffer from where they are removed by Serial.read(). If more than 64 characters are received before being read they are discarded.

Luckily Serial comms is relatively slow so a properly written program has no trouble reading Serial input even if it only puts them in a larger buffer before processing them when the input is complete

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.

...R

"is there any better way to do this?"

Just start with a simple echo code setup where you send a character from the serial monitor to the arduino, and the arduino captures it and sends it back to the serial monitor.

thanks for the quick answers..

Luckily Serial comms is relatively slow so a properly written program has no trouble reading Serial input even if it only puts them in a larger buffer before processing them when the input is complete

so, you mean that even if transmitter send just one char and the receiver go to Serial.read() command "10" seconds after the char will be there (in the buffer)?

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.

yes of course i've read this tutorial..very usuful but i didnt find all my answers.. (or they arent obvious)

Ok, i've already implement receive just one char and then stop receiving...then how i say to the receiver, there are no more chars coming? clear the buffer?

Just start with a simple echo code setup where you send a character from the serial monitor to the arduino, and the arduino captures it and sends it back to the serial monitor.

actually i dont want it to be duplex communication..

"simple" as: transmitter sends one char many times..receiver recognize it...i unplug the cable, receiver knows that there is no more transmission..i dont want to send real data..the char is just for identification

There is more than one library called Software Serial. In general, these libraries use interrupts and a buffer in an attempt to ensure that they do NOT miss any characters received while other non-interrupt things are happening.

so, you mean that even if transmitter send just one char and the receiver go to Serial.read() command "10" seconds after the char will be there (in the buffer)?

Yes

If you don't turn the Arduino off or reset it then the single char will be there in 10 seconds, 10 minutes, 10 hours...10 weeks later

uzer123: yes of course i've read this tutorial..very usuful but i didnt find all my answers.. (or they arent obvious)

Ok, i've already implement receive just one char and then stop receiving...then how i say to the receiver, there are no more chars coming? clear the buffer?

I'm a bit confused.

  • If you have written a program to receive a single character and that character arrives isn't that the moment to clear the buffer?
  • Why would there be more characters coming?
  • If there were more characters coming what would you want to do with them?

...R

"actually i dont want it to be duplex communication.."

The "duplex" echo is just for testing so you can see if your code is working the way you think it is. In your case, just check the serial buffer for the presence of your signal character. If it is present, you reset a millis() test for time elapsed. The millis() time is monitored, and if 10000 millis is exceeded, have the arduino print its statement.