Passing arrays to functions (pointers)

Hello all,

I your help to solve a problem of find a string in a substring. I have being trying, but, I always no not get a ‘NULL’ character and the string I want to find is there.

I am trying to communicate with a modem GSM sending commands and look for the received answers.

Your help is appreciated.

Thanks
Manuel

#include <SoftwareSerial.h>
#include <string.h> 


 boolean newData = false;

#define rx_pin 2
#define tx_pin 3
const byte num_chars = 100;
char receivedchars [num_chars]; 
unsigned long timeout;
byte rx_ret =0;

char * strstr (  char *, const char * );


SoftwareSerial mySerial(rx_pin, tx_pin);  //rx, tx

byte power= 8;


 void setup()  {
   // define pin modes for tx, rx:
   pinMode(rx_pin, INPUT);
   pinMode(tx_pin, OUTPUT);
   pinMode(power, OUTPUT);
   // set the data rate for the SoftwareSerial port
   mySerial.begin(9600);
   Serial.begin(9600);
   delay(200);
   Serial.println("<Arduino is ready>");
   digitalWrite(power, HIGH);
   delay (2000);
   digitalWrite(power, LOW);
   delay(500);
   mySerial.flush(); // erase

 char *p_receivedchars;
   p_receivedchars = &receivedchars[0];
 }
 
 void loop()
 {
  boolean newData = false;

 mySerial.println("AT");
 rx_ret = read_input_data(175);

 if(rx_ret){
   Serial.println("was here");
 }
 if (is_resp_received("OK\n\r")){
 
    Serial.println("response ok");
    
 }
 
 while(1){}
 }
 


 byte read_input_data(int timeout)
{
  unsigned long prev_time = millis();
  byte num_bytes = 0;
  byte i =0;
  //delay(timeout);
  byte rx_state = 0;
 
 
  while((mySerial.available()) || ((unsigned long)(millis()- prev_time) <= timeout)){
   
      num_bytes = mySerial.available();
      //Serial.println(num_bytes);   
      while(num_bytes)
      {
        num_bytes--;
      if(i <  num_chars){
          receivedchars [i] = mySerial.read();
          i++;
          receivedchars [i] = '\0'; //end the string with null char        
        }
        else
        {
        mySerial.read();// clean the buffer
        newData = true;
        //return (rx_state);
        //Serial.println(receivedchars);
      }
    }
   }

  rx_state = 1;
 return (rx_state); 
} 
 



 void showNewData() {
          if (newData = true){
          Serial.println(receivedchars);
          receivedchars[0]='\0';
          newData = false;
          }
         }
         
        
         
byte is_resp_received(char const *compare_string)
 {
  char *ch;
  byte ret_val = 0;
  Serial.println("I am in the function");
  if(newData = true) {
  
    ch = strstr((char *) receivedchars, compare_string);
    Serial.println(receivedchars);
    if (ch != NULL) {
      ret_val = 1;
       Serial.println("I am in the function 1");
	
    }
	else
	{

	}
  }
  Serial.println("I am in the function 2");
  return (ret_val);
}

You declared your own function strstr() but then you don't define it anywhere. I expect you meant to use the version of strstr() that's defined by string.h. If there's an error when you don't include that declaration then there's something else wrong.

You have actually printed the receivedchars, to show that something was received?

 char *p_receivedchars;
   p_receivedchars = &receivedchars[0];

This is useless. This variable goes out of scope immediately.

  if(newData = true) {

Assigning true to newData is probably NOT what you want to do here.

= != ==

    else
    {

    }

How useful!

PaulS and MorganS,

Thanks for your inputs.

But it do not gave me much help.

Manuel

MorganS,

Yes, I have Serial.println(receivedchars); to verify if something was received.

Thanks, Manuel

You should post your revised code. Please don't go back and edit the first post like some people do.

Aarg,

Thanks.

I changed the code according to the sugestion but it doesn’t work.

Manuel

#include <SoftwareSerial.h>
#include <string.h> 


 boolean newData = false;

#define rx_pin 2
#define tx_pin 3
const byte num_chars = 100;
char receivedchars [num_chars]; 
unsigned long timeout;
byte rx_ret =0;

char * strstr (  char *, const char * );


SoftwareSerial mySerial(rx_pin, tx_pin);  //rx, tx

byte power= 8;


 void setup()  {
   // define pin modes for tx, rx:
   pinMode(rx_pin, INPUT);
   pinMode(tx_pin, OUTPUT);
   pinMode(power, OUTPUT);
   // set the data rate for the SoftwareSerial port
   mySerial.begin(9600);
   Serial.begin(9600);
   delay(200);
   Serial.println("<Arduino is ready>");
   digitalWrite(power, HIGH);
   delay (2000);
   digitalWrite(power, LOW);
   delay(500);
   mySerial.flush(); // erase

  }
 
 void loop()
 {
  boolean newData = false;

 mySerial.println("AT");
 rx_ret = read_input_data(175);

 if(rx_ret){
   Serial.println("was here");  //test the passing points
 }
 if (is_resp_received("OK\n\r")){  // function call
 
    Serial.println("response ok");
    
 }
 
 while(1){}
 }
 


 byte read_input_data(int timeout)
{
  unsigned long prev_time = millis();
  byte num_bytes = 0;
  byte i =0;
  //delay(timeout);
  byte rx_state = 0;
 
 
  while((mySerial.available()) || ((unsigned long)(millis()- prev_time) <= timeout)){
   
      num_bytes = mySerial.available();
      //Serial.println(num_bytes);   
      while(num_bytes)
      {
        num_bytes--;
      if(i <  num_chars){
          receivedchars [i] = mySerial.read();
          i++;
          receivedchars [i] = '\0'; //end the string with null char        
        }
        else
        {
        mySerial.read();// clean the buffer
        newData = true;
        //return (rx_state);
        //Serial.println(receivedchars);
      }
    }
   }

  rx_state = 1;
 return (rx_state); 
} 
 



 void showNewData() {
          if (newData = true){
          Serial.println(receivedchars);
          receivedchars[0]='\0';
          newData = false;
          }
         }
         
        
         
byte is_resp_received(char const *compare_string)
 {
  char *ch;
  byte ret_val = 0;
  Serial.println("I am in the function");
  if(newData == true) {
  
    ch = strstr((char *) receivedchars, compare_string);
    Serial.println(receivedchars);
    if (ch != NULL) {
      ret_val = 1;
       Serial.println("I am in the function 1");
	
    }
	  }
  Serial.println("I am in the function 2");
  return (ret_val);
}

Hello all!

I have a function to read (mySerial.read()) the response commands from a GSM Modem.

I want to implement a function “strstr” to check the if the GSM Modem response is the correct one, but, I wasn’t able to do it in spite of reading some books, because the examples are completely different.

Thanks in advance.
Manuel

The code regarding the function is below:

 char *strstr(char *receivedchars, char char_ok); // In this I do not know how to get the return value


//I tryed this, but, gives a lot of compile errors:

 char str_ok[]="OK\n\r";
 
 if(char *strstr(char *receivedchars, str_ok) != NULL){
   Serial.println("Ok respponse");
 }

// Also I tryed this but also get compiling errors:

char *ptr
 ptr = strstr(receivedchars, "OK\n\r");


// The code to read the serial input is the following:

 byte read_input_data(int timeout)
{
  unsigned long prev_time = millis();
  byte num_bytes = 0;
  byte i =0;
  byte rx_state = 0;
 
   

  while((mySerial.available()) || ((unsigned long)(millis()- prev_time) <= timeout)){
   
      num_bytes = mySerial.available();
      //Serial.println(num_bytes);   
      while(num_bytes)
      {
        num_bytes--;
      if(i <  num_chars){
          receivedchars [i] = mySerial.read();
          i++;
          receivedchars [i] = '\0'; //end the string with null char        
        }
        else
        {
        mySerial.read();
        newData = true;
        //return (rx_state);
        //Serial.println(receivedchars);
      }
    }
  }
  rx_state = 1;
 return (rx_state); 
}

while(1){}

What do you expect this to do ?

char *strstr(char *receivedchars, char char_ok); // In this I do not know how to get the return value

This function definition seems misconceived.

If you have a string of received characters, and you want to determine whether that string contains some particular substring, then the function is going to require information about two things: the whole string to search, and the substring to look for. It is going to require 2 pointers to char, one for each of those two things.

The result of the function is going to either be a boolean ( true if the substring is there, false if it isn't ), or alternatively, the location in the string at which the substring was matched, and some arbitrary value ( -1 is often used ), if the substring was not matched.

Why not use the strstr function that already exists? Or at least pick a different name if you want to reinvent it.

//I tryed this, but, gives a lot of compile errors:

char str_ok[]="OK\n\r";

if(char *strstr(char *receivedchars, str_ok) != NULL){ Serial.println("Ok respponse"); }

Well, of course you got a lot of compile errors. Your function arguments are completely wrong and your function return value is completely wrong.

Instead of writing random nonsense and expecting it work, you will need to learn some of the basics of C/C++ language syntax before you make any progress.

Delta_G: Why not use the strstr function that already exists? Or at least pick a different name if you want to reinvent it.

Which "function that already exists" is that ?

// Also I tryed this but also get compiling errors:

char *ptr ptr = strstr(receivedchars, "OK\n\r");

This is almost what you need to do. The declaration of your pointer variable "ptr" needs to be ended by a semicolon.

http://www.cplusplus.com/reference/cstring/strstr/

You will see that the definition of the function here, is NOT the same as what you wrote.

Michinyon, thanks for your reply.

The while() is just to the program (Arduino) not keeping send the commands to the GSM Modem, it is only for debugging proposes.

Thanks for your inputs I am going to try again. I have been reading the www.cplusplus.com, but the examples I found are all in the main() function. I tried it but didn’t work.

Thanks again,
Manuel

while(1){}

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  strncpy (pch,"sample",6);
  puts (str);
  return 0;
}

strstr is a standard library function that you can use anywhere you are writing c++ code. The fact that they called in from a function called main and you want to call it from a different function has no bearing on anything. You can use it in whatever function you'd like.

This looks like a nasty case of cargo-cult programming. Fortunately it is a curable disease.

My comments earlier assumed that he knew what the <string.h> library contained, because he created a function name matching one of the functions in that library.

The C++ reference describes the contents of the libraries very well. (It’s not so good on basic syntax.) Go back to the C++ documentation for the string.h library and pick out a function that will do what you want.