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
Diff: cellular/Cellular.cpp
- Revision:
- 122:5f95f81a8b03
- Parent:
- 110:8f3149c99112
- Child:
- 123:a84f6b89132b
--- a/cellular/Cellular.cpp Tue Dec 31 23:41:59 2013 +0000 +++ b/cellular/Cellular.cpp Thu Jan 02 15:45:52 2014 +0000 @@ -33,7 +33,7 @@ if (dtr != NULL) { dtr->write(1); } - + delete dcd; delete dtr; } @@ -43,16 +43,16 @@ if (io == NULL) { return false; } - + if(dcd) { delete dcd; - dcd = NULL; + dcd = NULL; } if(dtr) { delete dtr; dtr = NULL; } - + if (DCD != NC) { // the radio will raise and lower this line dcd = new DigitalIn(DCD); //PTA4 - KL46 @@ -65,7 +65,7 @@ dtr->write(0); } instance->io = io; - + return (test() == SUCCESS); } @@ -81,9 +81,9 @@ if(isConnected()) { return true; } - + Timer tmr; - + //Check Registration: AT+CREG? == 0,1 tmr.start(); do { @@ -102,12 +102,12 @@ int rssi = getSignalStrength(); printf("[DEBUG] Signal strength: %d\r\n", rssi); if(rssi == 99) { - printf("[WARNING] No Signal ... waiting\r\n"); - wait(1); + printf("[WARNING] No Signal ... waiting\r\n"); + wait(1); } else { - break; + break; } - } while(tmr.read() < 30); + } while(tmr.read() < 30); //AT#CONNECTIONSTART: Make a PPP connection printf("[DEBUG] Making PPP Connection Attempt. APN[%s]\r\n", apn.c_str()); @@ -173,11 +173,11 @@ size_t pos = result.find("STATE:"); if(pos != std::string::npos) { result = Text::getLine(result, pos + sizeof("STATE:"), pos); - printf("[WARNING] Internal PPP state tracking differs from radio (CONNECTED:%s)\r\n", result.c_str()); + printf("[WARNING] Internal PPP state tracking differs from radio (CONNECTED:%s)\r\n", result.c_str()); } else { - printf("[ERROR] Unable to parse radio state: [%s]\r\n", result.c_str()); + printf("[ERROR] Unable to parse radio state: [%s]\r\n", result.c_str()); } - + } pppConnected = false; } @@ -236,7 +236,7 @@ printf("[DEBUG] PPP connection established\r\n"); } } - + //Set Local Port if(local_port != 0) { //Attempt to set local port @@ -292,7 +292,7 @@ sOpenSocketCmd = "AT#OUDP"; sMode = "UDP"; } - + string response = sendCommand(sOpenSocketCmd, 30000); if (response.find("Ok_Info_WaitingForData") != string::npos) { printf("[INFO] Opened %s Socket [%s:%d]\r\n", sMode.c_str(), address.c_str(), port); @@ -307,6 +307,10 @@ bool Cellular::isOpen() { + if(io->readable()) { + printf("[DEBUG] Assuming open, data available to read.\n\r"); + return true; + } return socketOpened; } @@ -327,27 +331,27 @@ return false; } - - + + if(io->write(ETX, 1000) != 1) { printf("[ERROR] Timed out attempting to close socket\r\n"); return false; } - + Timer tmr; int counter = 0; char tmp[256]; tmr.start(); do { if(socketOpened == false) { - break; + break; } read(tmp, 256, 1000); } while(counter++ < 10); - + io->rxClear(); io->txClear(); - + socketOpened = false; return true; } @@ -555,9 +559,9 @@ code = sendBasicCommand("AT", 1000); if(code == SUCCESS) { basicRadioComms = true; - break; + break; } else { - wait(1); + wait(1); } } while(tmr.read() < 15); @@ -565,7 +569,7 @@ printf("[ERROR] Unable to communicate with the radio\r\n"); return FAILURE; } - + return SUCCESS; } @@ -836,7 +840,7 @@ do { wait(0.1); timer += 100; - + 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)