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
-
The basic concept that you're asking about is called dependency injection.chrylis -cautiouslyoptimistic-– chrylis -cautiouslyoptimistic-2021年08月23日 00:50:38 +00:00Commented Aug 23, 2021 at 0:50
2 Answers 2
As regards the use of Stream
:
I usually use Stream
instead of HardwareSerial
because:
- It allows the use of other serial devices, like
SoftwareSerial
orUSBSerial
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.
-
Can you show me a tutorial, or github library which is using Stream.mehmet– mehmet2021年08月22日 18:12:35 +00:00Commented Aug 22, 2021 at 18:12
-
1@mehmet try this one: github.com/MajenkoLibraries/ICSC/tree/master/srcMajenko– Majenko2021年08月22日 18:14:13 +00:00Commented Aug 22, 2021 at 18:14
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 ofSoftwareSerial
andHardwareSerial
(TheStream
class you mentioned could be a common parent class).
-
2there is no Serial base class2021年08月22日 11:21:22 +00:00Commented 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 samemehmet– mehmet2021年08月22日 12:32:11 +00:00Commented Aug 22, 2021 at 12:32
-
2If you have access to the object itself, e.g.,
Serial
, you can saySerial.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– JRobert2021年08月22日 13:16:57 +00:00Commented 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).Michel Keijzers– Michel Keijzers2021年08月22日 19:32:35 +00:00Commented Aug 22, 2021 at 19:32
-
@mehmet, it is common C++ object member access you asked about2021年08月22日 19:39:22 +00:00Commented Aug 22, 2021 at 19:39
Explore related questions
See similar questions with these tags.