Serial communication

Hi everyone! I have a question... I'm a [very] beginner...
If a gsm module is connected to Arduino through RX/TX ports, are serial functions (e.g. serial.print, serial.write, serial available...) referred to the gsm module or serial monitor? Or: how I choose wether my funtion refers to one or to the other?
[Is "function" correct here?]
I'm trying to understand a sketch (found on a website) that controls something (e.g. relays, switch...) sending SMS.
But maybe I haven't studied enough and something is missing in my knowledge...
Can anyone help me? Thank you.

If you connect your GSM module to the RX and TX that are also connected to the USB-to-Serial adapter then you will have a conflict if you try to use both.

Usually you would create a software serial port with SoftwareSerial.h or use one of the Arduino boards that has a spare Serial port (MEGA, Leonardo, Micro).

Ok, thank you so much!
So, if nothing is connected to RX and TX, serial functions (serial.write, serial.print...) are referred to the serial monitor, right?
But if something IS connected, so they are referred to the external device, am I correct?
So there is something like a "serial channel" that can not be used by serial monitor and gsm module (or some other device) at the same time. Did I understand correctly?
I can't find anything that simply explains that...
Maybe I should understand what a "USB-to-Serial adapter" is...

salventre:
So, if nothing is connected to RX and TX, serial functions (serial.write, serial.print...) are referred to the serial monitor, right?
But if something IS connected, so they are referred to the external device, am I correct?

That's not correct.

The Rx and Tx pins (pins 0 and 1 on an Uno) can be used to connect directly to an external device but they are also connected to another microprocessor on the Uno board which sends the serial data via the USB cable to the Serial Monitor (or any other PC program). There is no programmable means to choose between these situations. Normally if you want to send data to the Serial Monitor you would not have anything connected to the Rx and Tx pins. Conversely, if you want to connect the Rx and Tx pins to something you would normally not use the Serial Monitor.

You can create a second serial port on any other pair of pins using the SoftwareSerial library.

...R

Ok, thank you.
So, now I'm getting (I think)...
If something is connected through RX and TX and I'm using serial.write, serial.print, etc. I'm sending data to both the external device and serial monitor, is this right?
So, I deduce that Uno cannot distinguish if data inputs (serial.available, etc.) come from serial monitor or from the external device, am I right?

salventre:
So, I deduce that Uno cannot distinguish if data inputs (serial.available, etc.) come from serial monitor or from the external device, am I right?

Correct.

And if the Arduino is sending serial output (for example with Serial.print() ) it cannot even tell if there is nothing connected either to Rx and Tx or via the USB cable.

...R

1. In Arduino UNO Board, the microcntroller is of type ATmega328P which has only one electronics based Serial Communication Port (COM Port /UART Port). This UART is usually connected (see Fig-1) with the Arduino IDE and Serial Monitor for down loading binary codes of a sketch and program debugging.


Figure-1:

(1) In Fig-1, we observe that there is no second UART Port in the UNO to connect any other external UART Port based device. In this case, the solution is (if you are not using MEGA which has 3 more UART Ports) to create a UART Port using software codes known as 'Software UART Port (SUART)'. This SUART Port behaves exactly as a UART Port except that the Baud Rate (Bd) is much lower which is about 9600.

(2) In Fig-1, we have shown a SUART Port using DPin-10 (SRX = Receive pin for SUART Port) and DPin-11 (TRX = Transmit pin for SUART Port). The SUART Port is automatically created when we include the following code line at the appropriate place (s) of the sketch. A SUART Port could be created using any valid DPins (digital IO pin).

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);  //Dpin-10 = SRX, DPin-11 = STX
SUART.begin(9600);

.

(3) To read charcaters coming to UNO over SUART Port, we may execute the following codes:

byte n = SUART.available();   //check if a character has arrived from external device
if(n !=0 )   //yes a character has arrived and get stored in FIFO Buffer
{
   char x = SUART.read();  //read the arrived character from FIFO Buffer and save in variable x.
   
}

Great! Now I get it! Thank you everyone!