tester
Fork of GPRSInterface by
GPRS/modem/modem.h@1:7298a7950f65, 2014-02-25 (annotated)
- Committer:
- lawliet
- Date:
- Tue Feb 25 05:56:19 2014 +0000
- Revision:
- 1:7298a7950f65
- Parent:
- 0:8ccbd963e74d
- Child:
- 6:464ccda1ebcc
version 1.0(update some code)
Who changed what in which revision?
User | Revision | Line number | New 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 | |
lawliet | 0:8ccbd963e74d | 26 | #include "mbed.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 | 1:7298a7950f65 | 50 | |
lawliet | 1:7298a7950f65 | 51 | protected: |
lawliet | 0:8ccbd963e74d | 52 | /** Power on Modem |
lawliet | 0:8ccbd963e74d | 53 | */ |
lawliet | 0:8ccbd963e74d | 54 | void preInit(void); |
lawliet | 0:8ccbd963e74d | 55 | |
lawliet | 0:8ccbd963e74d | 56 | /** check serialModem is readable or not |
lawliet | 0:8ccbd963e74d | 57 | * @returns |
lawliet | 0:8ccbd963e74d | 58 | * true on readable |
lawliet | 0:8ccbd963e74d | 59 | * false on not readable |
lawliet | 0:8ccbd963e74d | 60 | */ |
lawliet | 0:8ccbd963e74d | 61 | bool readable(); |
lawliet | 0:8ccbd963e74d | 62 | |
lawliet | 0:8ccbd963e74d | 63 | /** read one byte from serialModem |
lawliet | 0:8ccbd963e74d | 64 | * @returns |
lawliet | 0:8ccbd963e74d | 65 | * one byte read from serialModem |
lawliet | 0:8ccbd963e74d | 66 | */ |
lawliet | 0:8ccbd963e74d | 67 | char readByte(void); |
lawliet | 0:8ccbd963e74d | 68 | |
lawliet | 0:8ccbd963e74d | 69 | /** read from Modem module and save to buffer array |
lawliet | 0:8ccbd963e74d | 70 | * @param buffer buffer array to save what read from Modem module |
lawliet | 0:8ccbd963e74d | 71 | * @param count the maximal bytes number read from Modem module |
lawliet | 0:8ccbd963e74d | 72 | * @param timeOut time to wait for reading from Modem module |
lawliet | 0:8ccbd963e74d | 73 | * @returns |
lawliet | 0:8ccbd963e74d | 74 | * 0 on success |
lawliet | 0:8ccbd963e74d | 75 | * -1 on error |
lawliet | 0:8ccbd963e74d | 76 | */ |
lawliet | 0:8ccbd963e74d | 77 | int readBuffer(char* buffer,int count, unsigned int timeOut); |
lawliet | 0:8ccbd963e74d | 78 | |
lawliet | 0:8ccbd963e74d | 79 | |
lawliet | 0:8ccbd963e74d | 80 | /** clean Buffer |
lawliet | 0:8ccbd963e74d | 81 | * @param buffer buffer to clean |
lawliet | 0:8ccbd963e74d | 82 | * @param count number of bytes to clean |
lawliet | 0:8ccbd963e74d | 83 | */ |
lawliet | 0:8ccbd963e74d | 84 | void cleanBuffer(char* buffer, int count); |
lawliet | 0:8ccbd963e74d | 85 | |
lawliet | 0:8ccbd963e74d | 86 | /** send AT command to Modem module |
lawliet | 0:8ccbd963e74d | 87 | * @param cmd command array which will be send to GPRS module |
lawliet | 0:8ccbd963e74d | 88 | */ |
lawliet | 0:8ccbd963e74d | 89 | void sendCmd(const char* cmd); |
lawliet | 0:8ccbd963e74d | 90 | |
lawliet | 0:8ccbd963e74d | 91 | /**send "AT" to Modem module |
lawliet | 0:8ccbd963e74d | 92 | */ |
lawliet | 0:8ccbd963e74d | 93 | void sendATTest(void); |
lawliet | 0:8ccbd963e74d | 94 | |
lawliet | 0:8ccbd963e74d | 95 | /** compare the response from GPRS module with a string |
lawliet | 0:8ccbd963e74d | 96 | * @param resp buffer to be compared |
lawliet | 0:8ccbd963e74d | 97 | * @param len length that will be compared |
lawliet | 0:8ccbd963e74d | 98 | * @param timeout waiting seconds till timeout |
lawliet | 0:8ccbd963e74d | 99 | */ |
lawliet | 0:8ccbd963e74d | 100 | bool respCmp(const char *resp, unsigned int len, unsigned int timeout); |
lawliet | 0:8ccbd963e74d | 101 | |
lawliet | 0:8ccbd963e74d | 102 | /** check Modem module response before time out |
lawliet | 0:8ccbd963e74d | 103 | * @param *resp correct response which Modem module will return |
lawliet | 0:8ccbd963e74d | 104 | * @param *timeout waiting seconds till timeout |
lawliet | 0:8ccbd963e74d | 105 | * @returns |
lawliet | 0:8ccbd963e74d | 106 | * 0 on success |
lawliet | 0:8ccbd963e74d | 107 | * -1 on error |
lawliet | 0:8ccbd963e74d | 108 | */ |
lawliet | 0:8ccbd963e74d | 109 | int waitForResp(const char *resp, unsigned int timeout,DataType type); |
lawliet | 0:8ccbd963e74d | 110 | |
lawliet | 0:8ccbd963e74d | 111 | /** send AT command to GPRS module and wait for correct response |
lawliet | 0:8ccbd963e74d | 112 | * @param *cmd AT command which will be send to GPRS module |
lawliet | 0:8ccbd963e74d | 113 | * @param *resp correct response which GPRS module will return |
lawliet | 0:8ccbd963e74d | 114 | * @param *timeout waiting seconds till timeout |
lawliet | 0:8ccbd963e74d | 115 | * @returns |
lawliet | 0:8ccbd963e74d | 116 | * 0 on success |
lawliet | 0:8ccbd963e74d | 117 | * -1 on error |
lawliet | 0:8ccbd963e74d | 118 | */ |
lawliet | 0:8ccbd963e74d | 119 | int sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type); |
lawliet | 0:8ccbd963e74d | 120 | |
lawliet | 0:8ccbd963e74d | 121 | Serial serialModem; |
lawliet | 0:8ccbd963e74d | 122 | Timer timeCnt; |
lawliet | 0:8ccbd963e74d | 123 | |
lawliet | 0:8ccbd963e74d | 124 | private: |
lawliet | 0:8ccbd963e74d | 125 | |
lawliet | 0:8ccbd963e74d | 126 | }; |
lawliet | 0:8ccbd963e74d | 127 | |
lawliet | 0:8ccbd963e74d | 128 | #endif |