Hello. I have a problem which I can't figure out... In the datasheet of A6 module it says that every response from module will begin with \r\n and finish with \r\n also. So I decided to put \n as end marker for storing response from module to char array... The problem is that when I make a call to module the response from module should be like this
I know that because when i write the function update serial in this kind of way it works ok
void updateSerial()
{
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
}
}
But I decided to go with Robin's serial tutorial, so I wrote the function like this and this is where i am missing some characters after my phone number... The complete code is here... And this comes to me serial monitor when I make a call to A6, and as you can see, the characters behind my phone number are missing and I can't figure out why... I would appreciate any help for better understanding
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);
const char number[] = {'+', 'C', 'L', 'I', 'P', ':', ' ', '"', '3', '8', '5', '9', '8', '5', '6', '0', '9', '4', '5', '\0'};
boolean message = false;
const byte CharNumber = 20; // maximum numbers of bytes
char incomingChar[CharNumber]; // array for incoming message
char CharReceived = 0; //for storing the lenght of index/bytenumber
void receive() { //function for receiving data
static byte index = 0;
const char endflag = '\n'; // "n" indicates end of data
static byte endmarker = 0;
// while reciveing
while (mySerial.available() > 0 && message == false) // while we receiving
{
char in = mySerial.read();
if (in != endflag) { //if incoming char is not \n keep storing
incomingChar[index] = in;
index++;
if (index >= CharNumber) {
index = CharNumber - 1;
} //end if
} // end if
else if (in == endflag) {
message = true;
incomingChar[index] = '\0'; //terminate
CharReceived = index;
index = 0;
}
}
}
void showNewData() {
if (message == true) {
Serial.print("This just in ... ");
Serial.println(incomingChar);
message = false;
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {
uint8_t x = 0, answer = 0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Clean response buffer
while ( mySerial.available() > 0) mySerial.read(); // Wait for clean input buffer
mySerial.println(ATcommand); // Send the AT command
previous = millis();
// this loop waits for the answer
do {
// if there are data in the UART input buffer, reads it and checks for the asnwer
if (mySerial.available() != 0) {
response[x] = mySerial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL) {
answer = 1;
}
}
// Waits for the answer with time out
} while ((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
void updateSerial()
{
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
receive();
showNewData();
}
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and A6
mySerial.begin(9600);
Serial.println("Initializing...");
delay(3000);
while (sendATcommand("AT", "OK", 1000) == 0) {
delay(500);
mySerial.println("AT");
updateSerial();
}
mySerial.println("AT+CLIP=1"); // show the calling number
updateSerial();
}
void loop() {
updateSerial();
}