SoftwareSerial library use clarification

Hello,

I'd like to play with a Serial module (APC220) then I had a look at SoftwareSerial library and some examples on the web.

The thing that surprises me is that very often I can see functions used twice : once for Serial and once for the instantiated object ?

For instance on SoftwareSerial Example we can see :

Serial.println(...);
mySerial.println(...);

or :

  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());

What is the reason/need to do this twice once for Serial and once for the mySerial ?


On the other I can see the example of available() function which seems do that only once (make more sense to me) :

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() {
  if (mySerial.available()>0){
  mySerial.read();
 }
}

I read the docs but cannot find explanation on that.
Thanks for your help.
Tom

Serial.println(...);
mySerial.println(...);

Same function but different objects hence they are independent and are testing different serial inputs

if (mySerial.available()>0){

Tests the mySerial object to see whether data is available to be read. It is not testing the Serial object so that data may or may not be available there

Thanks for you reply.
I understand the steps of program (available()>0, etc...) but as we instantiate the library why do we have/need "different" objects ? In other languages/environments when I instantiate a class I use the resulting object (only).
In other word I was expecting to use only mySerial... and not Serial...

@tg2lt, how would you use 2 or 3 serial ports, if you named them the same thing?

but as we instantiate the library why do we have/need "different" objects ?

Maybe your confusion is caused by the fact that the Serial object does not need to be instantiated explicitly as it is done automatically by the compiler in the Arduino environment and is, therefore, immediately available

@OP, I am sharing with you my understanding which may be corrected/enhanced by the veterans.

1. You can have following declarations:

int x;
int y;

Here, there are two different variables; but, they are of the same data types (int).

2. You can have the following declarations:

struct myStruct     //myStruct is StructName/tag created using struct keyword    
{
   int x;
   char y;
   char myData[4];
};
myStruct var1;
myStruct var2;

Here, there are two different variables var1 and var2; which have the same data type -- the myStruct which contains 3 members.

3. In softwareSerial.h Library, we have the following declaration:

class SoftwareSerial        
{
    public:
        SoftwareSerial(uint8_t rxPin, uint8_t txPin, bool inverse_logic = false);
        void begin(long speed);
        bool listen();
        void end();
        virtual int read();
        virtual int available();
        virtual void flush();
        using Print::write;
        .................
     
     private:
        ............. 
};

Here, softwareSerial is a ClassName/tag created using class keyword. Now, we can use this ClassName/tag as a data type to declare different variables. These variables are called objects (in OOP's paradigm and there are reasons for saying so) on which we can apply methods (the member functions of SoftwareSerial) to get some tasks done.

SoftwareSerial mySerial1 (2, 3); //SRX, STX; constructor to assign digital pins to simulated UART  Port-1  
SoftwareSerial mySerial2 (4, 5);  //simulated (software based) SUART Port-2

char x = mySerial1.read();  //read() method is applied on object/SUART Port-1 named mySerial1.
char y = mySerial2.read();  //read() method is applied on object/SUART Port-2 named mySerial2.

4. Now, the tool chain is:
We use class keyword to create a ClassName/tag which contains functions and variables.
We use ClassName/tag to create different objects.
We apply methods (functions) on an object to accomplish different kinds of tasks.

OK I did not know that Serial object was instantiated implicitly by compiler.
And above example makes things more clear to me.
Thanks all.