get a string via serial port,, problem

hi guys, i just start to learn arduino and i got a problem with the serial communication.
i am using RS232 serial port send a letter("F" a string) to a RS232 to TTL converter then to the arduino uno.
i had tested to send a char(byte) and it is successful. But for string it is not.
i got some codes from the internet but after tried them all it is still not working.

Could any one show me a demo code? so that i can learn from it. thanks a lot.

hi guys, i just start to learn arduino and i got a problem with the serial communication.
i am using RS232 serial port send a letter(“F” a string) to a RS232 to TTL converter then to the arduino uno.
i had tested to send a char(byte) and it is successful. But for string it is not.
i got some codes from the internet but after tried them all it is still not working.

Could any one show me a demo code? so that i can learn from it. thanks a lot.

here are my codes and they are not work :frowning:

char buffer[10];

void setup()
{
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  
  Serial.begin(9600);
}

void loop()
{
  for (int i = 0; i < 3; i++)
  { 
   char letter = Serial.read();
   buffer[0] = letter;
  }
  if(buffer[0] == 'F')
  {
    digitalWrite(13,HIGH);
  }
  else if(buffer[0] == 'B')
  {
    digitalWrite(13,LOW);
  }
}

You're reading the data without checking if there is data to read. You should be using Serial.available() to check how much data is available, before your read it.

While your solution is a creditable first attempt there are lots of mistakes which would prevent it from working. There are lots of examples showing how to read and process serial input and I suggest you look to see how others have solved this problem. For example, look at Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

Arrch:
You’re reading the data without checking if there is data to read. You should be using Serial.available() to check how much data is available, before your read it.

thanks for teaching me. the checking is important.
i just share my code and hope it is helpful for others.

char buffer[10];          // use to store data (the bigger the better?) anyway the buffer size must bigger than the total data. 

void setup()
{
                                  // some setup. 
  Serial.begin(9600);
}
void loop()             // to get string from serial 
{
  if (Serial.available () > 0)  //checking
  {
    for (int i = 0; i < 3; i++)  //for loop to get all data from buffer, '3' is the string len.  
    { 
     buffer[i] = Serial.read();   
    }
  }
}

Checking thatSerial.available() > 0does not mean that 3 bytes are available to be read.
Why are you reading 3 bytes from serial anyway ?

UKHeliBob:
Checking thatSerial.available() > 0does not mean that 3 bytes are available to be read.
Why are you reading 3 bytes from serial anyway ?

basically i was reading a "F" (string letter) and it is 'F','\n' in bytes so i need the buffer to read two times to store all data inside buffer. 3 is just nice for my use. this number is to decide how many data that we want to store in the buffer.

So don't read a specific amount. Create an array to store the data. Evrytime there is a byte available, read it. If its a new line, process the data you've stored in the array. If It's not a new line, store it in the buffer.

Very simple serial code that will send back what is received. Test with the serial monitor.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}