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
Diff: GPRS/modem/modem.h
- Revision:
- 13:379ce1d51b88
- Parent:
- 12:47488369a980
--- a/GPRS/modem/modem.h Tue Mar 10 03:11:38 2015 +0000 +++ b/GPRS/modem/modem.h Fri Apr 03 15:44:04 2015 +0800 @@ -23,121 +23,50 @@ #ifndef __MODEM_H__ #define __MODEM_H__ -#if defined(LPC11UXX) || defined (LPC176X) -#include "BufferedSerial.h" +#if defined(TARGET_LPC11UXX) || defined (TARGET_LPC176X) +#include "BufferedSerial.h" +#define INTERFACE_BASE BufferedSerial #else -#include "mbed.h" +#include "mbed.h" +#define INTERFACE_BASE Serial #endif -#define DEFAULT_TIMEOUT 5 +#define DEFAULT_TIMEOUT_MS 30000 -enum DataType { - CMD = 0, - DATA = 1, -}; /** Modem class. * Used for Modem communication. attention that Modem module communicate with MCU in serial protocol */ -class Modem +class Modem : public INTERFACE_BASE { public: /** Create Modem Instance * @param tx uart transmit pin to communicate with Modem * @param rx uart receive pin to communicate with Modem - * @param baudRate baud rate of uart communication */ - Modem(PinName tx, PinName rx, int baudRate) : serialModem(tx, rx) { - serialModem.baud(baudRate); + Modem(PinName tx, PinName rx) : INTERFACE_BASE(tx, rx) { + baud(115200); }; - -#if defined(LPC11UXX) || defined (LPC176X) - BufferedSerial serialModem; -#else - Serial serialModem; -#endif - + protected: - /** Power on Modem - */ - void preInit(void); - - /** check serialModem is readable or not - * @returns - * true on readable - * false on not readable - */ - bool readable(); - - /** read one byte from serialModem - * @returns - * one byte read from serialModem - */ - char readByte(void); - - /** read from Modem module and save to buffer array - * @param buffer buffer array to save what read from Modem module - * @param count the maximal bytes number read from Modem module - * @param timeOut time to wait for reading from Modem module - * @returns - * 0 on success - * -1 on error - */ - int readBuffer(char* buffer,int count, unsigned int timeOut); - - - /** clean Buffer - * @param buffer buffer to clean - * @param count number of bytes to clean - */ - void cleanBuffer(char* buffer, int count); - - /** send AT command to Modem module - * @param cmd command array which will be send to GPRS module - */ - void sendCmd(const char* cmd); - - /** send data to Modem module - * @param data array which will be send to GPRS module - * @param len data length - */ - void sendData(const char* data, int len); - - /**send "AT" to Modem module - */ - void sendATTest(void); - - /** compare the response from GPRS module with a string - * @param resp buffer to be compared - * @param len length that will be compared - * @param timeout waiting seconds till timeout - */ - bool respCmp(const char *resp, unsigned int len, unsigned int timeout); - - /** check Modem module response before time out - * @param *resp correct response which Modem module will return - * @param *timeout waiting seconds till timeout - * @returns - * 0 on success - * -1 on error - */ - int waitForResp(const char *resp, unsigned int timeout,DataType type); - - /** send AT command to GPRS module and wait for correct response - * @param *cmd AT command which will be send to GPRS module - * @param *resp correct response which GPRS module will return - * @param *timeout waiting seconds till timeout - * @returns - * 0 on success - * -1 on error - */ - int sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type); - - Timer timeCnt; - -private: - + /** Read a line + * @param buf the buffer to store a line + * @param len size of the buffer + * @param timeout wait time (ms) + * @return -1 if timeout, length of the line if otherwise + */ + int readline(char *buf, int len, uint32_t timeout = DEFAULT_TIMEOUT_MS); + + int command(const char* format, ...); + + int match(const char *resp, uint32_t timeout = DEFAULT_TIMEOUT_MS); + + void flush(); + uint8_t read(); + +private: + Timer timer; }; #endif