Read a text received on serial arduino port ?

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Read a text received on serial arduino port ?

Postby freeseb » Mon Mar 02, 2009 1: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 3:26 am

Re: Read a text received on serial arduino port ?

Postby mtbf0 » Mon Mar 02, 2009 4:08 pm

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.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1642
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca

Re: Read a text received on serial arduino port ?

Postby nathanas » Wed Sep 28, 2011 10: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
  #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 9:35 am

Re: Read a text received on serial arduino port ?

Postby adafruit_support_bill » Wed Sep 28, 2011 10:28 am

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/clibrary/cstring/strncmp/
User avatar
adafruit_support_bill
 
Posts: 15995
Joined: Sat Feb 07, 2009 9:11 am

Re: Read a text received on serial arduino port ?

Postby nathanas » Wed Sep 28, 2011 12: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 9:35 am

Re: Read a text received on serial arduino port ?

Postby adafruit_support_bill » Wed Sep 28, 2011 12: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.
User avatar
adafruit_support_bill
 
Posts: 15995
Joined: Sat Feb 07, 2009 9:11 am

Re: Read a text received on serial arduino port ?

Postby PockyBum522 » Wed Oct 19, 2011 12: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
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: 1
Joined: Wed Oct 19, 2011 12:00 pm

Re: Read a text received on serial arduino port ?

Postby ygreq » Fri Dec 09, 2011 10:21 am

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
ygreq
 
Posts: 13
Joined: Mon May 02, 2011 10:17 am


Return to General Project help

Who is online

Users browsing this forum: Google [Bot] and 8 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [102]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]