前言
这是学习百问网7天智能家居的第3天,本篇文章将带大家了解ESP8266和STM之前的通信。
一、环形缓冲区
buff.c
#include "buff.h" #include <stdlib.h> #include <string.h> #include <stdio.h> int buff_Iint(Pbuffer buffer,u16 size) { //判断参数合法性 if(buffer==NULL||size==0) { return -1; } if(buffer->fifo==NULL) { buffer->fifo=(u8*)malloc(size);//申请空间 if(buffer->fifo==NULL)//申请空间失败 { return -1; } } //对缓冲区进行初始化 buffer->pr=buffer->pw=0; buffer->buf_size=size; return 0; } int buffer_write(Pbuffer buffer,const u8 data) { //判断参数合法性 if(buffer==NULL||buffer->fifo==NULL) { return -1; } //写入数据 if((buffer->pw+1)%buffer->buf_size!=buffer->pr) { buffer->fifo[buffer->pw]=data; buffer->pw=(buffer->pw+1)%buffer->buf_size; return 0; } return -1; } int buffer_writeBytes(Pbuffer buffer,u8 *data_strea,u8 len) { int i=0; //判断参数合法性 if(buffer==NULL||buffer->fifo==NULL) { return -1; } if(data_strea==NULL) { return -1; } if(len==0) { return -1; } for(i=0;i<len;i++) { if(buffer_write(buffer,data_strea[i])!=0) { break; } } return i; } int buffer_read(Pbuffer buffer,u8 *data) { //判断参数合法性 if(buffer==NULL||buffer->fifo==NULL) { return -1; } if(buffer->pr!=buffer->pw) { *data=buffer->fifo[buffer->pr]; buffer->pr=(buffer->pr+1)%buffer->buf_size; return 0; } return 0; } int buffer_readBytes(Pbuffer buffer,u8 *data_strea,u8 len) { int i=0; //判断参数合法性 if(buffer==NULL||buffer->fifo==NULL) { return -1; } if(data_strea==NULL) { return -1; } if(len==0) { return -1; } for(i=0;i<len;i++) { if(buffer_read(buffer,&data_strea[i])!=0) { break; } } return i; } int buffer_clean(Pbuffer buffer) { //判断参数合法性 if(buffer==NULL||buffer->fifo==NULL) { return -1; } memset(buffer->fifo,0,sizeof(buffer->buf_size));//数据全部清0 buffer->pr=buffer->pw=0;//读写置0 return 0; }
buff.h
#ifndef _BUFF_H_ #define _BUFF_H_ #include "main.h" #define u8 unsigned char #define u16 unsigned short #define u32 unsigned int typedef struct { u8 *fifo; u16 pw;//读指针 u16 pr;//写指针 u16 buf_size;//环形缓冲区大小 }buffer,*Pbuffer; int buff_Iint(Pbuffer buffer,u16 size); int buffer_write(Pbuffer buffer,u8 data); int buffer_read(Pbuffer buffer,u8 *data); int buffer_clean(Pbuffer buffer); int buffer_writeBytes(Pbuffer buffer,u8 *data_strea,u8 len); int buffer_readBytes(Pbuffer buffer,u8 *data_strea,u8 len); #endif
二、ESP8266AT指令集
使用下面的AT指令即可操作ESP8266模块
总结
本文就简单介绍到这里,百问网下一节课将带我们如何去使用STM32串口对ESP8266发送信息。