user53790: 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

user31105: 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).


user4143: 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.


53790: <p>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 <strong>Serial.write</strong> commend. if I examine other libraries, there are lots way to declare serial. Why and which is the best I want to learn.</p>
<p>there I have seen declaring Serial methods.</p>
<p><strong>Method 1</strong></p>
<p>in library .h file</p>
<pre><code>nothing
</code></pre>
<p>in library .cpp file</p>
<pre><code>#include &lt;SoftwareSerial.h&gt;

SoftwareSerial SIM(RX_PIN,TX_PIN);

void KapadokyaGSM::basla(){
    SIM.begin(9600);
  _tampon.reserve(255);
}
</code></pre>
<p><strong>Method 2</strong></p>
<p>in library .h file</p>
<pre><code>public:
EasyNex(HardwareSerial&amp; serial);
...
private:
HardwareSerial* _serial;
...
</code></pre>
<p>in library .cpp file</p>
<pre><code>EasyNex::EasyNex(HardwareSerial&amp; serial){  // Constructor's parameter is the Serial we want to use
_serial = &amp;serial;
}

void EasyNex::begin(unsigned long baud){
_serial-&gt;begin(baud);  // We pass the initialization data to the objects (baud rate) default: 9600
delay(100);            // Wait for the Serial to initialize
</code></pre>
<p>Also I have seen declaring with Stream but now i didnt find example codes. So finally <strong>Can anyone tell me what is the best for declearing Serial in library.</strong> it can be for only hardware serial or both</p>
 31105: <p>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).</p>
<p>It depends on your use</p>
<ul>
<li>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.</li>
<li>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.</li>
<li>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 <code>HardwareSerial</code> class. I don't know by head the parent class of <code>SoftwareSerial</code> and <code>HardwareSerial</code> (The <code>Stream</code> class you mentioned could be a common parent class).</li>
</ul>
 4143: <p>As regards the use of <code>Stream</code>:</p>
<p>I usually use <code>Stream</code> instead of <code>HardwareSerial</code> because:</p>
<ul>
<li>It allows the use of other serial devices, like <code>SoftwareSerial</code> or <code>USBSerial</code> that aren't &quot;Hardware&quot; UARTs.</li>
<li>It even allows the use of non-serial systems, like sending data over networks or wireless devices</li>
</ul>
<p>However it means:</p>
<ul>
<li>The user has to manually initialize their serial object / device as <code>Stream</code> has no facility for setting things like baud rates.</li>
</ul>
