Page 1 of 1

Read a text received on serial arduino port ?

Posted: Mon Mar 02, 2009 2:52 pm
by freeseb
Hi,

I'm sending a chain 'azerty' for example from my pc to the arduino.
All is Ok except that the arduino receives it character by character. what is the good way to have the whole 'azerty' in one time or to reconstruct it ?

Thanks a lot

seb

Re: Read a text received on serial arduino port ?

Posted: Mon Mar 02, 2009 5:08 pm
by mtbf0
something like this

Code: Select all

#define INLENGTH 16
#define INTERMINATOR 13
char inString[INLENGTH+1];
int inCount;
and later on a little of this

Code: Select all

  inCount = 0;
  do {
    while (!Serial.available());             // wait for input
    inString[inCount] = Serial.read();       // get it
    if (inString [inCount] == INTERMINATOR) break;
  } (++inCount < INLENGTH));
  inString[inCount] = 0;                     // null terminate the string
will read a string off the serial port and finish when it has received 16 characters or a carriage return. the result will be in a nice null terminated character array suitable for use with c library string functions.

Re: Read a text received on serial arduino port ?

Posted: Wed Sep 28, 2011 11:13 am
by nathanas
Dear mtbf0,

I want to do the exact same thing as freeseb. But the problem is that for some reason your code is not right, and i can't compile it.
The arduino IDE says:
"sketch_sep28c.cpp: In function 'void loop()':
sketch_sep28c:17: error: expected `while' before '(' token
sketch_sep28c:17: error: expected `;' before ')' token
sketch_sep28c:17: error: expected primary-expression before ')' token
sketch_sep28c:17: error: expected `;' before ')' token"

Line17 is: "if (inString [inCount] == INTERMINATOR) break;"

Please help me by fixing the code above.
And if you could I would like you to help me by telling me how I can compare the "text" that i receive to another string. Let's suppose I receive something and I want to check if the text I received is "OK". I want to know how to check this. I am developing a project for my university and I've been looking for this answer for about 10 days. I can only receive the first character.

Please help...

Code: Select all

  #define INLENGTH 16
    #define INTERMINATOR 13
    char inString[INLENGTH+1];
    int inCount;
      
      void setup() {
        
      }
      
      
      void loop(){
      
      inCount = 0;
      do {
        while (!Serial.available());             // wait for input
        inString[inCount] = Serial.read();       // get it
        if (inString [inCount] == INTERMINATOR) break;
      } (++inCount < INLENGTH));
      inString[inCount] = 0;                     // null terminate the string
        
      }

Re: Read a text received on serial arduino port ?

Posted: Wed Sep 28, 2011 11:28 am
by adafruit_support_bill
Some small syntax errors there. Try this:

Code: Select all

      #define INLENGTH 16
        #define INTERMINATOR 13
        char inString[INLENGTH+1];
        int inCount;
         
          void setup() {
           
          }
         
         
          void loop(){
         
          inCount = 0;
          do 
          {
            while (!Serial.available());             // wait for input
            inString[inCount] = Serial.read();       // get it
            if (inString [inCount] == INTERMINATOR) break;
          } while(true);
          (++inCount < INLENGTH);
          inString[inCount] = 0;                     // null terminate the string
           
          }
how I can compare the "text" that i receive to another string.
Use strncmp() http://www.cplusplus.com/reference/clib ... g/strncmp/

Re: Read a text received on serial arduino port ?

Posted: Wed Sep 28, 2011 1:05 pm
by nathanas
Can you please add to the code the Serialprint command to print the whole "word" that was received through the RXD pin?
So if it has received the word "TEST", I need it to be printed on my Serial Monitor.

Re: Read a text received on serial arduino port ?

Posted: Wed Sep 28, 2011 1:50 pm
by adafruit_support_bill
We can point you in the right direction, but we can't write your code for you. Serial.println() is the function you need.

If you are new to programming, you may want to start with the Arduino Tutorials for some step-by-step instructions.

Re: Read a text received on serial arduino port ?

Posted: Wed Oct 19, 2011 1:07 pm
by pockybum522
My apologies for posting to an old thread, but this is the top hit on google for what I was searching and I wanted to share with others my heavily, heavily modified code I found http://fayazkadir.com/blog/?p=2291 < There.

I ended up coming up with something you should be able to plug right into your project and have string reading capabilities over serial. I just tested this with the duemilanove and serial monitor to confirm it's working.

If you're using the arduino serial monitor, be sure to change no line ending to carriage return and 9600 baud.

To read strings:

Code: Select all

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600); // Initialize serial port
}

void loop()
{
  serialReader(); 
}

void serialReader(){
  int makeSerialStringPosition;
  int inByte;
  char serialReadString[50];
  const int terminatingChar = 13; //Terminate lines with CR

  inByte = Serial.read();
  makeSerialStringPosition=0;

  if (inByte > 0 && inByte != terminatingChar) { //If we see data (inByte > 0) and that data isn't a carriage return
    delay(100); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)

    while (inByte != terminatingChar && Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
      serialReadString[makeSerialStringPosition] = inByte; // Save the data in a character array
      makeSerialStringPosition++; //Increment position in array
      //if (inByte > 0) Serial.println(inByte); // Debug line that prints the charcodes one per line for everything recieved over serial
      inByte = Serial.read(); // Read next byte
    }

    if (inByte == terminatingChar) //If we terminated properly
    {
      serialReadString[makeSerialStringPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (terminating char) with 0
      Serial.println(serialReadString);
      if (strcmp(serialReadString, "LEDOn") == 0) digitalWrite(13, HIGH);
      if (strcmp(serialReadString, "LEDOff") == 0) digitalWrite(13, LOW);
    }
  } 
}


Re: Read a text received on serial arduino port ?

Posted: Fri Dec 09, 2011 11:21 am
by ygreq
Look what I found:

Code: Select all

boolean started = false;
boolean ended = false;

char inData[24]; // Size as appropriate
byte index = 0;

#define SOP "{"
#define EOP "}"

void loop()
{
   while(Serial.available() > 0)
   {
      char inChar = Serial.read();
      if(inChar == SOP)
      {
         started = true;
         index = 0;
         inData[index] = '\0';
      }
      else if(inChar == EOP)
      {
         ended = true;
         break;
      }
      else
      {
         if(index < 24-1) // Array size
         {
            inData[index++] = inChar;
            inData[index] = '\0';
         }
      }
   }

   if(started && ended)
   {
      // Parse the data in inData here...
   }
}
Thanks to PaulS on arduino.cc forum