Arduino IDE learning how to create library and classes with Stream or Serial

Thread Starter

zazas321

Joined Nov 29, 2015
893
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 :

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;
}
And the .header file:

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();
After the modem is initialised, I then try to communicate with the serial device by sending AT command using the Send_at_command function;
Code:
String answer_from_modem;
answer_from_modem= modem.send_AT_command();
Serial.print("answer=");
Serial.println(answer_from_modem);
However, I think my code does not work properly as I am not getting a proper response back:
1618397640873.png


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

Thread Starter

zazas321

Joined Nov 29, 2015
893
I hava maneged to create a class. I have ditched the Stream as it makes everything much more complicated. I am just using HardwareSerial instead ;
my cpp :
Code:
#include "custom_modem.h"
#include "Arduino.h"
#include "String.h"
/*
custom_modem::custom_modem(HardwareSerial* serial)
{
    _port = serial;
    _port(serial);
}
*/

custom_modem::custom_modem(HardwareSerial& serial): _port(serial)
{
}

void custom_modem::start_modem(){
    _port.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
}

String custom_modem::send_AT_command(char *command)
{
    String return_message; // Crate a string to store a return message
    _port.write(command); // Write an "AT" message to serial device. Expecting to receive "OK"
    delay(1);
     while(_port.available())
       {
          char c = _port.read(); // Read character at a time
          return_message.concat(c); // append a string with the character
       }
    return return_message;
}
And the header file:

Code:
#ifndef custom_modem_h
#define custom_modem_h
#include <Arduino.h>
#include <HardwareSerial.h>
#define MODEM_TX             27
#define MODEM_RX             26
class custom_modem
{
   public:
      //custom_modem(HardwareSerial* serial);
      custom_modem(HardwareSerial& serial);
      void start_modem();
      String send_AT_command(char *command);
   private:
      HardwareSerial _port;
};

#endif
Hopefully will be helpful to someone else :)
 
Top