Report
10 months, 1 week ago.

How to read responses from Sim900

Hi,

I'm trying to write a simple program that will allow me to see the responses from the Sim900 chip in a terminal in my PC.

I am currently using the code below. My problem is that is seems that this condition:

while (SIM900.readable())

is not true when the function is called. I can therefore never see the response from the chip. Do you have any advice for me?

Thanking you in advance.

Kind regards

#include "mbed.h"
#include <string>

Serial pc(SERIAL_TX, SERIAL_RX); // pc comunication
Serial SIM900(PA_9, PA_10);   //tx, rx SIM 900
string result;
char x;

DigitalOut my_led(LED1);
DigitalOut sim_power(PB_3);

void clearString()
{
    result.clear();
}

void callback_rx()
{
    pc.printf("\r\n Callback Start\n");
    while (SIM900.readable()) {
        x = SIM900.getc();
        pc.printf("x %d",x);
        result += x;
        pc.putc(x); // print the answer from SIM900
        pc.printf("\r\n Callback Done\n");
    }
}

void power()
{
    sim_power.write(1); // accension gsm
    wait(1);
    sim_power.write(0);
    wait(13);
 //       SIM900.printf("ATE0\r\n");  // DISABLE ECHO
 //   wait(1);
}

int main()
{
    power();
    wait_ms(1000);
    pc.printf("\r\n GSM 900 TEST\n");
    wait_ms(1000);
    SIM900.attach(&callback_rx);
    wait_ms(1000);
    SIM900.baud(9600); //

    while(1) {
        
        wait(1);
        my_led = !my_led;
        wait(1);
        my_led = !my_led;
        
        pc.printf("\r\n Send AT\n");
        clearString();
        SIM900.printf("AT"); //hello
        wait_ms(100);
        clearString();
    }
}
Comment on this question

1 Answer

10 months ago.

Hi Koos,

I would recommend that you do not do a print to serial(printf) in a interrupt subroutine. Printing out to serial take a very long time so another interrupt might happen while you are waiting to print to serial. Second, it seem that you missing an argument when you called the attach function.

attach function

SIM900.attach(&callback_rx, Serial::RxIrq); //this will link it when there is an interrupt on the Serial Rx

You can reference this Serial_interrupts program example: https://os.mbed.com/users/4180_1/code/Serial_interrupts/

Please let me know if you have any questions!

- Peter, team Mbed

If this solved your question, please make sure to click the "Thanks" link below!

Hi Peter,

Thank you very much for your assistance. I have implemented your suggestions. Unfortunately, I am still unable to display the response from the Sim900 chip on my terminal. My amended code is below. Do you have any suggestions for me? Thanking you in advance. Kind regards Koos

#include "mbed.h"
#include <string>

Serial pc(SERIAL_TX, SERIAL_RX); // pc comunication
Serial SIM900(PA_9, PA_10);   //tx, rx SIM 900
string result;
char x;

DigitalOut my_led(LED1);
DigitalOut sim_power(PB_3);

void clearString()
{
    result.clear();
}

void callback_rx()
{
        while (SIM900.readable()) 
    {
        result += x;
        pc.putc(x); // print the answer from SIM900
    }
}

void power()
{
    wait(3);
    pc.printf("\r\n Start Program\n");
    pc.printf("\r\n Power On\n");
    sim_power.write(1); // accension gsm
    wait(1);
    sim_power.write(0);
    pc.printf("\r\n Power Off\n");
    pc.printf("\r\n Waiting for Network...\n");
    wait(13);
//       SIM900.printf("ATE0\r\n");  // DISABLE ECHO
//   wait(1);
}

int main()
{
    power();
    wait_ms(500);
    pc.printf("\r\n GSM 900 TEST\n");
    wait_ms(500);
    SIM900.attach(&callback_rx, Serial::RxIrq);
    wait_ms(500);
    SIM900.baud(9600); //

    while(1) {

        wait(1);
        my_led = !my_led;

        pc.printf("\r\n Send AT\n");
        clearString();
        SIM900.printf("AT"); //hello
    }
}
posted by Koos van der Wat 22 Jun 2018

Hi Peter,

I have modified my code and it is now doing what I wanted. Please see below. Thanks again for your assistance.

Kind regards

Koos

#include "mbed.h"
#include <string.h>

Serial pc(SERIAL_TX, SERIAL_RX); // pc comunication
Serial gsm(PA_9, PA_10);   //tx, rx SIM 900

DigitalOut my_led(LED1);
DigitalOut sim_power(PB_3);

void power()
{
    wait(3);
    pc.printf("\r\n Start Program\n");
    pc.printf("\r\n Power On\n");
    sim_power.write(1); // accension gsm
    wait(1);
    sim_power.write(0);
    pc.printf("\r\n Power Off\n");
    pc.printf("\r\n Waiting for Network...\n");
    wait(5);
}

int main() {

    gsm.baud(9600);
    pc.baud(9600);

    char buf[40];
    char buf1[40];

power();

pc.printf("\r\n");
pc.printf("Sim900 GSM Test\r\n");        

    gsm.printf("AT\r\n");
    gsm.scanf("%s",buf);
    pc.printf("%s\r\n",buf);
    gsm.scanf("%s",buf1);
    pc.printf("%s\r\n",buf1);

    while(1) {
        wait(1);
        my_led = !my_led;
        }   
}
posted by Koos van der Wat 23 Jun 2018

Hello Koos,

Yes I am glad to see that your code work. It seem that you just need to print out the "\r\n" every time you write out to the SIM900. That will tell the SIM900 that this is the end of the command line.

Please let me know if you have any questions!

- Peter, team Mbed

If this solved your question, please make sure to click the "Thanks" link below!

posted by Peter Nguyen 25 Jun 2018

To post an answer, please log in.