a library to use GPRS like ethernet or wifi, which makes it possible to connect to the internet with your GPRS module

Dependencies:   BufferedSerial

Dependents:   ThinkSpeak_Test roam_v1 roam_v2 finalv3

Fork of GPRSInterface by wei zou

Committer:
yihui
Date:
Tue Mar 10 02:57:52 2015 +0000
Revision:
10:652c23d73373
Parent:
9:38800611a613
Child:
12:47488369a980
use BufferedSerial to increase stability;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:8ccbd963e74d 1 /*
lawliet 0:8ccbd963e74d 2 modem.h
lawliet 0:8ccbd963e74d 3 2014 Copyright (c) Seeed Technology Inc. All right reserved.
lawliet 0:8ccbd963e74d 4
lawliet 0:8ccbd963e74d 5 Author:lawliet zou(lawliet.zou@gmail.com)
lawliet 0:8ccbd963e74d 6 2014-2-24
lawliet 0:8ccbd963e74d 7
lawliet 0:8ccbd963e74d 8 This library is free software; you can redistribute it and/or
lawliet 0:8ccbd963e74d 9 modify it under the terms of the GNU Lesser General Public
lawliet 0:8ccbd963e74d 10 License as published by the Free Software Foundation; either
lawliet 0:8ccbd963e74d 11 version 2.1 of the License, or (at your option) any later version.
lawliet 0:8ccbd963e74d 12
lawliet 0:8ccbd963e74d 13 This library is distributed in the hope that it will be useful,
lawliet 0:8ccbd963e74d 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
lawliet 0:8ccbd963e74d 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lawliet 0:8ccbd963e74d 16 Lesser General Public License for more details.
lawliet 0:8ccbd963e74d 17
lawliet 0:8ccbd963e74d 18 You should have received a copy of the GNU Lesser General Public
lawliet 0:8ccbd963e74d 19 License along with this library; if not, write to the Free Software
lawliet 0:8ccbd963e74d 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lawliet 0:8ccbd963e74d 21 */
lawliet 0:8ccbd963e74d 22
lawliet 0:8ccbd963e74d 23 #ifndef __MODEM_H__
lawliet 0:8ccbd963e74d 24 #define __MODEM_H__
lawliet 0:8ccbd963e74d 25
yihui 10:652c23d73373 26 #include "BufferedSerial.h"
lawliet 0:8ccbd963e74d 27
lawliet 0:8ccbd963e74d 28 #define DEFAULT_TIMEOUT 5
lawliet 0:8ccbd963e74d 29
lawliet 0:8ccbd963e74d 30 enum DataType {
lawliet 0:8ccbd963e74d 31 CMD = 0,
lawliet 0:8ccbd963e74d 32 DATA = 1,
lawliet 0:8ccbd963e74d 33 };
lawliet 0:8ccbd963e74d 34
lawliet 0:8ccbd963e74d 35 /** Modem class.
lawliet 0:8ccbd963e74d 36 * Used for Modem communication. attention that Modem module communicate with MCU in serial protocol
lawliet 0:8ccbd963e74d 37 */
lawliet 0:8ccbd963e74d 38 class Modem
lawliet 0:8ccbd963e74d 39 {
lawliet 0:8ccbd963e74d 40
lawliet 0:8ccbd963e74d 41 public:
lawliet 0:8ccbd963e74d 42 /** Create Modem Instance
lawliet 0:8ccbd963e74d 43 * @param tx uart transmit pin to communicate with Modem
lawliet 0:8ccbd963e74d 44 * @param rx uart receive pin to communicate with Modem
lawliet 0:8ccbd963e74d 45 * @param baudRate baud rate of uart communication
lawliet 0:8ccbd963e74d 46 */
lawliet 0:8ccbd963e74d 47 Modem(PinName tx, PinName rx, int baudRate) : serialModem(tx, rx) {
lawliet 0:8ccbd963e74d 48 serialModem.baud(baudRate);
lawliet 0:8ccbd963e74d 49 };
lawliet 6:464ccda1ebcc 50
yihui 10:652c23d73373 51 BufferedSerial serialModem;
lawliet 1:7298a7950f65 52 protected:
lawliet 0:8ccbd963e74d 53 /** Power on Modem
lawliet 0:8ccbd963e74d 54 */
lawliet 0:8ccbd963e74d 55 void preInit(void);
lawliet 0:8ccbd963e74d 56
lawliet 0:8ccbd963e74d 57 /** check serialModem is readable or not
lawliet 0:8ccbd963e74d 58 * @returns
lawliet 0:8ccbd963e74d 59 * true on readable
lawliet 0:8ccbd963e74d 60 * false on not readable
lawliet 0:8ccbd963e74d 61 */
lawliet 0:8ccbd963e74d 62 bool readable();
lawliet 0:8ccbd963e74d 63
lawliet 0:8ccbd963e74d 64 /** read one byte from serialModem
lawliet 0:8ccbd963e74d 65 * @returns
lawliet 0:8ccbd963e74d 66 * one byte read from serialModem
lawliet 0:8ccbd963e74d 67 */
lawliet 0:8ccbd963e74d 68 char readByte(void);
lawliet 0:8ccbd963e74d 69
lawliet 0:8ccbd963e74d 70 /** read from Modem module and save to buffer array
lawliet 0:8ccbd963e74d 71 * @param buffer buffer array to save what read from Modem module
lawliet 0:8ccbd963e74d 72 * @param count the maximal bytes number read from Modem module
lawliet 0:8ccbd963e74d 73 * @param timeOut time to wait for reading from Modem module
lawliet 0:8ccbd963e74d 74 * @returns
lawliet 0:8ccbd963e74d 75 * 0 on success
lawliet 0:8ccbd963e74d 76 * -1 on error
lawliet 0:8ccbd963e74d 77 */
lawliet 0:8ccbd963e74d 78 int readBuffer(char* buffer,int count, unsigned int timeOut);
lawliet 0:8ccbd963e74d 79
lawliet 0:8ccbd963e74d 80
lawliet 0:8ccbd963e74d 81 /** clean Buffer
lawliet 0:8ccbd963e74d 82 * @param buffer buffer to clean
lawliet 0:8ccbd963e74d 83 * @param count number of bytes to clean
lawliet 0:8ccbd963e74d 84 */
lawliet 0:8ccbd963e74d 85 void cleanBuffer(char* buffer, int count);
lawliet 0:8ccbd963e74d 86
lawliet 0:8ccbd963e74d 87 /** send AT command to Modem module
lawliet 0:8ccbd963e74d 88 * @param cmd command array which will be send to GPRS module
lawliet 0:8ccbd963e74d 89 */
lawliet 0:8ccbd963e74d 90 void sendCmd(const char* cmd);
yihui 9:38800611a613 91
yihui 9:38800611a613 92 /** send data to Modem module
yihui 9:38800611a613 93 * @param data array which will be send to GPRS module
yihui 9:38800611a613 94 * @param len data length
yihui 9:38800611a613 95 */
yihui 9:38800611a613 96 void sendData(const char* data, int len);
lawliet 0:8ccbd963e74d 97
lawliet 0:8ccbd963e74d 98 /**send "AT" to Modem module
lawliet 0:8ccbd963e74d 99 */
lawliet 0:8ccbd963e74d 100 void sendATTest(void);
lawliet 0:8ccbd963e74d 101
lawliet 0:8ccbd963e74d 102 /** compare the response from GPRS module with a string
lawliet 0:8ccbd963e74d 103 * @param resp buffer to be compared
lawliet 0:8ccbd963e74d 104 * @param len length that will be compared
lawliet 0:8ccbd963e74d 105 * @param timeout waiting seconds till timeout
lawliet 0:8ccbd963e74d 106 */
lawliet 0:8ccbd963e74d 107 bool respCmp(const char *resp, unsigned int len, unsigned int timeout);
lawliet 0:8ccbd963e74d 108
lawliet 0:8ccbd963e74d 109 /** check Modem module response before time out
lawliet 0:8ccbd963e74d 110 * @param *resp correct response which Modem module will return
lawliet 0:8ccbd963e74d 111 * @param *timeout waiting seconds till timeout
lawliet 0:8ccbd963e74d 112 * @returns
lawliet 0:8ccbd963e74d 113 * 0 on success
lawliet 0:8ccbd963e74d 114 * -1 on error
lawliet 0:8ccbd963e74d 115 */
lawliet 0:8ccbd963e74d 116 int waitForResp(const char *resp, unsigned int timeout,DataType type);
lawliet 0:8ccbd963e74d 117
lawliet 0:8ccbd963e74d 118 /** send AT command to GPRS module and wait for correct response
lawliet 0:8ccbd963e74d 119 * @param *cmd AT command which will be send to GPRS module
lawliet 0:8ccbd963e74d 120 * @param *resp correct response which GPRS module will return
lawliet 0:8ccbd963e74d 121 * @param *timeout waiting seconds till timeout
lawliet 0:8ccbd963e74d 122 * @returns
lawliet 0:8ccbd963e74d 123 * 0 on success
lawliet 0:8ccbd963e74d 124 * -1 on error
lawliet 0:8ccbd963e74d 125 */
lawliet 0:8ccbd963e74d 126 int sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type);
lawliet 0:8ccbd963e74d 127
lawliet 0:8ccbd963e74d 128 Timer timeCnt;
lawliet 0:8ccbd963e74d 129
lawliet 0:8ccbd963e74d 130 private:
lawliet 0:8ccbd963e74d 131
lawliet 0:8ccbd963e74d 132 };
lawliet 0:8ccbd963e74d 133
lawliet 0:8ccbd963e74d 134 #endif