前一阵在做项目的时候 用到了GPRS模块 想做一个可以实时向服务器发送采集数据的板子。
在某宝搞了一块便宜的GA6B试了试,也不知道为啥用它给的那个驱动我这个单片机分分钟跑飞。
头大是在没办法所以自己写了一个驱动,看着效果还行。不过我只根据他给的文档做了非常基础的功能,不过实际上大概都是一样的,比较简单的AT指令集,对串口这块要求比较多罢了。
这边主要写了的功能就是
{GA6B初始化、回响禁止、回调字符检查、AT测试、网络附着、网络激活、TCP连接、发送数据帧、获取基站位置、发送短信}
和ESP8266不一样的是,他的AT指令的结束符是
而不是
这个在有时候会导致模块无响应(主要是在TCP通讯 发送HTTP请求);
下来发一下代码吧
GA6B.h[mw_shl_code=c,true]#ifndef __GA6B_H
#define __GA6B_H
static char GA6B_Table[][20] = {
"SMS Ready",
"OK",
"ALREAY CONNECT",
">",
"SEND OK",
"STATUS: CLOSED",
"CONNECT OK",
"STATUS: IP INITIAL",
"CED:",
"Call Ready"
};
static char AT_Table[][30] = {
"AT+GMI=?
",
"ATI
",
"AT
",
"AT+CIPSTATUS
",
"AT+CGATT=1
", //attach network
"AT+CGACT=1
", //active network
"AT+CIPSEND
",
"AT^CCED=0,1
",
"AT+CREADY
",
"AT+CMGF=1
", //set message-text mode
"AT+CSCS="GSM"
", //set message character
};
extern char textNumber[30];
static char IP[] = "remote IP";
static char Port[] = "remote Port";
static char IP_L[] = "139.129.20.175"; //For location
static char Port_L[] = "81"; //For location
unsigned char GA6B_Init(void);
unsigned char Uable_Echo(void);
unsigned char Ckeck_CallBack(char * KeyValue);
unsigned char AT_Test(void);
unsigned char Attach_Network(void);
unsigned char Active_NetWork(void);
unsigned char TCP_Connect(char * IP,char * Port);
unsigned char SendFrame(char * frame);
unsigned char GetLocation(void);
unsigned char SendMessage(char * context);
#endif
[/mw_shl_code]
GA6B.c
[mw_shl_code=c,true]#include "GA6B.h"
#include "usart.h"
#include "string.h"
#include "delay.h"
char textNumber[30] = "AT+CMGS="your phone number"
";
unsigned char GA6B_Init(void)
{
delay_ms(200);
if(findString(USART_RX_BUF,GA6B_Table[0]))
{
ClearRevBuff;
while(!AT_Test());
while(!Uable_Echo());
while(!Attach_Network());
while(!Active_NetWork());
while(!TCP_Connect(IP,Port));
delay_ms(1000);
return 1;
}
return 0;
}
unsigned char Uable_Echo(void)
{
PutString(0x02,"ATE0
");
delay_ms(500);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,"OK"))
{
PutString(0x01,"Command Echo Forbade!
");
ClearRevBuff;
return 1;
}
}
return 0;
}
unsigned char AT_Test(void)
{
PutString(0x02,AT_Table[2]);
delay_ms(500);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,"OK"))
{
PutString(0x01,"AT_Test_Passed!
");
ClearRevBuff;
return 1;
}
}
return 0;
}
unsigned char Attach_Network(void)
{
PutString(0x02,AT_Table[4]);
delay_ms(1000);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,"OK"))
{
PutString(0x01,"Network_Attached!
");
ClearRevBuff;
return 1;
}
}
return 0;
}
unsigned char Active_NetWork(void)
{
PutString(0x02,AT_Table[5]);
delay_ms(500);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,"OK"))
{
PutString(0x01,"Network_Actived!
");
ClearRevBuff;
return 1;
}
}
return 0;
}
unsigned char TCP_Connect(char * IP,char * Port)
{
ClearRevBuff;
memset(buff,0,99);
sprintf(buff,"AT+CIPSTART="TCP","%s",%s
",IP,Port);
PutString(0x02,buff);
delay_ms(1000);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,GA6B_Table[2])||findString(USART_RX_BUF,GA6B_Table[6]))
{
PutString(0x01,"TCP_Connection_Established!
");
ClearRevBuff;
return 1;
}
else
{
ClearRevBuff;
}
}
return 0;
}
unsigned char SendFrame(char * frame)
{
PutString(0x02,AT_Table[3]);
delay_ms(100);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,GA6B_Table[5]))
{
ClearRevBuff;
while(!TCP_Connect(IP,Port));
}
if(findString(USART_RX_BUF,GA6B_Table[7]))
{
while(!AT_Test());
while(!Uable_Echo());
while(!Attach_Network());
while(!Active_NetWork());
while(!TCP_Connect(IP,Port));
}
}
PutString(0x02,AT_Table[6]);
delay_ms(300);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,GA6B_Table[3]))
{
PutString(0x01,frame);
ClearRevBuff;
PutString(0x03,frame);
delay_ms(1500);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,GA6B_Table[1]))
{
ClearRevBuff;
return 1;
}
else
{
return 0;
}
}
}
}
return 0;
}
unsigned char GetLocation(void)
{
PutString(0x02,AT_Table[7]);
if(flag)
{
flag = 0;
PutString(0x01,USART_RX_BUF);
if(CutString(USART_RX_BUF,GA6B_Table[8],','))
{
ClearRevBuff;
while(!TCP_Connect(IP_L,Port_L));
memset(buff,0,99);
sprintf(buff,"GET
http://api.cellocation.com:81/ce ... i=%d&output=xml*",Location_Frame.mcc,Location_Frame.mnc,Location_Frame.lac,Shex2int(Location_Frame.ci));
PutString(0x01,buff);
SendFrame(buff);
return 1;
}
}
return 0;
}
unsigned char SendMessage(char * context)
{
PutString(0x02,AT_Table[9]);
delay_ms(300);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,GA6B_Table[1]))
{
ClearRevBuff;
PutString(0x02,AT_Table[10]);
delay_ms(300);
if(flag)
{
flag = 0;
if(findString(USART_RX_BUF,GA6B_Table[1]))
{
ClearRevBuff;
PutString(0x02,textNumber);
delay_ms(300);
if(findString(USART_RX_BUF,GA6B_Table[3]))
{
ClearRevBuff;
PutString(0x03,context);
delay_ms(1500);
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
}
else
{
return 0;
}
}
return 0;
}
[/mw_shl_code]
字符串相关函数
[mw_shl_code=c,true]int findString(char * input,const char * strander)
{
for(int i = 0;i<strlen(input);i++)
{
if(*(input+i)==*(strander))
{
for(int j=0;j<strlen(strander);j++)
{
if(*(input+i+j)!=*(strander+j))
{
return 0;
}
}
return 1;
}
}
return 0;
}
int CutString(char * input,const char * strander,unsigned char symbol)//input 被查找数组 strander 需要找到的关键字符symbol分隔符
{
int i = 0,j = 0,count = 0,frame_section = 1;
for(i = 0;i<strlen(input);i++)
{
if(*(input+i)==*(strander))
{
for(j = 0;j<strlen(strander);j++)
{
if(*(input+i+j)!=*(strander+j))
{
return 0;
}
}
break;//return i+j;
}
}
for(i = i+j;i<strlen(input);i++)
{
if(*(input+i)==symbol)
{
count = 0;
frame_section++;
}
else
{
switch (frame_section)
{
case 1:
{
Location_Frame.mcc[count++] = *(input+i);
}break;
case 2:
{
Location_Frame.mnc[count++] = *(input+i);
}break;
case 3:
{
Location_Frame.lac[count++] = *(input+i);
}break;
case 4:
{
Location_Frame.ci[count++] = *(input+i);
}break;
}
}
if(frame_section==5)return 1;
}
return -1;
}
int Shex2int(char hex[])
{
int temp = 0,weight = 0;
for(int i = 0;i<strlen(hex);i++)
{
if(hex
>=97&&hex<=102)
{
weight = hex - 87;
}
else if(hex>=48&&hex<=57)
{
weight = hex - 48;
}
else
{
return 0;
}
switch(i)
{
case 0:
{
temp += (weight*16*16*16);
}break;
case 1:
{
temp += (weight*16*16);
}break;
case 2:
{
temp += (weight*16);
}break;
case 3:
{
temp += weight;
}break;
}
}
return temp;
}
unsigned char Check_Lframe(void)
{
unsigned int i = 0;
unsigned char count = 1,temp = 1;
if(flag)
{
flag = 0;
for(;i<strlen(USART_RX_BUF);i++)
{
if(USART_RX_BUF==0x3E)
{
Location_Utf8.prinvince[0] = USART_RX_BUF[i+1];
Location_Utf8.prinvince[1] = USART_RX_BUF[i+2];
Location_Utf8.prinvince[2] = USART_RX_BUF[i+3];
Location_Utf8.city[0] = USART_RX_BUF[i+4];
Location_Utf8.city[1] = USART_RX_BUF[i+5];
Location_Utf8.city[2] = USART_RX_BUF[i+6];
Location_Utf8.countyr[0] = USART_RX_BUF[i+7];
Location_Utf8.countyr[1] = USART_RX_BUF[i+8];
Location_Utf8.countyr[2] = USART_RX_BUF[i+9];
break;
}
}
count = 0;
temp = 0;
for(i=0;i<9;i++)
{
switch(count)
{
case 0:
{
if(Location_Utf8.prinvince[temp]!=target[count][temp])return 0;
if(i == 2)count = 1;
}break;
case 1:
{
if(Location_Utf8.city[temp]!=target[count][temp])return 0;
if(i == 5)count = 2;
}break;
case 2:
{
if(Location_Utf8.countyr[temp]!=target[count][temp])return 0;
}break;
if(temp<2)temp++;
else temp = 0;
}
}
}
return 1;
}
unsigned char GetCommand(void)
{
if(flag_1)
{
flag_1 = 0;
if(findString(USART1_Usr_Mesg,"1"))
{
return 1;
}
}
return 0;
}[/mw_shl_code]
需要注意的时,一般我们通过TCP(HTTP)向服务器发送请求时如果返回中文的话,会有相对应的转码问题,GA6B似乎是没有ANSI这个转码的
再就是STM32F103C8T6的SRAM实在太小,在这上面估计是不能再完成相(UTF8->ANSI)的转码工作。
一周热门 更多>