一.准备工作
首先,这是我需要做的一个项目,但是我没有接触过4G模块,在网上查找资料很多都不全,因此踩了很多坑,包括4G模块怎样连上服务器,怎么与单片机通信等等。
首先是准备工作,需要下载3个软件,向日葵、网络调试助手以及SSCOM串口通讯助手,其他的一类软件也行,但我没试过。其次最好有两个USB转TTL的模块,这个可以用来看你的代码运行是不是正常的,最后,你要有一份你使用的4G模块文档,因为每个模块定义的连接方式不一样,比如A7680C的就与EC200的连接服务器方式就不一样。
二.服务器的搭建
准备工作做好以后就可以搭建你的服务器了,这需要使用向日葵软件进行内网穿刺,很多小白包括我刚开始的时候多不知道,以为直接使用网络调试助手打开一个服务器就行,其实这是不行的,向日葵的搭建有很多详细的教程,可以去搜看看。
首先win+r打开命令版。输入cmd进入终端。在输入ipcongfig就可以查看你电脑的IP地址,这里也要注意别看错了地址,如我链接的是以太网,就看以太网的,像我的就是222.196.33.227.
复制自己的IP地址就进入向日葵软件进行内网穿透,
就像这样就行,这个端口号是你用网络调试助手设置的
就像我这样,设置好之后就打开服务器,这是进入花生壳看看是否连接成功,成功就像我这样
这样,你的服务器就搭建好了,如果你想连接你的服务器,你不能直接连接你本机的IP地址,需要来连接花生壳提供的域名或者映射的IP地址,向我需要连接就需要指向115.236.153.170,端口号就是53289。你可以先打开串口调试助手SSCOM进行调试,看能不能连接到服务器。
三.测试4G模块
服务器搭建好之后你就需要测试一下你的4G模块,以免所有工作都做好了,连接不到服务器,到处查找原因,却找不到,使用串口通讯助手
1、AT+CPIN? ==>查询SIM卡是否正常,返回ready则表示SIM卡正常
(1)成功 :+CPIN: READY OK
(2)失败:+CME ERROR: 10
2、AT+CSQ ==>查询模组的信号强度,第一个值为0-31则正常,99为不正常
返回:+CSQ: 29,99 OK
3、AT+CREG? ==>查询模组是否注册上GSM网络,+CREG:0,1 表示已注册上本地网,+CREG:0,5表示注册上漫游网。
返回:+CREG: 0,1 OK
4、AT+NETOPEN ==>启动TCP服务
(1) 成功返回OK
(2) 失败返回ERROR
5、AT+CIPOPEN=<link_num>,”TCP”,, ==>连接TCP服务器 需要借助花生壳软件穿透内网
例如:AT+CIPOPEN=1,"TCP","115.236.153.170",53289
(1) 成功返回OK且(+CIPOPEN: 1,0)第二个参数为0
(2) 失败返回ERROR
6、AT+CIPSEND=<link_num>, ==>进入通道发送界面<link_num>=>通道数据长度
例如:AT+CIPSEND=1,9 前面的1为连接服务器选择的通道,没发送一次数据都需要重发命令
成功返回 >
如果切换功能模式,需重新执行4.5.6步才能发送数据。这就是A7680C连接上位机的步骤。这里一定要去看你模块对应的AT 指令,像A7680C连接服务器就必须输入AT+NETOPEN这一命令打开网络,像EC200就不用。4G模块测试好之后就可以写你的代码了。
四.代码编写
这个我也是借鉴了别人的代码,首先,我们需要定义两个串口,这两个串口用来对你的单片机进行检测,看你所写的的代码是否正确,这就需要USB转TTL模块,当然两个最好,这样可以对你所写的代码先跑一边,检测出其中的问题,以免有问题不知道在哪里。同时如果你使用两个模块检测出你的代码没问题,却连接4G模块连接不上,那你就要检测一下你的返回值是否正确,这些问题都排除之后还是不行,那就换个型号的板子,我开头使用c8t8的就不行,我检查了好久,就是不知到问题在哪里,最后我换了vet6的板子就行。首先是串口程序
4.1串口程序
#include "sys.h"
#include "usart.h"
#include "stdio.h"
#include "string.h"
#include "stm32f10x_tim.h"
//
//¼ÓÈëÒÔÏ´úÂë,Ö§³Öprintfº¯Êý,¶ø²»ÐèҪѡÔñuse MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý
struct __FILE
{
int handle;
};
FILE __stdout;
//¶¨Òå_sys_exit()ÒÔ±ÜÃâʹÓðëÖ÷»úģʽ
_sys_exit(int x)
{
x = x;
}
//Öض¨Òåfputcº¯Êý
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//Ñ»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
USART1->DR = (u8) ch;
return ch;
}
#endif
UART_BUF buf_uart1; //Á¬½ÓÉÏλ»ú
UART_BUF buf_uart2; //A7680C
UART_BUF buf_uart3; //TTL
void UART1_send_byte(char data)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, data);
}
void UART2_send_byte(char data)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, data);
}
void UART3_send_byte(char data)
{
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, data);
}
//´®¿Ú1ÖжϷþÎñ³ÌÐò
//×¢Òâ,¶ÁÈ¡USARTx->SRÄܱÜÃâĪÃûÆäÃîµÄ´íÎó
//½ÓÊÕ״̬
//bit15£¬ ½ÓÊÕÍê³É±êÖ¾
//bit14£¬ ½ÓÊÕµ½0x0d
//bit13~0£¬ ½ÓÊÕµ½µÄÓÐЧ×Ö½ÚÊýÄ¿
//³õʼ»¯IO ´®¿Ú1
//bound:²¨ÌØÂÊ
void uart_init(u32 bound){
//GPIO¶Ë¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ
USART_DeInit(USART1); //¸´Î»´®¿Ú1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA10
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
//USART ³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate = bound;//Ò»°ãÉèÖÃΪ9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæżУÑéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½
USART_Init(USART1, &USART_InitStructure); //³õʼ»¯´®¿Ú
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//¿ªÆô¿ÕÏÐÖжÏ
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆô½ÓÊÕÖжÏ
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú
}
//³õʼ»¯IO ´®2
//bound:²¨ÌØÂÊ
void uart2_init(u32 bound)
{
//GPIO¶Ë¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜ£¬GPIOAʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//USART2
USART_DeInit(USART2); //¸´Î»´®¿Ú2
//USART2_TX PA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA2
//USART2_RX PA.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA3
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//ÇÀÕ¼ÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
//USART ³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate = bound;//115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæżУÑéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½
USART_Init(USART2, &USART_InitStructure); //³õʼ»¯´®¿Ú
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿ªÆôÖжÏ
USART_Cmd(USART2, ENABLE); //ʹÄÜ´®¿Ú
}
//³õʼ»¯IO ´®3
//bound:²¨ÌØÂÊ
void uart3_init(u32 bound)
{
//GPIO¶Ë¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ʹÄÜ£¬GPIOAʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//USART3
USART_DeInit(USART3); //¸´Î»´®¿Ú3
//USART3_TX PB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOB, &GPIO_InitStructure); //³õʼ»¯PA2
//USART3_RX PB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOB, &GPIO_InitStructure); //³õʼ»¯PB11
//Usart3 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//ÇÀÕ¼ÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
//USART ³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate = bound;//115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæżУÑéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½
USART_Init(USART3, &USART_InitStructure); //³õʼ»¯´®¿Ú
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿ªÆôÖжÏ
USART_Cmd(USART3, ENABLE); //ʹÄÜ´®¿Ú
}
void Uart1_SendStr(char*SendBuf)//´®¿Ú1´òÓ¡Êý¾Ý
{
while(*SendBuf)
{
while((USART1->SR&0X40)==0);//µÈ´ý·¢ËÍÍê³É
USART1->DR = (u8) *SendBuf;
SendBuf++;
}
}
void Uart2_SendStr(char*SendBuf)//´®¿Ú1´òÓ¡Êý¾Ý
{
while(*SendBuf)
{
while((USART2->SR&0X40)==0);//µÈ´ý·¢ËÍÍê³É
USART2->DR = (u8) *SendBuf;
SendBuf++;
}
IWDG_Feed();//ι¹·
}
void Uart3_SendStr(char*SendBuf)//´®¿Ú3´òÓ¡Êý¾Ý
{
while(*SendBuf)
{
while((USART3->SR&0X40)==0);//µÈ´ý·¢ËÍÍê³É
USART3->DR = (u8) *SendBuf;
SendBuf++;
}
}
void USART1_IRQHandler(void) //´®¿Ú1ÖжϷþÎñ³ÌÐò
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½ÓÊÕÖжϣ¬¿ÉÒÔÀ©Õ¹À´¿ØÖÆ
{
buf_uart1.buf[buf_uart1.index++] =USART_ReceiveData(USART1);//½ÓÊÕÄ£¿éµÄÊý¾Ý
}
}
void ec200x_receive_process_event(unsigned char ch ) //´®¿Ú2¸ø4gÓÃ
{
if(buf_uart2.index >= BUFLEN)
{
buf_uart2.index = 0 ;
}
else
{
buf_uart2.buf[buf_uart2.index++] = ch;
}
}
void USART2_IRQHandler(void) //´®¿Ú2½ÓÊÕº¯Êý
{
if(USART_GetITStatus(USART2, USART_IT_RXNE)==SET)
{
ec200x_receive_process_event(USART_ReceiveData(USART2));
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET)
{
ec200x_receive_process_event(USART_ReceiveData(USART2));
USART_ClearFlag(USART2,USART_FLAG_ORE);
}
}
void USART3_IRQHandler(void) //´®¿Ú3ÖжϷþÎñ³ÌÐò
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //½ÓÊÕÄ£¿é·µ»ØµÄÊý¾Ý
{
buf_uart3.buf[buf_uart3.index++]=USART_ReceiveData(USART3); //½ÓÊÕÄ£¿éµÄÊý¾Ý;
}
}
4.2 4G程序
#include "4G.h"
#include "string.h"
#include "usart.h"
#include "led.h"
char *strx,*extstrx;
CSTX_4G CSTX_4G_Status; //Ä£¿éµÄ״̬ÐÅÏ¢
int errcount=0; //ʧ°Ü´ÎÊý ·ÀÖ¹ËÀÑ»·
int data_flag=0;//Êý¾Ý½ÓÊÜÐźÅ
char ATSTR[BUFLEN]; //×齨ATÃüÁîµÄº¯Êý
/*****************************************************
ÏÂÃæ¾ÍÊÇÐèÒªÐ޸ĵĵط½£¬Ð޸ķþÎñÆ÷µÄIPµØÖ·ºÍ¶Ë¿ÚºÅ
*****************************************************/
#define SERVERIP "115.236.153.170"
#define SERVERPORT 53289
/*****************************************************
Çå¿ÕÄ£¿é·´À¡µÄÐÅÏ¢
*****************************************************/
void Clear_Buffer(void)//Çå¿Õ»º´æ
{
printf("%s\r\n",buf_uart2.buf);
delay_ms(500);
buf_uart2.index=0;
memset(buf_uart2.buf,0,BUFLEN);
}
/*****************************************************
³õʼ»¯Ä£¿é ºÍµ¥Æ¬»úÁ¬½Ó£¬»ñÈ¡¿¨ºÅºÍÐźÅÖÊÁ¿
*****************************************************/
void CSTX_4G_Init(void)
{
//´òÓ¡³õʼ»¯ÐÅÏ¢
printf("start init A7680C\r\n");
//·¢µÚÒ»¸öÃüÁîAT+CPIN¼ì²é¿¨×´Ì¬
Uart2_SendStr("AT+CPIN?\r\n");
delay_ms(500);
printf("%s\r\n",buf_uart2.buf); //´òÓ¡´®¿ÚÊÕµ½µÄÐÅÏ¢
strx=strstr((const char*)buf_uart2.buf,(const char*)"R");//·µ»ØOK
Clear_Buffer();
while(strx==NULL)
{
printf("²éѯsim:%s......\r\n",buf_uart2.buf);
Clear_Buffer();
Uart2_SendStr("AT+CPIN?\r\n");
delay_ms(500);
strx=strstr((const char*)buf_uart2.buf,(const char*)"OK");//·µ»ØOK
}
printf("****sim¿¨Õý³£*****\r\n");
Uart2_SendStr("AT+CREG?\r\n");//¼ì²é¿¨ÊÇ·ñ×¢²áÉÏÍø
delay_ms(500);
strx=strstr((const char*)buf_uart2.buf,(const char*)"0,1");//·µ0£¬1
while(strx==NULL)
{
Clear_Buffer();
Uart2_SendStr("AT+CREG?\r\n");
delay_ms(500);
strx=strstr((const char*)buf_uart2.buf,(const char*)"0,1");//·µ0£¬1,˵Ã÷¿¨¿ÉÒÔÉÏÍø
}
printf("¿¨ÒѼ¤»î : %s \r\n",buf_uart2.buf);
Clear_Buffer();
Uart2_SendStr("AT+CSQ\r\n");//²é¿´»ñÈ¡CSQÖµ
delay_ms(500);
strx=strstr((const char*)buf_uart2.buf,(const char*)"+CSQ:");//·µ»ØCSQ
if(strx)
{
printf("ÐźÅÖÊÁ¿ÊÇ:%s \r\n",buf_uart2.buf+14);
}
}
/*****************************************************
¹Ø±Õ֮ǰ´æÔڵĺͷþÎñÆ÷µÄÁ´½Ó ¿ÉÄÜ·´À¡Ê§°Ü
*****************************************************/
void CSTX_4G_ConTCP(void)
{
//¹Ø±Õ֮ǰ½¨Á¢µÄÁ´½Ó
Uart2_SendStr("AT+CIPCLOSE=1\r\n");//¹Ø±ÕsocektÁ¬½Ó
delay_ms(500);
Clear_Buffer();
}
/*****************************************************
½¨Á¢TCPÁ´½Ó
*****************************************************/
void CSTX_4G_CreateTCPSokcet(void)//´´½¨sokcet
{
Uart2_SendStr("AT+NETOPEN\r\n");//´ò¿ªÍøÂ磬Æô¶¯TCP·þÎñ
delay_ms(500);
memset(ATSTR,0,BUFLEN);
sprintf(ATSTR,"AT+CIPOPEN=1,\"TCP\",\"%s\",%d\r\n",SERVERIP,SERVERPORT);
Uart2_SendStr(ATSTR);//´´½¨Á¬½ÓTCP,ÊäÈëIPÒÔ¼°·þÎñÆ÷¶Ë¿ÚºÅÂë
delay_ms(500);
strx=strstr((const char*)buf_uart2.buf,(const char*)"1,0");//¼ì²éÊÇ·ñµÇ½³É¹¦
Clear_Buffer();
//errcount=0;
while(strx==NULL)
{
errcount++;
strx=strstr((const char*)buf_uart2.buf,(const char*)"1,0");//¼ì²éÊÇ·ñµÇ½³É¹¦
delay_ms(500);
printf("%d\r\n",errcount);
if(errcount>50) //³¬Ê±Í˳öËÀÑ»· ±íʾ·þÎñÆ÷Á¬½Óʧ°Ü
{
errcount = 0;
break;
}
}
Clear_Buffer();
}
/*****************************************************
·¢ËÍÊý¾Ýº¯Êý
*****************************************************/
void CSTX_4G_Senddata(uint8_t *len,uint8_t *data)//·¢ËÍ×Ö·û´®Êý¾Ý
{
memset(ATSTR,0,BUFLEN);
sprintf(ATSTR,"AT+CIPSEND=1,%s\r\n",len);
Uart2_SendStr(ATSTR);
delay_ms(500);
//µÈ´ýÄ£¿é·´À¡ >
strx=strstr((const char*)buf_uart2.buf,(const char*)">");//Ä£¿é·´À¡¿ÉÒÔ·¢ËÍÊý¾ÝÁË
while(strx==NULL)
{
errcount++;
strx=strstr((const char*)buf_uart2.buf,(const char*)">");//Ä£¿é·´À¡¿ÉÒÔ·¢ËÍÊý¾ÝÁË
if(errcount>100) //·ÀÖ¹ËÀÑ»·Ìø³ö
{
errcount = 0;
break;
}
}
Uart2_SendStr((char *)data);//·¢ËÍÕæÕýµÄÊý¾Ý
delay_ms(1000);
strx=strstr((const char*)buf_uart2.buf,(const char*)"OK");//¼ì²éÊÇ·ñ·¢Ëͳɹ¦
errcount=0;
while(strx==NULL)
{
errcount++;
strx=strstr((const char*)buf_uart2.buf,(const char*)"OK");//¼ì²éÊÇ·ñ·¢Ëͳɹ¦
printf("f:%d\r\n",errcount);
delay_ms(100);
if(errcount>50) //³¬Ê±Í˳öËÀÑ»· ±íʾ·þÎñÆ÷Á¬½Óʧ°Ü
{
errcount = 0;
break;
}
}
Clear_Buffer();
}
/*****************************************************
ÊÕµ½·þÎñÆ÷Ï·¢µÄÊý¾Ý¾ÍÖ±½Ó´òÓ¡
*****************************************************/
void CSTX_4G_RECTCPData(void)
{
strx=strstr((const char*)buf_uart2.buf,(const char*)"+IPD8");//·µ»Ø+QIURC:£¬±íÃ÷½ÓÊÕµ½TCP·þÎñÆ÷·¢»ØµÄÊý¾Ý
//data_flag = 1;
if(strx)
{
Clear_Buffer();
//data_flag = 0;
}
}
接下来是头文件
#ifndef __BC26_H
#define __BC26_H
#include "usart.h"
#include <stm32f10x.h>
#include "delay.h"
void Clear_Buffer(void);//Çå¿Õ»º´æ
void CSTX_4G_Init(void);
void CSTX_4G_PDPACT(void);
void CSTX_4G_ConTCP(void);
void CSTX_4G_CreateTCPSokcet(void);
void CSTX_4G_Senddata(uint8_t *len,uint8_t *data);
void CSTX_4G_CreateSokcet(void);
void Clear_Buffer(void);
void CSTX_4G_ChecekConStatus(void);
void CSTX_4G_RECTCPData(void);
typedef struct
{
uint8_t CSQ;
uint8_t Socketnum; //񅧏
uint8_t reclen; //»ñÈ¡µ½Êý¾ÝµÄ³¤¶È
uint8_t res;
uint8_t recdatalen[10];
uint8_t recdata[100];
uint8_t netstatus;//ÍøÂçָʾµÆ
} CSTX_4G;
#endif
4.3主函数
接下来就是主函数
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "math.h"
#include "stdio.h"
#include "stm32f10x_flash.h"
#include "stdlib.h"
#include "string.h"
#include "wdg.h"
#include "timer.h"
#include "stm32f10x_tim.h"
#include "4G.h"
///***ʹÓÃÓÚ4G°æ±¾**************/
int main(void)
{
extern int data_flag;
int a=0;
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
NVIC_Configuration(); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
uart_init(115200);//´®¿Ú1³õʼ»¯£¬¿ÉÁ¬½ÓPC½øÐдòÓ¡Ä£¿é·µ»ØÊý¾Ý
Uart1_SendStr("UART1 Init Successful\r\n");
uart2_init(115200);//³õʼ»¯A7680CÁ¬½Ó´®¿Ú
Uart2_SendStr("UART2 Init Successful\r\n");
CSTX_4G_Init();//¶ÔÉ豸³õʼ»¯
CSTX_4G_ConTCP();//¹Ø±ÕÉÏÒ»´ÎÁ¬½Ó
CSTX_4G_CreateTCPSokcet();//´´½¨Ò»¸öSOCKETÁ¬½Ó
while(1)
{
CSTX_4G_Senddata("6","123456");//·¢Êý¾Ý
delay_ms(1000);
Clear_Buffer();
CSTX_4G_RECTCPData();//ÊÕÊý¾Ý,½ÓÊÕ·þÎñÆ÷Ï·¢µÄÊý¾Ý²¢´òÓ¡µ½´®¿Ú1½øÐÐÏÔʾ
Clear_Buffer();
}
}
好了,这就是STM32连接A7680C的全部过程