自己写底层真的好累啊,而且还写的不好。不过终于是有所收获,完整地把一个GPRS TCP发送数据写好了
分享自己写的代码吧,主要是STM32的串口发送AT指令,然后接收GA6B返回的指令并进行判断,大概就是这样。
使用前确保自己的SIM卡已经注册可以上网。
然后我对串口发送的稍微做了点修改,懒症晚期,不喜欢写注释。(最近在摸索这个模块拉取天气预报,不知道有没有老哥会这个)
主函数代码
[mw_shl_code=c,true]#include "usart.h"
#include "delay.h"
#include "GPIO.h"
#include "string.h"
#include "GA6B.h"
int main(void)
{
char frame[30] = "
$#dbw123#1#1#1#1#1#5
*";
delay_init();
Uart1_init(115200);
Uart2_init(115200); //AT Testing...
for(int e = 0;e<3000;e++)
for(int b = 0;b<500;b++);
while(!AT_Test());
while(!Attach_Network()); //Activing Network...
while(!Active_NetWork());
while(!TCP_Connect());
while(1)
{
PutString(0x02,AT_Table[6]);
delay_ms(100);
if(flag)
{
flag = 0;
if(USART_RX_BUF[0]=='>')
{
memset(USART_RX_BUF,0,199);
PutString(0x03,frame);
USART_SendData(USART2,0x1A);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
delay_ms(1500);
PutString(0x01,"OJBK
");
}
}
delay_ms(1000);
}
}
[/mw_shl_code]
frame 是
$#dbw123#1#1#1#1#1#5
*,是因为我服务器的TCP server是这样写的,第一个$是关键字用来识别有效数据帧,数据和数据库表名用#号隔开,最后利用TCPserver 填入mysql数据库中(比较菜····)。
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",
""
};
static char AT_Table[][20] = {
"AT+GMI=?
",
"ATI
",
"AT
",
"AT+CIPSTATUS
",
"AT+CGATT=1
", //attach network
"AT+CGACT=1
", //active network
"AT+CIPSEND
",
""
};
static char IP[] = "your IP";
static char Port[] = "your PORT";
unsigned char SIM_Status(void);
unsigned char AT_Test(void);
unsigned char Attach_Network(void);
unsigned char Active_NetWork(void);
unsigned char TCP_Connect(void);
unsigned char Command_Check(unsigned char order);
unsigned char SendFrame(char frame[]);
#endif[/mw_shl_code]
GA6B.c
[mw_shl_code=c,true]#include "GA6B.h"
#include "usart.h"
#include "string.h"
#include "delay.h"
unsigned char mflag = 1;
unsigned char SIM_Status(void)
{
if(mflag)
{
mflag = 0;
PutString(0x01,"Please Wait for Initialization!
");
delay_ms(50);
}
if(flag)
{
flag = 0;
if(Command_Check(0))
{
PutString(0x01,"SMS_Ready!
");
delay_ms(50);
return 1;
}
}
return 0;
}
unsigned char Command_Check(unsigned char order)
{
unsigned char i;
for(i = 0;i<strlen(GA6B_Table[order]);i++)
{
if(USART_RX_BUF != GA6B_Table[order])
{
memset(USART_RX_BUF,0,199);
return 0;
}
}
memset(USART_RX_BUF,0,199);
return 1;
}
unsigned char AT_Test(void)
{
PutString(0x02,AT_Table[2]);
delay_ms(200);
if(flag)
{
flag = 0;
if(Command_Check(1))
{
PutString(0x01,"AT Test Passed
");
delay_ms(50);
return 1;
}
}
return 0;
}
unsigned char Attach_Network(void)
{
PutString(0x02,AT_Table[4]);
delay_ms(500); //Delay depends on your sim card and service quility
if(flag)
{
flag = 0;
if(Command_Check(1))
{
PutString(0x01,"Network Attached
");
delay_ms(50);
return 1;
}
}
return 0;
}
unsigned char Active_NetWork(void)
{
PutString(0x02,AT_Table[5]);
delay_ms(500); //Delay depends on your sim card and service quility
if(flag)
{
flag = 0;
if(Command_Check(1))
{
PutString(0x01,"Network Actived
");
delay_ms(50);
return 1;
}
}
return 0;
}
unsigned char TCP_Connect(void)
{
char command[45];
sprintf(command,"AT+CIPSTART="TCP","%s",%s
",IP,Port);
PutString(0x02,command);
delay_ms(1000);
if(flag)
{
flag = 0;
if(Command_Check(2))
{
printf("TCP Connection Established
");
return 1;
}
}
return 0;
}
unsigned char SendFrame(char frame[])
{
PutString(0x02,AT_Table[6]);
delay_ms(100);
if(flag)
{
flag = 0;
if(USART_RX_BUF[0]=='>')
{
memset(USART_RX_BUF,0,100);
PutString(0x02,frame);
delay_ms(10);
USART_SendData(USART2,0x1A);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
delay_ms(100);
if(flag)
{
flag = 0;
if(Command_Check(4))
{
return 1;
}
else
{
return 2;
}
}
}
}
return 0;
}
[/mw_shl_code]
对于串口这块就是用的串口1和串口2 ,串口1连接上位机,串口2连接GAB6
usart.c
[mw_shl_code=c,true]void USART1_IRQHandler(void)
{
unsigned char Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART1);
if(Res!=0x0d&&Res!=0x0a)
{
USART_RX_BUF[USART_RX_STA] = Res;
USART_RX_STA++;
}
else
{
//sprintf(buff,"%s",USART_RX_BUF);
//buff[USART_RX_STA] = ' ';
USART_RX_STA = 0;
flag = 1;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
}
void USART2_IRQHandler( void )
{
unsigned char Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART2);
if(Res!=0x0d&&Res!=0x0a)
{
USART_RX_BUF[USART_RX_STA] = Res;
USART_RX_STA++;
}
else
{
//USART_RX_BUF[USART_RX_STA] = 0x0a;
USART_RX_STA = 0;
flag = 1;
//PutString(0x01,USART_RX_BUF);
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
}
void PutString(u8 mode,char * array)
{
unsigned char i = 0;
if(mode == 0x01)
{
while(*(array+i)!=0x0a&&*(array+i)!=' ')
{
USART_SendData(USART1,*(array+i));
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
i++;
}
}
else if(mode ==0x02)
{
while(*(array+i)!=0x0a)
{
USART_SendData(USART2,*(array+i));
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
i++;
}
}
else
{
while(*(array+i)!='*')
{
USART_SendData(USART2,*(array+i));
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
i++;
}
}
}
[/mw_shl_code]
一周热门 更多>