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.
Fork of GPRSInterface by
modem.cpp
00001 /* 00002 modem.cpp 00003 2014 Copyright (c) Seeed Technology Inc. All right reserved. 00004 00005 Author:lawliet zou(lawliet.zou@gmail.com) 00006 2014-2-24 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "modem.h" 00024 //Serial pc(USBTX,USBRX); 00025 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //MOSI, MISO, SCLK, SSEL. Tested on K64F, correct pins. 00026 AnalogIn LM35(PTB2); 00027 // added by Noah Milam 00028 void Modem::gprs_response() 00029 { 00030 printf("\nlistening for client\n"); 00031 char buffer[100]; 00032 int count = 0; 00033 00034 mkdir("/sd/mydir", 0777); // makes directory if needed 00035 FILE *fp = fopen("/sd/mydir/chemData.csv", "w"); // creats new file to write 00036 fprintf(fp,"phone Number,chem data, latitude,longitude\n"); // writes in a header for the table 00037 fclose(fp); // closes file 00038 00039 while(1) 00040 { 00041 if(serialModem.readable()) { 00042 00043 while(serialModem.readable()) { 00044 char c = serialModem.getc(); 00045 buffer[count++] = c; 00046 } 00047 } 00048 if(buffer[0] != '\0'){ 00049 buffer[count] = '\0'; 00050 FILE *fp = fopen("/sd/mydir/chemData.csv", "a"); // opens file to append it 00051 fprintf(fp,"%s\n",buffer);//writes to file 00052 fclose(fp); // closes file 00053 printf("%s \n",buffer); 00054 } 00055 for(int i = 0; i < count+2; i++) {buffer[i] = NULL;} 00056 count = 0; 00057 } 00058 } 00059 00060 void Modem::wait_for_sms(){ 00061 printf("waiting for message\n"); 00062 while(1){ 00063 if(serialModem.readable()){ 00064 return; 00065 } 00066 } 00067 } 00068 //end added by Noah Milam 00069 char Modem::readByte(void) 00070 { 00071 return serialModem.getc(); 00072 } 00073 00074 bool Modem::readable() 00075 { 00076 return serialModem.readable(); 00077 } 00078 00079 int Modem::readBuffer(char *buffer,int count, unsigned int timeOut) 00080 { 00081 int i = 0; 00082 timeCnt.start(); 00083 while(1) { 00084 while (serialModem.readable()) { 00085 char c = serialModem.getc(); 00086 buffer[i++] = c; 00087 if(i >= count)break; 00088 } 00089 if(i >= count)break; 00090 if(timeCnt.read() > timeOut) { 00091 timeCnt.stop(); 00092 timeCnt.reset(); 00093 break; 00094 } 00095 } 00096 return 0; 00097 } 00098 00099 void Modem::cleanBuffer(char *buffer, int count) 00100 { 00101 for(int i=0; i < count; i++) { 00102 buffer[i] = '\0'; 00103 } 00104 } 00105 00106 void Modem::sendCmd(const char* cmd) 00107 { 00108 serialModem.puts(cmd); 00109 } 00110 void Modem::sendCmdResp(const char* cmd) 00111 { 00112 serialModem.puts(cmd); 00113 getResp(); 00114 } 00115 void Modem::getResp() 00116 { 00117 char buffer[100]; 00118 int count = 0; 00119 timeCnt.start(); 00120 while(timeCnt.read() < 5) 00121 { while(serialModem.readable()) { 00122 char c = serialModem.getc(); 00123 buffer[count++] = c; 00124 } 00125 } 00126 timeCnt.stop(); 00127 timeCnt.reset(); 00128 buffer[count] = '\0'; 00129 printf("%s \n",buffer); 00130 for(int i = 0; i < count+2; i++) { 00131 buffer[i] = NULL; 00132 } 00133 count = 0; 00134 } 00135 00136 void Modem::store_response() 00137 { 00138 timeCnt.start(); 00139 while(timeCnt.read() < 20) 00140 { while(serialModem.readable()) 00141 { 00142 char c = serialModem.getc(); 00143 printf(&c); 00144 } 00145 } 00146 timeCnt.stop(); 00147 timeCnt.reset(); 00148 } 00149 void Modem::sendATTest(void) 00150 { 00151 sendCmdAndWaitForResp("AT\r\n","OK",DEFAULT_TIMEOUT,CMD); 00152 } 00153 00154 bool Modem::respCmp(const char *resp, unsigned int len, unsigned int timeout) 00155 { 00156 int sum=0; 00157 timeCnt.start(); 00158 00159 while(1) { 00160 if(serialModem.readable()) { 00161 char c = serialModem.getc(); 00162 sum = (c==resp[sum]) ? sum+1 : 0; 00163 if(sum == len)break; 00164 } 00165 if(timeCnt.read() > timeout) { 00166 timeCnt.stop(); 00167 timeCnt.reset(); 00168 return false; 00169 } 00170 } 00171 timeCnt.stop(); 00172 timeCnt.reset(); 00173 00174 return true; 00175 } 00176 00177 int Modem::waitForResp(const char *resp, unsigned int timeout,DataType type) 00178 { 00179 int len = strlen(resp); 00180 int sum=0; 00181 timeCnt.start(); 00182 00183 while(1) { 00184 if(serialModem.readable()) { 00185 char c = serialModem.getc(); 00186 sum = (c==resp[sum]) ? sum+1 : 0; 00187 if(sum == len)break; 00188 } 00189 if(timeCnt.read() > timeout) { 00190 timeCnt.stop(); 00191 timeCnt.reset(); 00192 return -1; 00193 } 00194 } 00195 timeCnt.stop(); 00196 timeCnt.reset(); 00197 00198 if(type == CMD) { 00199 while(serialModem.readable()) { 00200 char c = serialModem.getc(); 00201 } 00202 } 00203 00204 return 0; 00205 } 00206 00207 int Modem::sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type) 00208 { 00209 sendCmd(data); 00210 return waitForResp(resp,timeout,type); 00211 }
Generated on Tue Jul 12 2022 15:14:00 by
1.7.2