2021-09-11 15:53:43 索煒達電子 662
項目編號:E1063
文件大?。?.6M
源碼說明:帶中文注釋
開發(fā)環(huán)境:C編譯器
簡要概述:
自己寫底層真的好累啊,而且還寫的不好。不過終于是有所收獲,完整地把一個GPRS TCP發(fā)送數據寫好了
分享自己寫的代碼吧,主要是STM32的串口發(fā)送AT指令,然后接收GA6B返回的指令并進行判斷,大概就是這樣。
使用前確保自己的SIM卡已經注冊可以上網。
然后我對串口發(fā)送的稍微做了點修改,懶癥晚期,不喜歡寫注釋。(最近在摸索這個模塊拉取天氣預報,不知道有沒有老哥會這個)
主函數代碼
[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] = "\n$#dbw123#1#1#1#1#1#5\n*";
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\r\n");
}
}
delay_ms(1000);
}
}
[/mw_shl_code]
frame 是\n$#dbw123#1#1#1#1#1#5\n*,是因為我服務器的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=?\r\n",
"ATI\r\n",
"AT\r\n",
"AT+CIPSTATUS\r\n",
"AT+CGATT=1\r\n", //attach network
"AT+CGACT=1\r\n", //active network
"AT+CIPSEND\r\n",
""
};
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!\r\n");
delay_ms(50);
}
if(flag)
{
flag = 0;
if(Command_Check(0))
{
PutString(0x01,"SMS_Ready!\r\n");
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\r\n");
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\r\n");
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\r\n");
delay_ms(50);
return 1;
}
}
return 0;
}
unsigned char TCP_Connect(void)
{
char command[45];
sprintf(command,"AT+CIPSTART=\"TCP\",\"%s\",%s\r\n",IP,Port);
PutString(0x02,command);
delay_ms(1000);
if(flag)
{
flag = 0;
if(Command_Check(2))
{
printf("TCP Connection Established\r\n");
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] = '\0';
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)!='\0')
{
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++;
}
}
}