|
我是菜鸟,代码中的AT命令不需要什么封装就直接放在函数里么,直接就能被识别了?
- #include
- #include
- #include "def.h"
- #include "option.h"
- #include "2410addr.h"
- #include "2410lib.h"
- //#include "2410slib.h"
- //#include "mmu.h"
- #include "gprs.h"
- #define USE_TICK
- //#define USE_TIMERTEST
- struct MenuItem
- {
- char *name;
- void (*func)(void);
- };
- #ifdef USE_TICK
- static volatile unsigned int TickCount = 0;
- static void InitTick(void);
- #ifdef USE_TIMERTEST
- static void TimerTest(void);
- #endif
- #endif
- #ifdef USE_TICK
- #define GETTICKCOUNT() (TickCount)
- #define SLEEP(time) { \
- unsigned int t = GETTICKCOUNT(); \
- while(GETTICKCOUNT()-t < (time)) \
- { \
- IDLEPROCESS(GETDEFAULTCOMMBUF()); \
- } \
- }
- #else
- #define GETTICKCOUNT() 0
- #endif
- /*************************************
- *缓冲区管理
- *************************************/
- #define COMMBUFSIZE 1024
- struct CommBuf
- {
- char buf[COMMBUFSIZE+1];
- unsigned int size;
- char *rptr;
- char *wptr;
- };
- #define COMMBUF_ISEMPTY(obj) ((obj)->size == 0)
- #define COMMBUF_ISFULL(obj) ((obj)->size == COMMBUFSIZE)
- #define COMMBUF_DATASIZE(obj) ((obj)->size)
- #define COMMBUF_EMPTYSIZE(obj) (COMMBUFSIZE - COMMBUF_DATASIZE(obj))
- #define INCRPTR(obj) { \
- (obj)->rptr++; \
- (obj)->size--; \
- if((obj)->rptr >= (obj)->buf + COMMBUFSIZE) \
- (obj)->rptr = (obj)->buf; \
- }
- #define INCWPTR(obj) { \
- (obj)->wptr++; \
- (obj)->size++; \
- if((obj)->wptr >= (obj)->buf + COMMBUFSIZE) \
- (obj)->wptr = (obj)->buf; \
- }
- #define IDLEPROCESS(obj) { \
- while(rUFSTAT1 & 0x0f) \
- { \
- if(COMMBUF_ISFULL(obj)) \
- break; \
- *((obj)->wptr) = RdURXH1(); \
- /*Uart_SendByte(*((obj)->wptr));*/ \
- INCWPTR(obj); \
- } \
- }
- static struct CommBuf RecvBuf;
- #define GETDEFAULTCOMMBUF() (&RecvBuf)
- void InitCommBuf(struct CommBuf *obj)
- {
- if(obj == NULL)
- return;
- memset(obj->buf, 0, sizeof(obj->buf));
- obj->size = 0;
- obj->rptr = obj->wptr = obj->buf;
- }
- void CommBuf_Clean(struct CommBuf *obj)
- {
- if(obj == NULL)
- return;
- obj->size = 0;
- obj->rptr = obj->wptr;
- }
- void CommBuf_Dump(struct CommBuf *obj)
- {
- SLEEP(1000);
- //Uart_Printf("\n----------log----------\n");
- //Uart_Printf(RecvBuf.buf);
- //Uart_Printf("\n----------end----------\n");
- }
- //从缓冲区读一个字符
- char CommBuf_ReadChar(struct CommBuf *obj, int timeout, int *err)
- {
- char c;
- unsigned int t = GETTICKCOUNT();
- while(COMMBUF_ISEMPTY(obj))
- {
- if(GETTICKCOUNT()-t > timeout)
- {
- if(err != NULL)
- *err = -1;
- return '\0';
- }
- IDLEPROCESS(obj);
- }
- c = *obj->rptr;
- INCRPTR(obj);
- if(err != NULL)
- *err = 0;
- return c;
- }
- //等待字符串,成功返回0,否则返回-1。
- int CommBuf_WaitString(struct CommBuf *obj, char *str, int timeout)
- {
- int err;
- char c;
- char *p = str;
- while(1)
- {
- if(*p == '\0')
- return 0;
- c = CommBuf_ReadChar(obj, timeout, &err);
- if(err)
- break;
- if(*p == c)
- p++;
- else
- {
- p = str;
- if(*p == c)
- p++;
- }
- }
- return -1;
- }
- //读缓冲区
- int ReadCommBuf(struct CommBuf *obj, void *pBuf, size_t sizebuf, unsigned int timeout)
- {
- int i;
- char *buf = pBuf;
- unsigned int t;
- if(obj == NULL)
- return -2;
- t = GETTICKCOUNT();
- while(COMMBUF_ISEMPTY(obj))
- {
- if(GETTICKCOUNT()-t > timeout)
- return -1;
- IDLEPROCESS(obj);
- }
- for(i=0; i
- {
- while(COMMBUF_ISEMPTY(obj))
- {
- if(GETTICKCOUNT()-t > timeout/*100*/)
- return i;
- IDLEPROCESS(obj);
- }
- *buf = *obj->rptr;
- INCRPTR(obj);
- IDLEPROCESS(obj);
- }
- return i;
- }
- #if 0
- int WriteCommBuf(struct CommBuf *obj, void *pBuf, size_t size)
- {
- int i;
- char *buf = pBuf;
- if(obj == NULL)
- return 0;
- if(size > COMMBUF_EMPTYSIZE(obj))
- {
- return 0;
- }
- for(i=0; i < size; i++)
- {
- *(obj->wptr) = *buf;
- INCWPTR(obj);
- }
- return i;
- }
- #endif
- /********************************************/
- //GPRS 初始化和通信处理
- static void InitUart1(int baud)
- {
- int i;
- rGPHCON = 0x2afaaa;
- rGPHUP = 0x7ff;
- rULCON1 = 0x03;
- rUFCON1 = 0x07;//(3<<6) | (3<<4) | (1<<2) | (1<<1) | (1<<0);//0x07;
- rUCON1 = 0x005;//0x245;
- rUBRDIV1=((int)(PCLK/(16*baud))-1);
- for(i=0;i<5000;i++);
- rUMCON1 = 0x10;
- }
- //GPRS 初始化
- static void InitGPRS(void)
- {
- InitUart1(9600);//初使化串口1
- #ifdef USE_TICK
- InitTick();
- #ifdef USE_TIMERTEST
- TimerTest();
- #endif//USE_TIMERTEST
- #endif//USE_TICK
- }
- void GPRS_SendChar(char c)
- {
- while(rUFSTAT1 & 0x200)
- {
- IDLEPROCESS(GETDEFAULTCOMMBUF());
- }
- WrUTXH1(c);
- }
- void GPRS_SendString(char *str)
- {
- for(; *str; str++)
- {
- GPRS_SendChar(*str);
- }
- }
- //用“AT”命令测试GPRS模块,成功返回1,否则返回0
- int GPRS_TestGPRS(void)
- {
- char log[32];
- int len;
- InitCommBuf(GETDEFAULTCOMMBUF());
- GPRS_SendString("at\r");
- if((len=ReadCommBuf(GETDEFAULTCOMMBUF(), log, sizeof(log)-1, 1000)) > 0)
- {
- log[len] = '\0';
- if(strstr(log, "OK"))
- return 1;
- }
- return 0;
- }
- /***********************************************/
- //短信功能
- //发送短消息,成功返回0,否则返回 -1:GPRS模块没有应答、-2:操作被拒绝、-3:发送失败
- int GPRS_SendTextSMS(char *phone, char *text)
- {
- char log[64];
- int len;
- InitCommBuf(GETDEFAULTCOMMBUF());
- //初使化CommBuf
- GPRS_SendString("at+csca="+8613800571500"\r"); //by cx 设置短消息中心
- SLEEP(1000);
- // 延时
- GPRS_SendString("at+cmgf=1\r"); //by cx 设置短消息格式
- SLEEP(1000);
- CommBuf_Clean(GETDEFAULTCOMMBUF());
- //清空CommBuf
- GPRS_SendString("at+cmgs="");
- GPRS_SendString(phone);
- GPRS_SendString(""\r");
- len = ReadCommBuf(GETDEFAULTCOMMBUF(), log, sizeof(log)-1, 1000);
- if(len > 0)
- {
- log[len] = '\0';
- Uart_Printf(log);
- if(strstr(log, "> ") && !strstr(log, "ERROR"))
- {
- GPRS_SendString(text);
- GPRS_SendString("\x1a\r");
- if(!CommBuf_WaitString(GETDEFAULTCOMMBUF(), "OK", 10000))
- return 0;//发送成功
- else
- return -3;//发送失败
- }
- return -2;//操作被拒绝
- }
- return -1;//GPRS模块没有应答
- }
- //发送短信UI
- void GPRS_SendSmsUI(void)
- {
- char tel[32];
- char smstext[512];
- Uart_Printf("\n\n[发送短信息]");
- Uart_Printf("\n电话号码:");
- Uart_GetString(tel);
- //从串口读出电话号码
- Uart_Printf("\n短信内容:\n");
- Uart_GetString(smstext);
- // 从串口读出短信息
- Uart_Printf("\n正在发送...");
- switch(GPRS_SendTextSMS(tel, smstext))
- {
- case -1:
- Uart_Printf("\n***GPRS模块没有应答");
- break;
- case -2:
- Uart_Printf("\n***操作被拒绝");
- break;
- case -3:
- Uart_Printf("\n***发送失败");
- break;
- case 0:
- Uart_Printf("\n发送成功");
- break;
- }
- CommBuf_Dump(GETDEFAULTCOMMBUF());
- }
- /***********************************/
- //电话功能
- //设置来电显示
- int GPRS_InitCall(void)
- {
- char log[32];
- int len;
- InitCommBuf(GETDEFAULTCOMMBUF());
- GPRS_SendString("AT+CR=1\r");
- len = ReadCommBuf(GETDEFAULTCOMMBUF(), log, sizeof(log)-1, 1000);
- if(len > 0)
- {
- log[len] = '\0';
- if(strstr(log, "OK"))
- {
- CommBuf_Clean(GETDEFAULTCOMMBUF());
- GPRS_SendString("AT+CLIP=1\r");
- len = ReadCommBuf(GETDEFAULTCOMMBUF(), log, sizeof(log)-1, 1000);
- if(len > 0)
- {
- log[len] = '\0';
- if(strstr(log, "OK"))
- return 0;//成功
- }
- }
- }
- return -1;//失败
- }
- //电话呼叫
- int GPRS_Call(char *phone, volatile int *stop)
- {
- char log[128];
- unsigned int t;
- int len, ret;
- InitCommBuf(GETDEFAULTCOMMBUF());
- GPRS_SendString("atd");
- GPRS_SendString(phone);
- GPRS_SendString(";\r");
- Uart_Printf("\n正在呼叫...");
- SLEEP(1000);
- CommBuf_Clean(GETDEFAULTCOMMBUF());
- t = GETTICKCOUNT();
- len = 0;
- do
- {
- ret = ReadCommBuf(GETDEFAULTCOMMBUF(), &log[len], sizeof(log)-1-len, 1000);
- if(ret > 0)
- {
- len += ret;
- log[len] = '\0';
- //Uart_Printf(log);
- if(strstr(log, "OK"))
- {
- Uart_Printf("\n已经接通。");
- SLEEP(5000);
- }
- else if(strstr(log, "BUSY"))
- {
- Uart_Printf("\nBUSY");
- }
- else if(strstr(log, "NO ANSWER"))
- {
- Uart_Printf("\nNO ANSWER");
- }
- else if(strstr(log, "NO CARRIER"))
- {
- Uart_Printf("\nNO CARRIER");
- }
- else
- {
- continue;
- }
- ret = 0;
- break;
- }
- ret = -1;
- }while(GETTICKCOUNT()-t < 120000);
- GPRS_SendString("ath\r");
- if(ret == -1)
- Uart_Printf("\n无应答。");
- SLEEP(1000);
- return ret;
- }
- void GPRS_CallUI(void)
- {
- char phone[32];
- Uart_Printf("\n[电话呼叫]\n电话号码:");
- Uart_GetString(phone);
- GPRS_Call(phone, NULL);
- CommBuf_Dump(GETDEFAULTCOMMBUF());
- }
- /***********************************/
- //AT命令行接口
- static void GPRS_CommandLine(void)
- {
- char key=0;
- Uart_Printf("\n[GPRS AT命令]\n");
- while(1)
- {
- if(rUFSTAT0 & 0x0f)
- {
- key = RdURXH0();//串口0读出数据
- if(key == 0x1b)
- break;
- while(rUFSTAT1 & 0x200);
- WrUTXH1(key);//串口1写数据
- }
- if(rUFSTAT1 & 0x0f)
- {
- while(rUFSTAT0 & 0x200);
- //WrUTXH0(RdURXH1());
- key = RdURXH1();
- if(rUERSTAT1 & 0x0f);
- //Uart_Printf("");
- WrUTXH0(key);
- }
- }
- }
- /***********************************/
- //菜单处理
- const struct MenuItem menu[] = {
- {"AT命令行", GPRS_CommandLine},
- {"发送短信", GPRS_SendSmsUI},
- {"电话呼叫", GPRS_CallUI}
- };//数组变量
- #define MENUITEMCOUNT (sizeof(menu)/sizeof(menu[0]))
- static int ShowMenu(void)
- {
- int i;
- Uart_Printf("\n\n\n--------= GPRS MENU =--------\n");
- for(i=0; i
- {
- Uart_Printf(" [%d] %s\n", i, menu[i].name);
- }
- Uart_Printf("选择:");
- do//输入所选的菜单
- {
- i = Uart_Getch() - '0';//字符变成数字
- }while(i<0 || i>=MENUITEMCOUNT);//对输入的数字是否有效
- Uart_Printf("%d\n", i);
- return i;
- }
- void GPRS_Test(void)
- {
- int i;
- InitGPRS();//初使化GPRS
- InitCommBuf(GETDEFAULTCOMMBUF());
- if(!GPRS_TestGPRS())
- {
- Uart_Printf("\n没有检测到GPRS模块。");
- return;
- }
- while(1)
- {
- i = ShowMenu();//打印菜单
- if(menu[i].func)
- menu[i].func();
- }
- }
- /*****************************************/
- #ifdef USE_TICK
- #ifdef USE_TIMERTEST
- static void TimerTest(void)
- {
- unsigned int t;
- char h,m,s;
- Uart_Printf("\n[Timer Test]\n");
- Uart_Printf("\nHour:");
- h = Uart_GetIntNum();
- Uart_Printf("\nMin:");
- m = Uart_GetIntNum();
- Uart_Printf("\nSec:");
- s = Uart_GetIntNum();
- Uart_Printf("\n\n%02d:%02d:%02d", h, m, s);
- t = GETTICKCOUNT();
- while(1)
- {
- if(GETTICKCOUNT() - t > 1000)
- {
- t = GETTICKCOUNT();
- if(s < 59)
- s++;
- else if(m < 59)
- {
- s = 0;
- m++;
- }
- else if(h < 23)
- {
- s = m = 0;
- h++;
- }
- else
- {
- s = m = h = 0;
- }
- Uart_Printf("\b\b\b\b\b\b\b\b%02d:%02d:%02d", h, m, s);
- }
- }
- }
- #endif //USE_TIMERTEST
- static void __irq TickCountInt(void)
- {
- rSRCPND = BIT_TIMER4;
- rINTPND = BIT_TIMER4;
- if(rINTPND);
- TickCount++;
- }
- static void InitTick(void)
- {
- pISR_TIMER4 = (unsigned)TickCountInt;
- rTCFG0 = 0x00fc00; //Dead zone=0,Prescaler1=15(0x0f),Prescaler0=15(0x0f)
- rTCFG1 = 0x000000; //All interrupt,Mux4=1/2,Mux3=1/4,Mux2=1/8,Mux1=1/16,Mux0=1/16
- rTCNTB4 = 0x64;
- rTCON = 0x600000; //Auto reload, Inverter off, Manual update, Dead zone disable, Stop
- rTCON = 0x500000; //Auto reload(T0=One-shot),Inverter off,No operation,Dead zone disable,Start
- //
- rINTMSK &= ~BIT_TIMER4;
- }
- #endif //USE_TICK
复制代码
|
|