1

Well, I am trying make a library for HMI display. display and Arduino comminicate with Serial. So I will so many times Serial port. Generally I will use Serial.write commend. if I examine other libraries, there are lots way to declare serial. Why and which is the best I want to learn.

there I have seen declaring Serial methods.

Method 1

in library .h file

nothing

in library .cpp file

#include <SoftwareSerial.h>

SoftwareSerial SIM(RX_PIN,TX_PIN);

void KapadokyaGSM::basla(){
    SIM.begin(9600);
  _tampon.reserve(255);
}

Method 2

in library .h file

public:
EasyNex(HardwareSerial& serial);
...
private:
HardwareSerial* _serial;
...

in library .cpp file

EasyNex::EasyNex(HardwareSerial& serial){  // Constructor's parameter is the Serial we want to use
_serial = &serial;
}

void EasyNex::begin(unsigned long baud){
_serial->begin(baud);  // We pass the initialization data to the objects (baud rate) default: 9600
delay(100);            // Wait for the Serial to initialize

Also I have seen declaring with Stream but now i didnt find example codes. So finally Can anyone tell me what is the best for declearing Serial in library. it can be for only hardware serial or both

1

2 Answers 2

4

As regards the use of Stream:

I usually use Stream instead of HardwareSerial because:

  • It allows the use of other serial devices, like SoftwareSerial or USBSerial that aren't "Hardware" UARTs.
  • It even allows the use of non-serial systems, like sending data over networks or wireless devices

However it means:

  • The user has to manually initialize their serial object / device as Stream has no facility for setting things like baud rates.
2
1

There is no best way, the first is using a global variable, being static. The second is dynamically (or at least to pass a serial instance).

It depends on your use

  • If you want your code always to work with the same declared serial (hardware or serial) instance, you can use the first method, as it is the easiest.
  • If you want your serial hardware port to be flexible (your code to work e.g. with a selectable hardware serial port on an Arduino Mega), you need the second.
  • If you want to your serial port to be either software or hardware, you can use the second method, but you need another type (not the mentioned HardwareSerial class. I don't know by head the parent class of SoftwareSerial and HardwareSerial (The Stream class you mentioned could be a common parent class).
5
  • 2
    there is no Serial base class
    – Juraj
    Commented Aug 22, 2021 at 11:21
  • Can i ask a last question, why second method uses "->" and Why not "." Such us _serial.begin(baud). Is this totally same
    – mehmet
    Commented Aug 22, 2021 at 12:32
  • 2
    If you have access to the object itself, e.g., Serial, you can say Serial.println(). If your library only has a pointer to the object, you have use ->, the dereference operator: serialPtr->println(). The result is the same: calling the object's 'println()' member. The difference is in how the compiled code gets you to that member.
    – JRobert
    Commented Aug 22, 2021 at 13:16
  • @Juraj Thanks for that addition (I was not sure, so I removed the comment about not being sure and the statement in my answer). Commented Aug 22, 2021 at 19:32
  • @mehmet, it is common C++ object member access you asked about
    – Juraj
    Commented Aug 22, 2021 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.