0

I am trying to make use of a SoftwareSerial variable in a function, but for some reason it is always unavailable. The various codes are given below.

gsm.h

#ifndef GSM_H_
#define GSM_H_
#include <SoftwareSerial.h>

struct gsm{

    char *message;
    char phone_number[20];

    void getText(SoftwareSerial serial, int index);
};


#endif


gsm.cpp

#include "gsm.h"
#include <string.h>
#include <arduino.h>
#include <SoftwareSerial.h>

void gsm::getText(SoftwareSerial serial, int index){

    char str[100];
    serial.print("AT+CMGR=1 \r");
    delay(250);
    if(serial.available()){
       Serial.print("serial is available");
       serial.readBytes(message, 100);
    }

}


test.ino

#include "gsm.h"
#include <SoftwareSerial.h>
#include <string.h>
#include <stdio.h>

SoftwareSerial mySerial(10, 11); // RX, TX

gsm gm;

void setup(){

   Serial.begin(9600);
   mySerial.begin(9600);
   gm.getText(mySerial,1);
   Serial.print(gm.message);
}

void loop(){

}

Serial.print(gm.message) is supposed to return what has been copied into message by serial.readBytes(message,100). But it seems the test for serial availability failed as the code inside if(serial.available()) don't get executed. Please i need help as i have been battling with this for some time now.

wokoro douye samuel
  • 2,194
  • 3
  • 15
  • 29

1 Answers1

0

try to use this:

if (mySerial.available()>0){
   mySerial.read();
}
Linh
  • 57,942
  • 23
  • 262
  • 279
stackmalux
  • 77
  • 11