9

From the sample code

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  Serial.println("Goodnight moon!");
  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

What is the difference between, why use 2 types of calls to make AT command calls.

SoftwareSerial mySerial (10,11) mySerial.begin(10,11)

and

Serial.begin(9600)

Edit, this answers my question in my own words based on @jfpoilpret response.

  1. It is important to initialize both Serial and SoftwareSerial, since SoftwareSerial is used to send the actual commands to the GSM, and Serial is used to view all the commands and their responses on the debug port of the GSM
  2. The pins on the GSM and Arduino are used to connect the the 2, and pass on actual commands. While the RS232 is only used for debugging. We can however connect a RS232 to USB connector so as to connect the GSM to the desktop(or laptop)
  3. The code in the loop is essential to help convert all actual commands to the debugging RS232 port, and the other way around, so as to enable us to use a tool like cutecom (on ubuntu) to pass commands to the GSM serial input pins.

Important Note : When using cutecom ensure that you select CR line end to send commands.

2 Answers 2

10

All Atmel MCU, on which Arduino models are based, include special hardware for serial communication; this part of the MCU is called USART (stands for Universal Synchronous/Asynchronous Receiver/Transmitter).

Based on the Arduino model you have, you may have only one (eg on UNO) or more USART (leonardo, mega...) on the board MCU.

Each MCU USART is mapped to specific pins of the board, you cannot change those pins.

On Arduino, if you use the monitor to send debug information to your PC, then one USART is used, this is when you used Serial.

If you need more than one serial communication link (e.g. because you have some devices that communicate through serial) but your Arduino model has only one USART, then you have to use the library SoftwareSerial that "simulates" the job of an USART only by software. Hence it is much less optimized than the hardware USART.

One advantage of SoftwareSerial is that you can map it to any pair of pins you like.

Going to your code sample:

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

Obviously here, Serial is used for debugging (sending info to the serial monitor of your PC), whereas mySerial is used to communciate with the GSM device.

So this sketch is used to allow you to type AT commands on the monitor, which will then be forwarded to the GSM device; conversely, any strings returned from the GSM device will be echoed to the serial monitor.

1
  • I'm using SIM808 module with Arduino Uno, one thing that I don't understand is when I use TX1 and RX0 of Arduino to communicate to GSM module, I get error on Arduino software during upload process: something like 'avrude ....' . But when I use SoftwareSerial setting tx and rx pins to pin7 and pin8, there's no problem on uploading when pins are connected. One other thing is that when different versions of Arduino is used how do you communicate to GSM device if you don't use SoftwareSerial? I've seen code for other versions of Arduino and SIM908 at which SoftwareSerial lib. is not used at all.
    – RegarBoy
    Commented Feb 21, 2017 at 15:04
0
#include <SoftwareSerial.h>

SoftwareSerial GSMSerial(7, 8); //use pins 7 and 8 as RX and TX

void setup() {
   GSMSerial.begin(19200);               // the GPRS/GSM baud rate   
   Serial.begin(19200);                 // the GPRS/GSM baud rate   
}

void loop() {
    if(Serial.available()) {
       //to send commands received from Serial Monitor to GSM module
       GSMSerial.print((char)Serial.read());
    }
    else  if(GSMSerial.available()) {
       //To print data responded from GSM module to the Serial Monitor.  
       Serial.print((char)GSMSerial.read());
    }
}

In the code above inside loop function first if statement is needed to talk to GMS module from Serial Monitor, e.g. when you type AT in the monitor and press enter your command gets sent to the GSM module.

The second if statement else if is to print whatever is received or responded from GSM module e.g. GSM module responds the received command like AT and its result OK after you send command AT to the module.

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.