How to compare serial.read with a string value?Strange characters through serial

I am making a project for my university about communication between arduino and GM862 GPS-GSM module.
I have successfully made tha arduino communicate with the Telit, but the only way to receive the responses from my AT commands is by doing a trick. I connect the TXD of arduino to TXD of my module and the RXD of module to RXD of a FT232RL serial to usb board. Then I read the port results through hyperterminal to my computer and everything is right.
When I am doing the same thing through my arduino program (RXD is connected to RXD of arduino) then I receive the correct responses but with some strange characters at the end.
I am using the Arduino Duemilanove with two Software serial ports to communicate to my module.

But because I need to make a program that handles some data that's been received by arduino, I need it to work fully with the arduino IDE (Serial Monitor).
I am using some kind of code but sometimes I receive strange characters that are not making sense. Everytime I receive the correct answer etc."OK" but its followed by characters.

I am providing my code to you so maybe someone can help me.

#include <NewSoftSerial.h>

NewSoftSerial mySerial(2, 3);

void setup()  
{
  Serial.begin(4800);
  Serial.println("GM862 testing...");

  // set the data rate for the NewSoftSerial port
  mySerial.begin(4800);
  mySerial.println("AT");
  
 
}

void loop()                     // run over and over again
{

  if (mySerial.available()) {
      Serial.print((char)mySerial.read());
  }
  if (Serial.available()) {
      mySerial.print((char)Serial.read());
  }
  
  
  
  
}

What I receive to my arduino serial monitor:

GM862 testing...
EÕÔHèjªHøGM862 testing...
AT

OK
GM862 testing...
AT

OK
GM862 testing...
AT

OK
GM862 testing...
AT

OK
GM862 testing...
Q55RzµÕHøGM862 testing...
AT

OK

I need to compare the "text" i receive from myserial to my arduino with another string. How can I do this? Can anyone send me the right code for this?
For example, I need to know how to compare what I receive, with the word "OK' or the word "GM862-GPS" so i make a routine for the program to do some functions if its TRUE.

I am trying something like this...

if (mySerial.available() == "OK") {
Serial.println(" Modem --> OK");
}

Can someone help me by giving me an example code of what I want to do? Or just fix this one?

Thanks in advance!

if (mySerial.available() == "OK") {

The available() method returns the number of characters available to read. It will never equal the string OK.

The need to read serial data and store it in an array only comes up about once a day. Clearly you spent 0 time searching.

What I receive to my arduino serial monitor:

If this is from a single opening of the Serial Monitor, it should be clear that the Arduino is resetting. Nothing in the code is causing this that I can see, so you appear to have a hardware problem.

I think that this is done by newsoftserial library automatically...

the problem is how to compre what i receive to a string.... I am newbie in programming and I need a lot of help..

I think that this is done by newsoftserial library automatically...

You think what is done automatically? NewSoftSerial does not reset the Arduino at the end of loop.

I meant that the library does the job of saving the characters to arraies by itself (I think)

PaulS:
If this is from a single opening of the Serial Monitor, it should be clear that the Arduino is resetting. Nothing in the code is causing this that I can see, so you appear to have a hardware problem.

There is no hardware problem. I am resseting the arduino manually by hand each time. I did it so I give you a sample of what I am debugging.

I meant that the library does the job of saving the characters to arraies by itself (I think)

Better think again, then. It does not.

Ok then..Can you please, send me a code of making the above think possible?

I need correct reading for the beginning. The answers I receive every time I send an AT command through my arduino to the module are less than 16 characters...

Hi PaulS,

I found a post with some code in arduino forum that will help me... I made some modifications and I hope that you can help me make it work.
The problem is that if I don't press OK to the serial monitor, for the first time, I never receive "Correct!"
How can we manipulate this code to my needs?
I need it to save every response to another string variable, and then delete it from inData, so I can receive and check new responses from my module.

I am using the exact code given below:

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character


void setup(){
  
  Serial.begin (4800);
  Serial.println("Starting test...");
}
void loop()
{
   while(Serial.available() > 0) // Don't read unless
								  
   {
	 if(index < 19) // One less than the size of the array
	 {
	     inChar = Serial.read(); // Read a character
	     inData[index] = inChar; // Store it
	     index++; // Increment where to write next
	     inData[index] = '\0'; // Null terminate the string

	 }



   }
   
   
   while (strcmp (inData, "OK") == 0) {
   Serial.println("Correct!");
}
   
}
   while (strcmp (inData, "OK") == 0) {
   Serial.println("Correct!");
}

I seriously doubt that you want a while statement. It seems like an if statement is more appropriate.

if (strcmp (inData, "OK") == 0) {
   Serial.println("Correct!");
   delay(500);
}

Thanks

nathanas:
I need it to save every response to another string variable, and then delete it from inData, so I can receive and check new responses from my module.

Besides the if problem, I need it to be more than one times functional. Or show me a way to delet arrays. I dont know much about arrays and thats why I am expieriencing this difficulty...

Or show me a way to delet arrays.

You don't want to delete the array, just the data in the array.

index = 0;
inData[index] = \'0';

is used to reset the array and index for the next use.

Thanks for your help...
I managed to make a program to give me feedback when i receive the specified text from my Telit GM862-GPS module.

I am going to test everything tomorrow because I am too tired, I've been looking for it all day.

The code I am currently using is:

char inData[20]; 
char inChar; 
byte index = 0; 


void setup(){
  
  Serial.begin (4800);
  Serial.println("Starting test...");
}
void loop()
{
  
   
   
   readserial();
  if (strcmp (inData, "OK") == 0) {
    
   Serial.println("Correct!");
   index=0;
   delay(500);
  }
else if(strcmp (inData,"Telit")==0)   {
 Serial.println("Manufacturer OK");
 index=0;
delay(500); 
}
  else {
   
  }
}


void readserial(){
  
   while(Serial.available() > 0) 
								  
   {
	 if(index < 19) // One less than the size of the array
	 {
	     inChar = Serial.read(); 
	     inData[index] = inChar; 
	     index++; 
	     inData[index] = '\0'; 

	 }



   }
}

I don't know what's up with all that white space in your code. Makes it hard to read. But, your readSerial() function is lacking. It makes some assumptions that will often not be true.

It assumes that all the serial data that constitutes a packet will be available to read when it gets around to reading it. That assumption will likely be true when the phone responds with "OK", but when the responses get to be longer, it will not be true.

It also assumes that all the serial data that it reads belongs to a single packet. Again, this is a poor assumption. You need to figure out what the phone is using to separate packets. Carriage return? Line feed? Both? Some other symbol? Some non-printable value?

I used a terminal to communicate with the Telit andit only sends to the module the commands by checking a box after the text you write (+CR). So I guess it's using a Carriage Return way. This i am able to check from arduino by checking for "/n" each time i read from my serial?

Something I've done is to only store useful vharacter to the array something like
Raw = Serial.read();
if( raw! = 10 && raw != 13){
then code to store into array
you could probably do if raw is in between two number,( low end of ascii characters to high end of ascii characters)
that would omit useless characters so you can compare easily
btw 10 and 13 are return and newline I think, or atleast the two ascii control characters that I usually ignore