Hello,
I have connected a GSM modem to pin no 2,3 of the mega and trying to read data from modem and display it on serial monitor, for testing purpose i have made a call through modem and it works fine but i am unable to read data from it . I have used some code from the link Serial Input Basics - Programming Questions - Arduino Forum
which gives details of reading a string from serial monitor. here is my code
#include<SoftwareSerial.h>
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial mySerial(rxPin,txPin);
void initCall();
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void setup() {
delay(20000);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);
delay(500);
initCall();
Serial.println("Serial call");
delay(20000);
mySerial.println("ATD>SM1");
// this works fine and calls number which is located in sim
delay(45000);
Serial.println("Call has been made");
}
void loop() {
}
void initCall()
{
mySerial.println("ATZ");
recvWithEndMarker();
showNewData(); // expected result is OK
delay(2000);
mySerial.println("ATE0");
recvWithEndMarker();
showNewData(); // expected result is OK
delay(2000);
mySerial.println("AT+CNMI=1,0,0,0,0");
recvWithEndMarker();
showNewData(); // expected result is OK
delay(2000);
mySerial.println("AT+CRC=1");
recvWithEndMarker();
showNewData();// expected result is OK
delay(2000);
mySerial.println("AT+CMGF=1");
recvWithEndMarker();
showNewData();// expected result is OK
delay(2000);
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
// if (Serial.available() > 0) {
while (mySerial.available() > 0 && newData == false) {
rc = mySerial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}