compere string using strcmp

I know how to compere string in normal Serial monitor
I want to know how to compere when I'm connecting another device to the Arduino

this is the code that working

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

and this is my try to get the same from far away device using serial connection
I have change all "Serial.print\read" to "mySerial.print\read" , just not the last one

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX


void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(19200);
  delay(1500);
  Serial.println("Ready to work!");
  mySerial.begin(19200);
  mySerial.println("AT");//just to see that it's working
}

void loop()
{
  serialReader();
}

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

  inByte = mySerial.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 && mySerial.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 = mySerial.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);
    }
  }
}

can someone help\guide ?

Thanks ,

Before Serial.read() you should check Serial.available() for -every- character.

The half-fast way is to check and get 1 character then delay() while the rest "should come in". LOL, how many ways can that go wrong?

Normally I check Serial.available() right in loop(), usually as the last priority if there's other tasks I have going on since Serial is so glacial-slow compared to Arduino. But I code as if I need to be checking something 10,000x a second as well as having user I/O.

try what you said (if I understand you correct)
and I know I see in the Serailmonitor 1 no mater what I send

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX


void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(19200);
  delay(1500);
  Serial.println("Ready to work!");
  mySerial.begin(19200);
  mySerial.println("AT");//just to see that it's working
}

void loop()
{
  serialReader();
}

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

 while (mySerial.available() )
  inByte = mySerial.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 && mySerial.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 = mySerial.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, "on") == 0) digitalWrite(13, HIGH);
      if (strcmp(serialReadString, "off") == 0) digitalWrite(13, LOW);
    }
  }
}

This compiles but I can't test it. It should be close to or working.
I could do more but this should be confusing enough.

Basically, this will go through loop() 1,000's if not 1,000,000 times just to read "off". Of you want to know then add a counter and print that, it should open your eyes and mind.

The flow is

  1. wait for data
  2. read data and either append it to the string or end the string
    2A) if end the string, print, lex and modify led state then re-init buffer index
  3. goto 1
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

char serialReadString[50]; // now global, allocated once and stays
byte maxStringLen = 49; // allow for terminating NULL
byte makeSerialStringPosition = 0;
byte inByte;
const byte terminatingChar = 13; //Terminate lines with CR


void setup() 
{
  pinMode(13, OUTPUT);
  Serial.begin(19200);
  Serial.println("Ready to work!");
  mySerial.begin(19200);
  mySerial.println("AT");//just to see that it's working
}

void loop()
{
  if ( mySerial.available())
  {
    inByte = mySerial.read();

    if ((inByte != terminatingChar) && (makeSerialStringPosition < maxStringLen))
    { 
      serialReadString[makeSerialStringPosition++] = inByte; // position is post-incremented
    }
    else // end the string if read terminatingChar --or-- the buffer array is full 
    {
      serialReadString[makeSerialStringPosition] = 0; 
      Serial.println(serialReadString);
      if (strcmp(serialReadString, "on") == 0) digitalWrite(13, HIGH);
      if (strcmp(serialReadString, "off") == 0) digitalWrite(13, LOW);

      makeSerialStringPosition = 0; // init for next input
    }
  }
}

it's written alright , and it's have no errors- thanks

but it doesn't work for me , I will try something else and ask again...

Thanks ,

That's a whole lot of code and wasted processor time for something fairly simple:

const byte MAX_CHARS = 10; // maximum number of characters we can receive before overflow occurs
const char TERMINATING_CHAR = '\n'; // let's us know when the string is complete

char buffer[MAX_CHARS+1]; // storage for our characters (+ 1 for null-terminator)
byte index = 0;  // keeps track of where to put the next character

void setup()
{
  Serial.begin(115200);
  Serial.println("begin");
}

void loop()
{
  if (Serial.available() > 0) // if there is something in the buffer to read
  {
    char inByte = Serial.read(); // read it
    if (inByte == TERMINATING_CHAR) // the string is complete
    {
      // DO SOMETHING WITH THE STRING HERE
      buffer[0] = '\0'; // clear the buffer
      index = 0;
    }
    else if (index < MAX_CHARS) // make sure we aren't overflowing the buffer
    {
      buffer[index] = inByte; // store character in our buffer
      index++; // move index to next spot
      buffer[index] = '\0'; // and null terminate for string processing
    }
    else
    {
      // OVERFLOW OCCURRED, DO SOMETHING ABOUT IT
    }
  }
}

Arrch:
That's a whole lot of code and wasted processor time for something fairly simple:

It would be a good idea to check there was space in the array (including an allowance for the following null terminator) before appending the new character.

PeterH:
It would be a good idea to check there was space in the array (including an allowance for the following null terminator) before appending the new character.

I agree, but the above was a simple example that demonstrates the base logic behind it. What happens when an overflow occurs has been left to the designer to decide. I suppose I can edit my example to include an overflow check.