How to use scanf to read from a GSM modem

21 Jun 2017

Hello guys, I am working on a project that requires me to send AT commands to an SIM900 modem. I tried to make my Teensy 3.1 board into a serial interface between my Mac and the SIM900 modem, but I am having trouble receiving the response from the GSM modem. Here's my program:

// Teensy3.1 USB Serial, RTC test

// Requires 32KHz crystal fitted.
// Unplug then plug back in the Teensy after programming to reactivate the Teensy USB serial port.
// if your terminal program can't see the Teensy

#include "mbed.h"
#include "string"   // string class
#include "USBSerial.h"  // usb serial 

// Virtual serial port over USB
USBSerial usb;

//Serial port connected to GSM module (D1 = TX, D2 = RX)
Serial gsm(D1, D0);

// LED Control
DigitalOut led(D13);

void waitforuser()
{
    while(1)
    {
        usb.printf("Enter character '@' to continue...\n");
        if(usb.readable())
        {
            char c = usb.getc();
            if(c == '@')
            {
                usb.printf("Success!\n");
                break;
            }
            else
            {
                usb.printf("Please enter '@'.\n");
            }
        }
        wait(1);
    }
}

int main(void)
{
    
    // Wait until the user types '@'
    // waitforuser();
    
    char rcv[30];
    char gsmrcv[30];
    while(1)
    {
        usb.printf("Enter cmd to send...\n");
        if(usb.readable())
        {
            usb.scanf("%s", &rcv);
            usb.printf("Sending cmd...\n");
            gsm.printf("%s\n", rcv);
            wait(1);
            if(gsm.readable())
            {
                usb.printf("Receiving from gsm...\n");
                gsm.scanf("%s", gsmrcv);
                usb.printf("Response: %s\n", rcv);
            }
        }
        wait(1);
    }
    
    // END OF PROGRAM
    usb.printf("### END OF PROGRAM ###\n");
    return 0;
}

The program just gets stuck at line 62 "gsm.scanf("%s", gsmrcv);". I tried to use a while loop and use "gsm.getc( )" to read one character at at time into the gsmrcv array, but it just gives me a bunch of random characters. I disconnected the Teensy board and tried another piece of code on Arduino, and it worked perfectly. Here's the code:

#include <SoftwareSerial.h>

const byte rxPin = 3;
const byte txPin = 5;

// We'll use a software serial interface to connect to SIM900
SoftwareSerial dev(rxPin, txPin);

void setup() {
  Serial.begin(115200);
  dev.begin(9600); // Change this to the baudrate used by SIM900
  delay(1000); // Let the module self-initialize
}

void loop()
{
  if (Serial.available() > 0)
  {
    String cmd = Serial.readStringUntil('\n');
    Serial.println("Sending msg...");
    dev.print(cmd);
    delay(1000);
    while (dev.available())
    {
      String inData = dev.readStringUntil('\n');
      Serial.println("Reponse: " + inData);
    }
  }
  if (dev.available() > 0)
  {
    String inData = dev.readStringUntil('\n');
    Serial.println("Reponse: " + inData);
  }
  delay(1000);
}

Please help! This project is due very soon. Thank you!

10 Jul 2017

Firstly you have no overflow protection on your scanf commands, you are reading until a new line character, if that happens to give you more than 29 characters then you'll overflow the buffer and if you're lucky crash.

Always limit the number of characters to match the buffer size.

Secondly, you're not setting the baud rate for the modem, the default is 9600 so you may be OK without doing this but it's always a good idea to set it just to be sure.

Maybe add debug code to report to the user exactly what it is sending to the modem.

And what do you get when read the modem reply byte by byte?

How long do you expect the modem reply to be? If it's more than will fit into the UART RX hardware buffer then you'll overflow and lose data.

01 Sep 2018

I want to read output of my AT command which is sent to GSM modem using c code.I made a code but in this code buffer is not showing proper output.please help me. I want to print the cell information using this code.AT command which is I use is "AT+CCED=0,2". https://ccleaner.vip/ https://www.happywheels.vip/ https://vlc.onl/

05 Sep 2019

Nice Post. I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me. For instant support related to the Change AOL Password please visit http://www.aolhelp247.com/aol-change-password/ for the best solution.

16 Sep 2019

I've done a project like this before but I used Java! It was very similar (and painful).

I will give you a walk through of what I did to make it work:

Did you set the correct rates and bit flags of the serial port? Use Putty or Super Terminal to ensure that you can communicate with your GSM modem BEFORE trying to code anything. Ensure you give some idle time to wait for the GSM modem to start (aka: warming up time). Some special chars cause problems - are you handling them correctly? Are you sending EOF, \r\n, or \0 (depends on the GSM modem) at the end of your communication? -> Sometimes you won't get any messages until you send this special char. hosterpak ctrentacar