storing variables in single array for transmission

Dependencies:   SDFileSystem dspmodified mbed

Fork of FTPGET_Merged by Pravin Magar

Revision:
21:9e71641aeac8
Parent:
20:764af4c48cf2
Child:
22:4141d6a13488
--- a/merged_code.cpp	Tue Jun 23 12:36:20 2015 +0000
+++ b/merged_code.cpp	Thu Jun 25 05:49:34 2015 +0000
@@ -134,6 +134,52 @@
 }
 
 //--------------------------------------------------------------------------------------------------//
+//                  Send AT Command (Compare with 2 responses)                                                    //
+//--------------------------------------------------------------------------------------------------//
+int sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, unsigned int timeout)
+{
+    int grs=0;
+    int answer=0;                                                                                   //x=0 and answer=0, if not put, AT+CCLK?\r is not executed
+    char response[300];
+    memset(response, '\0', 100);                                                                    // Initialize the string
+    wait_ms(100);
+    previous = time(NULL);
+    char dummy;
+    do
+    {
+        if(gsm.readable()!=0)
+        { 
+            dummy=gsm.getc();
+            //pc.putc(dummy);
+        } 
+    }
+    while((time(NULL) - previous) < 1);                                                             // Clean the input buffer
+    //pc.printf("\r\n");
+    gsm.printf("%s \r", ATcommand);                                                                 // Send the AT command
+    grs = 0;
+    previous = time(NULL);
+    do 
+    {                                                                                               // this loop waits for the answer
+        if(gsm.readable() != 0) 
+        {
+            response[grs] = gsm.getc();                                                             // if there are data in the UART input buffer, reads it and checks for the asnwer
+            pc.putc(response[grs]);
+            grs++;
+            if (strstr(response, expected_answer1) != NULL) 
+            {                                                                                       // check if the desired answer  is in the response of the module
+                    answer=1;
+            }
+            if (strstr(response, expected_answer2) != NULL) 
+            {                                                                                       // check if the desired answer  is in the response of the module
+                    answer=1;
+            }
+        }
+    } while((answer !=1) && ((time(NULL) - previous) < timeout));                                   // Waits for the asnwer with time out
+    //pc.printf("\r\nanswer %d\r\n",answer);
+    return answer;
+}
+
+//--------------------------------------------------------------------------------------------------//
 //                  iteration                                                     
 //--------------------------------------------------------------------------------------------------//
 
@@ -635,27 +681,22 @@
 //--------------------------------------------------------------------------------------------------//
 //                 GSM Initialization                                                               //
 //--------------------------------------------------------------------------------------------------//
-void gsm_init()
+bool gsm_init()
 {
     int cnt = 0;
-    while((sendATcommand("AT+CREG?", "+CREG: 1,1", 10,2) || sendATcommand("AT+CREG?", "+CREG: 1,5", 5,0)) == 0) 
+    while(sendATcommand2("AT+CREG?", "+CREG: 1,1", "+CREG: 1,5", 10)==0)      //ATcommand with dual response comparison
     {
-        if(sendATcommand("AT+CREG=1", "+CREG:1", 5,0)) 
+        if(sendATcommand2("AT+CREG=1", "+CREG:1", "+CREG:5", 5)) 
         {
-            pc.printf("creg is 1");
-            return;
-        }
-        else if(sendATcommand("AT+CREG=1", "+CREG:5", 5,0)) 
-        {
-            pc.printf("creg is 5");
-            return;
+            pc.printf("creg success");
+            return 1;
         }
         ++cnt;
         if(cnt > 5) 
         {
             pc.printf("GSM registration failed");
             reset_mod();
-            return;
+            return 0;
         }
     }
 }