Read a text received on serial arduino port ?

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
freeseb
 
Posts: 4
Joined: Fri Mar 28, 2008 4:26 am

Read a text received on serial arduino port ?

Post 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

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Read a text received on serial arduino port ?

Post 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.

nathanas
 
Posts: 2
Joined: Wed Sep 28, 2011 10:35 am

Re: Read a text received on serial arduino port ?

Post 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
        
      }

User avatar
adafruit_support_bill
 
Posts: 89346
Joined: Sat Feb 07, 2009 10:11 am

Re: Read a text received on serial arduino port ?

Post 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/

nathanas
 
Posts: 2
Joined: Wed Sep 28, 2011 10:35 am

Re: Read a text received on serial arduino port ?

Post 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.

User avatar
adafruit_support_bill
 
Posts: 89346
Joined: Sat Feb 07, 2009 10:11 am

Re: Read a text received on serial arduino port ?

Post 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.

User avatar
pockybum522
 
Posts: 12
Joined: Wed Oct 19, 2011 1:00 pm

Re: Read a text received on serial arduino port ?

Post 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);
    }
  } 
}


User avatar
ygreq
 
Posts: 54
Joined: Mon May 02, 2011 11:17 am

Re: Read a text received on serial arduino port ?

Post 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

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”