Cellular library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependents:   mtsas mtsas mtsas mtsas

Files at this revision

API Documentation at this revision

Comitter:
Vanger
Date:
Wed Jul 30 20:22:23 2014 +0000
Parent:
40:ecef43f87c7a
Child:
42:adcf25505b1c
Commit message:
Rewrote sendCommand() function in Cellular.cpp, and fixed failure to find radio type error in CellularFactory.cpp caused by unsolicited response #STN

Changed in this revision

Cellular/Cellular.cpp Show annotated file Show diff for this revision Revisions of this file
Cellular/CellularFactory.cpp Show annotated file Show diff for this revision Revisions of this file
Cellular/EasyIP.cpp Show annotated file Show diff for this revision Revisions of this file
Test/TestSMS.h Show annotated file Show diff for this revision Revisions of this file
--- a/Cellular/Cellular.cpp	Tue Jul 29 19:39:01 2014 +0000
+++ b/Cellular/Cellular.cpp	Wed Jul 30 20:22:23 2014 +0000
@@ -197,43 +197,48 @@
             return "";
         }
     }
-    int time_done = 0;
-    //Time in 100s of milliseconds maximum between character transmissions
-    const int MAX_INPUT_DELAY = 2;
-    int timer = 0;
-    size_t previous = 0;
+    mbed::Timer tmr;
     char tmp[256];
     tmp[255] = 0;
-    bool started = !echoMode;
     bool done = false;
+    tmr.start();
     do {
-        wait(0.1);
-        timer += 100;
+        wait(0.05);
         
-        previous = result.size();
         //Make a non-blocking read call by passing timeout of zero
         int size = io->read(tmp,255,0);    //1 less than allocated (timeout is instant)
         if(size > 0) {
             result.append(tmp, size);
         }
-        if(!started) {
-            //In Echo Mode (Command will have echo'd + 2 characters for \r\n)
-            if(result.size() > command.size() + 2) {
-                started = true;
-            }
-        } else {
-            //Uses an timer to make sure no characters are received
-            //within a period of time after the last recevied character.
-            if(result.size() == previous) {
-                time_done++;
-                if(time_done > MAX_INPUT_DELAY) {
-                    done = true;
-                }
-            } else {
-                time_done = 0;
+        
+        //If there is a response, check that the response is not finished with a response code
+        //OK, ERROR, CONNECT are the 3 most likely responses
+        if(result.size() > (command.size() + 2)) {
+            if(result.find("OK") != std::string::npos) {
+                //Found OK
+                done = true;
+            } else if(result.find("ERROR") != std::string::npos) {
+                //Found ERROR
+                done = true;
+            } else if(result.find("CONNECT") != std::string::npos) {
+                //Found CONNECT]
+                ////Could add socketOpened flag check here if CONNECT is found
+                done = true;
+            } else if(result.find("NO CARRIER") != std::string::npos) {
+                //Found NO CARRIER
+                done = true;
+            } else if(result.find("RING") != std::string::npos) {
+                done = true;
+            } else if(result.find("NO DIALTONE") != std::string::npos) {
+                done = true;
+            } else if(result.find("BUSY") != std::string::npos) {
+                done = true;
+            } else if(result.find("NO ANSWER") != std::string::npos) {
+                done = true;
             }
         }
-        if(timer >= timeoutMillis) {
+        
+        if(tmr.read_ms() >= timeoutMillis) {
             if (command != "AT" && command != "at") {
                 logWarning("sendCommand [%s] timed out after %d milliseconds", command.c_str(), timeoutMillis);
             }
--- a/Cellular/CellularFactory.cpp	Tue Jul 29 19:39:01 2014 +0000
+++ b/Cellular/CellularFactory.cpp	Wed Jul 30 20:22:23 2014 +0000
@@ -25,10 +25,15 @@
     
     /* "ATI4" gets us the model (HE910, DE910, etc) */
     for (int i = 0; i < 5; i++) {
-        model = sendCommand(io, "ATI4", 2000);
+        model = sendCommand(io, "ATI4", 3000);
         if (model.find("error") == string::npos && model.find("ERROR") == string::npos && !model.empty()) {
             /* didn't get an error - keep going */
-            break;
+            if(model.find("#STN") != string::npos) {
+                //If response found is from unsolicited response #STN: from the radio,
+                //then we got a "corrupted" response, and to try again.
+                continue;
+            }
+                break;
         }
         
         wait(1);
--- a/Cellular/EasyIP.cpp	Tue Jul 29 19:39:01 2014 +0000
+++ b/Cellular/EasyIP.cpp	Wed Jul 30 20:22:23 2014 +0000
@@ -45,7 +45,7 @@
 
     logDebug("radio type: %s", Cellular::getRadioNames(type).c_str());
     //Turns on the HW flow control, equiv. to AT&K
-    if(sendBasicCommand("AT&K=3", 2000) != MTS_SUCCESS) {
+    if(sendBasicCommand("AT&K3", 2000) != MTS_SUCCESS) {
         logWarning("Failed to set flow control to radio");
     }
     return true;
@@ -352,6 +352,13 @@
         logError("port out of range (0-65535)");
         return false;
     }
+    
+    if(type == MTSMC_EV3) {
+        if(!local_port) {
+            logDebug("Local port set to 1, port 0 not supported for MTSMC_EV3");
+            local_port = 1;
+        }
+    }
 
     //3) Check PPP connection
     if(!isConnected()) {
@@ -705,7 +712,7 @@
     io->txClear();
     
     std::string result;
-    unsigned int timeoutMillis = 20000; //20s
+    unsigned int timeoutMillis = 10000; //time in ms
     const int size_cmd = 3;
     //Attempt to write command
     wait(1.2); //Format for +++ command is 1 second wait, send +++, then another second wait
--- a/Test/TestSMS.h	Tue Jul 29 19:39:01 2014 +0000
+++ b/Test/TestSMS.h	Wed Jul 30 20:22:23 2014 +0000
@@ -22,7 +22,7 @@
 TestSMS::TestSMS() : TestCollection("TestSMS") {}
 
 void TestSMS::run() {
-    const char APN[] = "";
+    const char APN[] = "internet";
     
     string number;
     string response;