Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem dspmodified mbed
Revision 21:9e71641aeac8, committed 2015-06-25
- Comitter:
- lalitkumar
- Date:
- Thu Jun 25 05:49:34 2015 +0000
- Parent:
- 20:764af4c48cf2
- Child:
- 22:4141d6a13488
- Commit message:
- gsm init improved with simultaneous dual response comparison for ATcommand (sendATcommand2)
Changed in this revision
| merged_code.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- 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;
}
}
}