Sending serial data

Hello, if I send two letters separately (ex. "a" then "b") through serial will the Serial.available function return as 2??

If you don't read them, eventually yes.

So if I send both then Serial.available returns 2, good. So now if I do val1= Serial.read and then val2=Serial.read after, val1 should ==a and val2 should == b

Hello, if I send two letters separately (ex. “a” then “b”) through serial will the Serial.available function return as 2??

Serial data received goes into the serial receive buffer and will stay there until read with Serial.read() (one byte per read). I am assuming that you are using serial monitor. All sent characters go into the serial buffer so if you type ‘a’ and then send, 1 to 3 characters are sent depending on the line ending selected in serial monitor (to the left of baud setting).

So the answer is, it depends.

Try this with different line endings set to see.

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  {
    Serial.println(Serial.read());    
  }

}

So if I send both then Serial.available returns 2, good. So now if I do val1= Serial.read and then val2=Serial.read after, val1 should ==a and val2 should == b

Are they sent separately, 'a', send, 'b', send?
If sent with no line endings, yes. Otherwise you need to compensate for the additional line feed and/or carriage return.

If you want to know how to properly receive serial data look at this most excellent post.

Check the lower right of your Serial Monitor- is it set to No Line Ending, Carriage Return, or Carriage Return + Line Feed?
That will impact how many bytes come across when you press Enter.
You could have your code ignore those: for example:

if (Serial.available() > 0){
newByte = Serial.read();
if (newByte == '\n'  || newByte == '\r')(  // need to check on these www.asciitable.com
//  maybe compare to 0x0A, 0x0D instead?
// ignore the byte
}
else {
// do something with newByte
}
}

Okay maybe I'll get some more specific answers if I explain what I plan to do with this code. The serial data I am receiving isn't coming from the serial monitor. It is coming in from an HC-12 wireless serial module. One Arduino with an HC-12 is sending letters and the other is receiving and reading those letters to do stuff based on what letter was received.

Okay maybe I'll get some more specific answers

Can't get much more specific than reply #1

The answers don't change just because you are getting the data from serial monitor or an HC-12. Serial is Serial, is Serial.

Read the post in the link in reply #4. It will tell you how to do what you want. If that doesn't do it for you I sure as ship can't.

To answer your question in reply #2, yes.

To explain a little more about serial communications:

If you send two characters from your sender, there is no guarantee that they are available in the receiver without a delay between them. As a result, the below code will, once a character is received, output 1 to the serial monitor and once the second character is received output 2. It will also display the time when the character was received.

void loop()
{
  static int oldCharcount = 0;
  int charCount = Serial.available();
  // only display on change
  if (charCount != 0 && oldCharcount != charCount)
  {
    Serial.print(millis()); Serial.print(", number of characters received: "); Serial.println(charCount);
    oldCharcount = charCount;
  }
}

There are several reasons for that
1)
It takes time to transmit a byte; at e.g. 9600 baud, roughly 1ms. For the processor, this is an eternity.
2)
between sending the first and the second character, the sender has to do something else which will delay the transmission of the second character

If you run the above with different baudrates (and serial monitor), you can easily observe point 1

  300 baud
  2762, number of characters received: 1
  2795, number of characters received: 2

  9600 baud
  1408, number of characters received: 1
  1409, number of characters received: 2

  115200 baud
  5385, number of characters received: 1
  5385, number of characters received: 2

@300 baud, it takes ~30ms to receive the second byte byte, @9600 it takes ~1ms and @115200 it basically takes no time.

Point 2 is more difficult to simulate with the serial monitor. But believe me that your sender can be doing something else in between the two bytes, even if you use Serial.print("ab");

If you want to send and receive multiple bytes, follow the approach in the thread linked by groundfungus.