環境及工具與第一章相同,這裏就不在重複!api
串口發送示例代碼以下:數組
os_printf("hello_esp_8266\r\n");//使用這個語句必需包含#include "osapi.h"頭文件
串口接收稍微複雜一些,須要先緩存接收到的字符,而後在進行判斷,示例代碼以下(代碼位於SDK/app/driver/uart.c):緩存
LOCAL void ICACHE_FLASH_ATTR /////// uart_recvTask(os_event_t *events) { if(events->sig == 0){ #if UART_BUFF_EN Uart_rx_buff_enq(); #else uint8 fifo_len = (READ_PERI_REG(UART_STATUS(UART0))>>UART_RXFIFO_CNT_S)&UART_RXFIFO_CNT; uint8 d_tmp = 0; uint8 idx=0; uint8 cache[fifo_len];//新建一個緩存數組 for(idx=0;idx<fifo_len;idx++) { d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF; cache[idx]=d_tmp;//將接收到的值存儲到數組 uart_tx_one_char(UART0, d_tmp); } if(cache[0]=='a'&&cache[1]=='b'){//這裏處理本身的代碼 os_printf("finally found you\r\n"); } WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR); uart_rx_intr_enable(UART0); #endif }else if(events->sig == 1){ #if UART_BUFF_EN //already move uart buffer output to uart empty interrupt //tx_start_uart_buffer(UART0); #else #endif } }