Hello. I am learning how to create arduino Library from scratch and how to use classes. I want to create a class object which will use Stream or Serial to interact with my serial device.
I have made cpp and header files:
.cpp file :
And the .header file:
And then in my arduino .ino file, I create the class object:
I am then sending then initializing my serial device using my class function start_modem:
After the modem is initialised, I then try to communicate with the serial device by sending AT command using the Send_at_command function;
However, I think my code does not work properly as I am not getting a proper response back:

Perhaps the problem is with my send_at_command function? :
I have made cpp and header files:
.cpp file :
Code:
#include "custom_modem.h"
#include "Arduino.h"
custom_modem::custom_modem(HardwareSerial* serial)
{
_port = serial;
}
void custom_modem::start_modem(){
static_cast<HardwareSerial*>(_port)->begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
}
String custom_modem::send_AT_command()
{
String return_message;
static_cast<HardwareSerial*>(_port)->write("AT");
delay(1);
while( static_cast<HardwareSerial*>(_port)->available())
{
char c = static_cast<HardwareSerial*>(_port)->read();
return_message.concat(c);
}
return return_message;
}
Code:
#ifndef custom_modem_h
#define custom_modem_h
#include <Arduino.h>
#include <Stream.h>
#include <HardwareSerial.h>
#include <SoftwareSerial.h>
#define MODEM_TX 27
#define MODEM_RX 26
class custom_modem
{
public:
//custom_modem(SoftwareSerial& serial);
custom_modem(HardwareSerial* serial);
void start_modem();
String send_AT_command();
private:
Stream* _port;
//Stream& _serial;
};
#endif
And then in my arduino .ino file, I create the class object:
Code:
#define SerialAT Serial1
custom_modem modem(&SerialAT);
I am then sending then initializing my serial device using my class function start_modem:
Code:
modem.start_modem();
Code:
String answer_from_modem;
answer_from_modem= modem.send_AT_command();
Serial.print("answer=");
Serial.println(answer_from_modem);

Perhaps the problem is with my send_at_command function? :
Code:
String custom_modem::send_AT_command()
{
String return_message;
static_cast<HardwareSerial*>(_port)->write("AT");
delay(1);
while( static_cast<HardwareSerial*>(_port)->available())
{
char c = static_cast<HardwareSerial*>(_port)->read();
return_message.concat(c);
}
return return_message;
}