STM32+W5500+以太网应用开发+003_TCP服务器添加OLED(u8g2)显示状态
STM32+W5500+以太网应用开发+003_TCP服务器添加OLED(u8g2)显示状态
- 实验效果3-TCP服务器+OLED
- 1 拷贝显示驱动代码
- 1.1 拷贝源代码
- 1.2 将源代码添加到工程
- 1.3 修改代码优化等级
- 1.4 添加头文件路径
- 1.5 修改STM32CubeMX工程
- 2 修改源代码
- 2.1 添加头文件
- 2.2 main函数修改
- 3 实验测试效果
实验效果3-TCP服务器+OLED
1 拷贝显示驱动代码
1.1 拷贝源代码
拷贝HARDWARE文件夹,里面包括OLED.c/.h和移植好的U8g2csrc文件夹
1.2 将源代码添加到工程
将代码分别添加到工程的User/OLED组和User/U8G2组(所有.c文件都添加)
1.3 修改代码优化等级
这里添加的好多文件用不到,需要把代码优化等级调整到Level 1
1.4 添加头文件路径
…\HARDWARE\oled
…\HARDWARE\U8g2csrc
1.5 修改STM32CubeMX工程
只需要添加3个控制引脚,SPI和W5500是共用的
2 修改源代码
源代码全部在main.c里面修改
2.1 添加头文件
/* USER CODE BEGIN Includes */
#include <string.h>
#include <stdio.h>
#include "w5500_spi.h"
#include "wizchip_conf.h"
#include "socket.h"
#include "OLED.h"
#include "u8g2_app.h"
/* USER CODE END Includes */
2.2 main函数修改
局部变量
/* USER CODE BEGIN 1 */
u8g2_t u8g2;
char draw_buf[20];
/* USER CODE END 1 */
显示初始化
/* USER CODE BEGIN 2 */
OLED_RST_Set();//OLED初始化
HAL_Delay(100);
OLED_RST_Clr();
HAL_Delay(200);
OLED_RST_Set();
OLED_Init();
u8g2Init(&u8g2);//U8G2初始化
u8g2_FirstPage(&u8g2);
printf("A simple TCP Server Application using W5500!\r\n");
W5500Init(); //W5500初始化
ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); //配置网络参数
wiz_PhyConf phyconf;
phyconf.by = PHY_CONFBY_SW;
phyconf.duplex = PHY_DUPLEX_FULL;
phyconf.speed = PHY_SPEED_10;
phyconf.mode = PHY_MODE_AUTONEGO;
ctlwizchip(CW_SET_PHYCONF, (void*)&phyconf); //配置PHY参数
draw_waitcable(&u8g2);//OLED提示:等待网线连接
PHYStatusCheck(); //检查网络连接状态
PrintPHYConf(); //打印PHY配置信息
printf("Simple TCP Server Application\r\n");
draw_waitclient(&u8g2);//OLED提示:等待客户端连接
/* USER CODE END 2 */
主循环
添加的代码部分有“OLED提示:”注释
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
printf("\r\nInitializing server socket\r\n");
//Parameters in order socket_id, protocol TCP or UDP, Port number, Flags=0
//Return value is socket ID on success
if(socket(1,Sn_MR_TCP,LISTEN_PORT,0)!=1)//创建一个socket:Socket号,TCP/UDP类型,端口号
{
//error
printf("Cannot create Socket!\r\n");
while(1);//halt here
}
//success
printf("Socket Created Successfully ! \r\n");
uint8_t socket_io_mode=SOCK_IO_BLOCK;
ctlsocket(1, CS_SET_IOMODE , &socket_io_mode);//set blocking IO mode
printf("IP Address is %d.%d.%d.%d\r\n",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
printf("Start listening on port %d ! \r\n",LISTEN_PORT);
printf("Waiting for a client connection. \r\n");
memset(draw_buf,0,20);
snprintf(draw_buf,20,"%d.%d.%d.%d:%d",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3],LISTEN_PORT);
draw_server(&u8g2,draw_buf);//OLED提示:等待客户端连接
//Make it a passive socket (i.e. listen for connection)
if(listen(1)!=SOCK_OK)//监听端口
{//our socket id is 1 (w5500 have 8 sockets from 0-7)
//error
printf("Cannot listen on port %d",LISTEN_PORT);
while(1);
}
uint8_t sr=0x00;//socket status register
do
{
sr=getSn_SR(1);//获取Sn_SR寄存器,参数0~7
}while (sr!=SOCK_ESTABLISHED && sr!=SOCK_CLOSED);
if(sr==SOCK_CLOSED)
{
printf("Some error occurred on server socket. Please restart.\r\n");
while(1);
}
if(sr==SOCK_ESTABLISHED)//成功连接
{
//we come here only when a client has connected.
//Now we can read data from the socket
printf("A client connected!\r\n");
printf("Waiting for Client Data ...!\r\n");
draw_conn_client(&u8g2);//OLED提示:客户端连接
while(1)
{
int len=recv(1, receive_buff, RECEIVE_BUFF_SIZE);//从连接设备读取数据到receive_buff
if(len==SOCKERR_SOCKSTATUS)
{
//client has disconnected
printf("Client has disconnected\r\n");
printf("*** SESSION OVER ***\r\n\r\n");
break;
}
receive_buff[len]='\0';
printf("Received %d bytes from client\r\n",len);
printf("Data Received: %s", receive_buff);
// memset(draw_buf,0,20);
// snprintf(draw_buf,20,"%d.%d.%d.%d:%d",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3],LISTEN_PORT);
draw_waitdata(&u8g2,(char *)receive_buff);//OLED提示:显示接收数据
if(strcmp((char*)receive_buff,"Who are u")==0)
{//判断接收到"Who are u"
memcpy(receive_buff,"I am role_2099!",15);//修改应答内容
len = 15;
}
//Echo the data back encloused in a [] pair
send(1,(uint8_t*)"[",1);//starting sq bracket 向客户端发送[
send(1,receive_buff,len);// the data 向客户端发送接收到的内容或者特定的回答
send(1,(uint8_t*)"]",1);//closing sq bracket 向客户端发送]
printf("\r\nECHO sent back to client\r\n");
//Look for quit message and quit if received
if(strcmp((char*)receive_buff,"QUIT")==0)
{//收到"QUIT",端口客户端连接
printf("Received QUIT command from client\r\n");
printf("Disconnecting ... \r\n");
printf("*** SESSION OVER ***\r\n\r\n");
memset(draw_buf,0,20);
snprintf(draw_buf,20,"%d.%d.%d.%d:%d",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3],LISTEN_PORT);
draw_server(&u8g2,draw_buf);//OLED提示:等待客户端连接
disconnect(1);//disconnect from the clinet 断开客户端连接
break;//come out of while loop 退出,回到131行,重新等待客户端连接
}
}//While loop (as long as client is connected)
}//if block, client connect success
}
/* USER CODE END 3 */
3 实验测试效果