MODEM SIM900 socket websocket and command sms

Dependencies:   Pulse SDFileSystem TinyJpgDec mbed-rtos mbed

Committer:
metabi814
Date:
Fri Jun 12 10:07:59 2015 +0000
Revision:
0:214db1fdfc95
sim900 socket and websocket project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
metabi814 0:214db1fdfc95 1 /*
metabi814 0:214db1fdfc95 2 modem.cpp
metabi814 0:214db1fdfc95 3 2014 Copyright (c) Seeed Technology Inc. All right reserved.
metabi814 0:214db1fdfc95 4
metabi814 0:214db1fdfc95 5 Author:lawliet zou(lawliet.zou@gmail.com)
metabi814 0:214db1fdfc95 6 2014-2-24
metabi814 0:214db1fdfc95 7
metabi814 0:214db1fdfc95 8 This library is free software; you can redistribute it and/or
metabi814 0:214db1fdfc95 9 modify it under the terms of the GNU Lesser General Public
metabi814 0:214db1fdfc95 10 License as published by the Free Software Foundation; either
metabi814 0:214db1fdfc95 11 version 2.1 of the License, or (at your option) any later version.
metabi814 0:214db1fdfc95 12
metabi814 0:214db1fdfc95 13 This library is distributed in the hope that it will be useful,
metabi814 0:214db1fdfc95 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
metabi814 0:214db1fdfc95 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
metabi814 0:214db1fdfc95 16 Lesser General Public License for more details.
metabi814 0:214db1fdfc95 17
metabi814 0:214db1fdfc95 18 You should have received a copy of the GNU Lesser General Public
metabi814 0:214db1fdfc95 19 License along with this library; if not, write to the Free Software
metabi814 0:214db1fdfc95 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
metabi814 0:214db1fdfc95 21 */
metabi814 0:214db1fdfc95 22
metabi814 0:214db1fdfc95 23 #include "modem.h"
metabi814 0:214db1fdfc95 24
metabi814 0:214db1fdfc95 25 char Modem::readByte(void)
metabi814 0:214db1fdfc95 26 {
metabi814 0:214db1fdfc95 27 return serialModem.getc();
metabi814 0:214db1fdfc95 28 }
metabi814 0:214db1fdfc95 29
metabi814 0:214db1fdfc95 30 bool Modem::readable()
metabi814 0:214db1fdfc95 31 {
metabi814 0:214db1fdfc95 32 return serialModem.readable();
metabi814 0:214db1fdfc95 33 }
metabi814 0:214db1fdfc95 34
metabi814 0:214db1fdfc95 35 int Modem::readBuffer(char *buffer,int count, unsigned int timeOut)
metabi814 0:214db1fdfc95 36 {
metabi814 0:214db1fdfc95 37 int i = 0;
metabi814 0:214db1fdfc95 38 timeCnt.start();
metabi814 0:214db1fdfc95 39 while(1) {
metabi814 0:214db1fdfc95 40 while (serialModem.readable()) {
metabi814 0:214db1fdfc95 41 char c = serialModem.getc();
metabi814 0:214db1fdfc95 42 buffer[i++] = c;
metabi814 0:214db1fdfc95 43 if(i >= count)break;
metabi814 0:214db1fdfc95 44 }
metabi814 0:214db1fdfc95 45 if(i >= count)break;
metabi814 0:214db1fdfc95 46 if(timeCnt.read() > timeOut) {
metabi814 0:214db1fdfc95 47 timeCnt.stop();
metabi814 0:214db1fdfc95 48 timeCnt.reset();
metabi814 0:214db1fdfc95 49 break;
metabi814 0:214db1fdfc95 50 }
metabi814 0:214db1fdfc95 51 }
metabi814 0:214db1fdfc95 52 return 0;
metabi814 0:214db1fdfc95 53 }
metabi814 0:214db1fdfc95 54
metabi814 0:214db1fdfc95 55 void Modem::cleanBuffer(char *buffer, int count)
metabi814 0:214db1fdfc95 56 {
metabi814 0:214db1fdfc95 57 for(int i=0; i < count; i++) {
metabi814 0:214db1fdfc95 58 buffer[i] = '\0';
metabi814 0:214db1fdfc95 59 }
metabi814 0:214db1fdfc95 60 }
metabi814 0:214db1fdfc95 61
metabi814 0:214db1fdfc95 62 void Modem::sendCmd(const char* cmd)
metabi814 0:214db1fdfc95 63 {
metabi814 0:214db1fdfc95 64 serialModem.puts(cmd);
metabi814 0:214db1fdfc95 65 }
metabi814 0:214db1fdfc95 66
metabi814 0:214db1fdfc95 67 void Modem::sendATTest(void)
metabi814 0:214db1fdfc95 68 {
metabi814 0:214db1fdfc95 69 sendCmdAndWaitForResp("AT\r\n","OK",DEFAULT_TIMEOUT,CMD);
metabi814 0:214db1fdfc95 70 }
metabi814 0:214db1fdfc95 71
metabi814 0:214db1fdfc95 72 bool Modem::respCmp(const char *resp, unsigned int len, unsigned int timeout)
metabi814 0:214db1fdfc95 73 {
metabi814 0:214db1fdfc95 74 int sum=0;
metabi814 0:214db1fdfc95 75 timeCnt.start();
metabi814 0:214db1fdfc95 76
metabi814 0:214db1fdfc95 77 while(1) {
metabi814 0:214db1fdfc95 78 if(serialModem.readable()) {
metabi814 0:214db1fdfc95 79 char c = serialModem.getc();
metabi814 0:214db1fdfc95 80 sum = (c==resp[sum]) ? sum+1 : 0;
metabi814 0:214db1fdfc95 81 if(sum == len)break;
metabi814 0:214db1fdfc95 82 }
metabi814 0:214db1fdfc95 83 if(timeCnt.read() > timeout) {
metabi814 0:214db1fdfc95 84 timeCnt.stop();
metabi814 0:214db1fdfc95 85 timeCnt.reset();
metabi814 0:214db1fdfc95 86 return false;
metabi814 0:214db1fdfc95 87 }
metabi814 0:214db1fdfc95 88 }
metabi814 0:214db1fdfc95 89 timeCnt.stop();
metabi814 0:214db1fdfc95 90 timeCnt.reset();
metabi814 0:214db1fdfc95 91
metabi814 0:214db1fdfc95 92 return true;
metabi814 0:214db1fdfc95 93 }
metabi814 0:214db1fdfc95 94
metabi814 0:214db1fdfc95 95 int Modem::waitForResp(const char *resp, unsigned int timeout,DataType type)
metabi814 0:214db1fdfc95 96 {
metabi814 0:214db1fdfc95 97 int len = strlen(resp);
metabi814 0:214db1fdfc95 98 int sum=0;
metabi814 0:214db1fdfc95 99 timeCnt.start();
metabi814 0:214db1fdfc95 100
metabi814 0:214db1fdfc95 101 while(1) {
metabi814 0:214db1fdfc95 102 if(serialModem.readable()) {
metabi814 0:214db1fdfc95 103 char c = serialModem.getc();
metabi814 0:214db1fdfc95 104 sum = (c==resp[sum]) ? sum+1 : 0;
metabi814 0:214db1fdfc95 105 if(sum == len)break;
metabi814 0:214db1fdfc95 106 }
metabi814 0:214db1fdfc95 107 if(timeCnt.read() > timeout) {
metabi814 0:214db1fdfc95 108 timeCnt.stop();
metabi814 0:214db1fdfc95 109 timeCnt.reset();
metabi814 0:214db1fdfc95 110 return -1;
metabi814 0:214db1fdfc95 111 }
metabi814 0:214db1fdfc95 112 }
metabi814 0:214db1fdfc95 113 timeCnt.stop();
metabi814 0:214db1fdfc95 114 timeCnt.reset();
metabi814 0:214db1fdfc95 115
metabi814 0:214db1fdfc95 116 if(type == CMD) {
metabi814 0:214db1fdfc95 117 while(serialModem.readable()) {
metabi814 0:214db1fdfc95 118 char c = serialModem.getc();
metabi814 0:214db1fdfc95 119 }
metabi814 0:214db1fdfc95 120 }
metabi814 0:214db1fdfc95 121
metabi814 0:214db1fdfc95 122 return 0;
metabi814 0:214db1fdfc95 123 }
metabi814 0:214db1fdfc95 124
metabi814 0:214db1fdfc95 125 int Modem::sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type)
metabi814 0:214db1fdfc95 126 {
metabi814 0:214db1fdfc95 127 sendCmd(data);
metabi814 0:214db1fdfc95 128 return waitForResp(resp,timeout,type);
metabi814 0:214db1fdfc95 129 }