0

Read a text received on serial arduino port ?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Read a text received on serial arduino port ?

by freeseb on Mon Mar 02, 2009 2:52 pm

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
freeseb
 
Posts: 4
Joined: Fri Mar 28, 2008 4:26 am

Re: Read a text received on serial arduino port ?

by mtbf0 on Mon Mar 02, 2009 5:08 pm

something like this
Code: Select all | TOGGLE FULL SIZE
#define INLENGTH 16
#define INTERMINATOR 13
char inString[INLENGTH+1];
int inCount;
and later on a little of this
Code: Select all | TOGGLE FULL SIZE
  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.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Read a text received on serial arduino port ?

by nathanas on Wed Sep 28, 2011 11:13 am

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 | TOGGLE FULL SIZE
  #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
       
      }
nathanas
 
Posts: 2
Joined: Wed Sep 28, 2011 10:35 am

Re: Read a text received on serial arduino port ?

by adafruit_support_bill on Wed Sep 28, 2011 11:28 am

Some small syntax errors there. Try this:
Code: Select all | TOGGLE FULL SIZE
      #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/clibrary/cstring/strncmp/

adafruit_support_bill
 
Posts: 64080
Joined: Sat Feb 07, 2009 10:11 am

Re: Read a text received on serial arduino port ?

by nathanas on Wed Sep 28, 2011 1:05 pm

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.
nathanas
 
Posts: 2
Joined: Wed Sep 28, 2011 10:35 am

Re: Read a text received on serial arduino port ?

by adafruit_support_bill on Wed Sep 28, 2011 1:50 pm

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.

adafruit_support_bill
 
Posts: 64080
Joined: Sat Feb 07, 2009 10:11 am

Re: Read a text received on serial arduino port ?

by pockybum522 on Wed Oct 19, 2011 1:07 pm

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 | TOGGLE FULL SIZE
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);
    }
  }
}


pockybum522
 
Posts: 10
Joined: Wed Oct 19, 2011 1:00 pm

Re: Read a text received on serial arduino port ?

by ygreq on Fri Dec 09, 2011 11:21 am

Look what I found:

Code: Select all | TOGGLE FULL SIZE
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

ygreq
 
Posts: 51
Joined: Mon May 02, 2011 11:17 am

Please be positive and constructive with your questions and comments.