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.
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!