A library for talking to Multi-Tech's Cellular SocketModem Devices.

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Revision:
68:c490e4a51778
Parent:
67:1003b410f781
Child:
71:82205735732b
--- a/cellular/Cellular.cpp	Mon Dec 23 19:55:31 2013 +0000
+++ b/cellular/Cellular.cpp	Tue Dec 24 01:15:44 2013 +0000
@@ -312,17 +312,7 @@
     int bytesRead = 0;
     
     if(timeout >= 0) {
-        Timer tmr;
-        tmr.start();
-        do {
-            int available = io->readable();
-            if (available > 0) {
-                int size = MIN(available, max - bytesRead);
-                bytesRead += io->read(&data[bytesRead], size);
-            } else {
-                wait(0.05);   
-            }
-        } while (tmr.read_ms() <= timeout && bytesRead < max);
+        bytesRead = io->read(data, max, static_cast<unsigned int>(timeout));
     } else {
         bytesRead = io->read(data, max);
     }
@@ -356,7 +346,6 @@
         bytesRead = index;
     }
     
-    printf("[DEBUG] Scanning received data for socket closed message\r\n");
     //Scan for socket closed message
     for(size_t i = 0; i < bytesRead; i++) {
         if(data[i] == 'O') {
@@ -569,7 +558,7 @@
     return UNKNOWN;
 }
 
-Cellular::Code Cellular::sendBasicCommand(string command, int timeoutMillis, ESC_CHAR esc)
+Cellular::Code Cellular::sendBasicCommand(const std::string& command, unsigned int timeoutMillis, ESC_CHAR esc)
 {
     if(socketOpened) {
         printf("[ERROR] socket is open. Can not send AT commands\r\n");    
@@ -735,7 +724,7 @@
 }
 
 
-string Cellular::sendCommand(string command, int timeoutMillis, ESC_CHAR esc)
+string Cellular::sendCommand(const std::string& command, unsigned int timeoutMillis, ESC_CHAR esc)
 {
     if(io == NULL) {
         printf("[ERROR] MTSBufferedIO not set\r\n");
@@ -746,24 +735,32 @@
         return "";
     }
 
-    int size = command.size() + 1;
-    char cmd[size];
-    strcpy(cmd, command.c_str());
-    if (esc == CR) {
-        cmd[size -1] = '\r';
-    } else if (esc == CTRL_Z) {
-        cmd[size -1] = 0x1A;
-    } else if(esc == NONE) {
-        cmd[size -1] = '\0';
-    }
-
     io->rxClear();
     io->txClear();
     std::string result;
-    int status = io->write(cmd, size);
-    int available = io->readable();
-    int previous = -1;
+    
+    //Attempt to write command
+    if(io->write(command.data(), command.size(), timeoutMillis) != command.size()) {
+        //Failed to write command
+        printf("[ERROR] failed to send command to radio within %d milliseconds\r\n", timeoutMillis);
+        return "";
+    }
+    
+    //Send Escape Character
+    if (esc == CR) {
+        if(io->write('\r', timeoutMillis) != 1) {
+            printf("[ERROR] failed to send '\\r' to radio within %d milliseconds\r\n", timeoutMillis);
+            return "";
+        }
+    } else if (esc == CTRL_Z) {
+        if(io->write(0x1A, timeoutMillis) != 1) {
+            printf("[ERROR] failed to send 'CTRL+Z' to radio within %d milliseconds\r\n", timeoutMillis);
+            return "";
+        }
+    }
+
     int timer = 0;
+    size_t previous = 0;
     char tmp[256];
     tmp[255] = 0;
     bool started = !echoMode;
@@ -771,9 +768,7 @@
     do {
         wait(.1);
         timer = timer + 100;
-        previous = available;
-        available = io->readable();
-
+        previous = result.size();
         int size = io->read(tmp,255);    //1 less than allocated
         if(size > 0) {
             result.append(tmp, size);
@@ -785,7 +780,7 @@
                 started = true;
             }
         } else {
-            done =  (available == previous);
+            done =  (result.size() == previous);
         }
         if(timer >= timeoutMillis) {
             printf("[WARNING] sendCommand timed out after %d milliseconds\r\n", timeoutMillis);