Receive text over Serial and display after enter key is detected?

I’ve been scouring the internet, and these forums, trying to understand how to use serial.read to read in text from the serial port, and upon receving the enter key (carriage return?) send the string to a Sure 3216 display.

I’ve been on the internet long enough to know how this works, so please dont flame me with “do a search” because I’ve looked at the following resources:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1267807830
http://www.adafruit.com/forums/viewtopic.php?f=8&t=9918 ← this is written wrong, but even after I got it to compile it didn’t work
and I’ve also tried searching google for “read text from serial” and other terms, only finding bits about how to read a character at a time, etc.

My Code is

//THIS WORKS BELOW
void loop() {
  char input[10];
  memset(input, '\0', 10);
  byte inByte = '\0';
  while(inByte != '!') {
    inByte = Serial.read(); // Wait for the start of the message
  }
  if(inByte == '!') {
   while(Serial.available() < 8) { // Wait until we receive 8 characters
      ;
    }
    for (int i=0; i < 8; i++) {
     
      input[i] = Serial.read(); // Read the characters into an array
      
    }
  }
  scrolltextsizexcolor(0,input,2,GREEN, 0,my2font,8,8,'G',0);
}

This works, but to use it, I have to send “!” followed by exactly 8 characters (or whatever I set the count to…10,15,etc). I haven’t figured out (because I don’t know C that well) how to basically flip this so that instead of waiting for a ! and then reading to 8, it reads until it sees enter and then displays what it has.

Can someone help me figure this out?

TIA!

const int bufferSize = 128;
char inputBuffer[bufferSize];
int bufferPointer = 0;
void loop() 
    {
    char inByte;

    if (Serial.available())
        {
        inByte = Serial.read();

        if (inByte == '\n') 
            {
            // 'newline' character
            inputBuffer[bufferPointer++] = '\0';
            scrolltextsizexcolor(0,inputBuffer,2,GREEN, 0,my2font,8,8,'G',0);
            bufferPointer = 0;
            }
        else
            {
            // not a 'newline' character
            if (bufferPointer < bufferSize - 1)  // Leave room for a null terminator
                inputBuffer[bufferPointer++] = inChar;                
            }
     }

this should work

// code modified next two lines 
char c = '';
while (c != '!') if (Serial.available() >0) c = Serial.Read();

int i = 0;
while (i< 8) if (Serial.available() > 0) input[i++] = Serial.Read();

but it is active polling (== waiting) for data.

better use a simple state machine

int state = 0;  // 0 = waiting for !   1 = filling array 
input[8];
int i;
bool done = false;  // true if array is filled
void loop()
{
  if (Serial.available() > 0 && done = false)
  {
    c = Serial.Read();
    switch (state)
    {
      case 0: 
        if (c == '!') { state = 1; i=0; }
        break;
      case 1:  
        input[i++] = c; 
        if (i==8) state = 0; 
        done = true; 
        break;
    }
  }
  // ... do other things while no serial available

  if (done) // input[] contains 8 bytes  ....
  {
     .. proces input[]
    done = false;
  }
}

this should work

Code: char c = ""; while (c != "!")

Hmmm.

yes , the while() has only one statement in its loop so no {} needed, and the if() has also only one statement.... and yes it looks not maintainable :)

And the assignment of a string to a char?

Oops :blush: you're right ... getting late here .. single quotes needed, i'll modify my posting.

Thanx for pointing out, Rob

johnwasser:

const int bufferSize = 128;

char inputBuffer[bufferSize];
int bufferPointer = 0;
void loop()
    {
    char inByte;

if (Serial.available())
        {
        inByte = Serial.read();

if (inByte == ‘\n’)
            {
            // ‘newline’ character
            inputBuffer[bufferPointer++] = ‘\0’;
            scrolltextsizexcolor(0,inputBuffer,2,GREEN, 0,my2font,8,8,‘G’,0);
            bufferPointer = 0;
            }
        else
            {
            // not a ‘newline’ character
            if (bufferPointer < bufferSize - 1)  // Leave room for a null terminator
                inputBuffer[bufferPointer++] = inChar;               
            }
     }

John Thanks for the input… I had to change “inChar” at the end to “inByte”, and add a “}” at the end but it works. Where do I send the beer? :slight_smile:

Possibly another way to do what you want.

//zoomkat 9-9-10 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
    Serial.begin(9600);
        }

void loop() {

        //expect a string like wer,qwe rty,123 456,hyre kjhg,
        //or like hello world,who are you?,bye!,
        while (Serial.available()) {
        delay(1);  //small delay to allow input buffer to fill
      char c = Serial.read();  //gets one byte from serial buffer
        if (c == ',') {break;}  //breaks out of capture loop to print readstring
        readString += c; //makes the string readString
        }
      
      if (readString.length() >0) {
      Serial.println(readString); //prints string to serial port out
      
      //do stuff here
      
      readString=""; //clears variable for new input
      }
   }

evanrich: John Thanks for the input.. I had to change "inChar" at the end to "inByte", and add a "}" at the end but it works. Where do I send the beer? :)

No beer necessary. Glad I could help. ^_^