Passing a Vector (string) to a functions

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all,

 

Please I need your Help, because I am a beginner in C and also didn't get it in the Arduino forum.

 

I am trying to control a GSM modem to send and receive SMS's to read a temperature sensor and switch ON or OFF a motor. I send the AT commands to the modem and need to look for the answer, as well as, read SMS's the split the information.

 

I already read books about C trying to understand Pointers and Vectors, but, I am a lot confused ( vectors do not need pointers because the name is already pointing to the [0] of the vector, etc.) and I do not find any example similar to what i am using.

 

At the moment I sent AT command to the modem, read the response but I can not compare it.

 

Again your help is appreciated because I spend already a week looking for a solution. 

 

Thanks in advance.

Manuel 

 

below is a summary of the code used:

 

I get the following error: undefined reference to 'strstr (  char *, const char * )'

 

#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 * );

char *OK;
char ok_str[]={"OK\n\r"};

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

byte power= 8;
//byte rx_finished = 0;

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

  OK = strstr (receivedchars, ok_str);
  if(OK ){
    Serial.println("OK received");
  }

 showNewData();
  
  while(1){ // to avoid keeping senn the command continuesly
  }
}



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();
        newData = true;
      }
    }
    
  }
  rx_state = 1;
  return (rx_state); 
} 


// this routine is only to debug porposes

void showNewData() {
  if (newData = true){
    Serial.println(receivedchars);
    receivedchars[0]='\0';
    newData = false;
  }
}

 

What   

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that the term used in 'C' language circles is "array" - rather than "vector"

 

An array is a contiguous area of storage for a number of items all of the same type.

 

char my_array[3]; // An array containing 3 elements of type char

 

Indeed, the name of an array can be used as a pointer to the array's first element.

 

char * my_char_pointer;  // A pointer to something of type 'char'


// So the following two are equivalent:
my_char_pointer = &my_array[0];
my_char_pointer = my_array;

 

http://c-faq.com/aryptr/aryptreq...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is most odd. You are including string.h and strstr() is contained in libc.a that C always links with so I've no idea why you'd get "undefined reference" which basically means the linker cannot find the function - yet it should be libc.a 

 

What's more, this can't be about C++ linkage and overloading strstr() as the .h will be defining it extern "C" 

 

Most odd. 

 

EDIT: Oh,  just wait a minute, why do you have a local declaration of strstr() in your code? That will have C++ linkage and will override the one declared in string.h, that's the fault here. 

Last Edited: Wed. Jun 3, 2015 - 07:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Awneil and Clawson,

 

Thanks for your inputs. If I include the function prototype "char * strstr (  char *, const char * );"  the error msg is : undefined reference o 'strstr (  char *, const char * ).

 

According to what you say I must incled the "# include < libc.a>.

 

I did it, but, I continue to have the same error. I know that I have the "OK\n\r" in the receivedchars  string but the result is always NULL.

 

If you can help a litle more I will appreciated.

 

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

ManuelSilva wrote:
If I include the function prototype "char * strstr (  char *, const char * );"  the error msg is : undefined reference o 'strstr (  char *, const char * ).

That's what clawson was telling you - you need to remove that prototype!

 

Quote:
According to what you say I must incled the "# include < libc.a>.

No! Absolutely not!!

 

libc.a is a ready-compiled binary library - you cannot #include it in a source file!!

 

Just remove the spurious local prototype.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Jun 3, 2015 - 08:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Aweil, thanks again.

 

I already removed the function prototype and the "# include < libc.a>.  I compiled the sketch without errors, but, it still do not work. I have the code below to test the function. The function "showNewData();" prints the chars received and the "OK\n\r" is there. 

Thanks agains.

Manuel

OK = strstr (receivedchars, "OK\n\r");
  if(OK != NULL){
    Serial.println("OK received");
  }

   showNewData();

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ManuelSilva wrote:
the "OK\n\r" is there.

Are you sure?

 

In particular, are you sure that the final 2 characters are really '\n' and '\r' in that order ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Awneil, yes, the last 2 chars are 0x0D, 0X0A.

 

Please apologize, now, that you ask, I notice they are wrong sequence.

 

I already changed the sketch, compile it and the function is working correctly.

 

Many thanks for your help. It solved me a problems that I am fighting quite a while.

 

Best regards,

Manuel