【STM32F723 Discovery Kit】tcp server和tcp client测试
[复制链接]
这篇来测试下,在w5500模块上实现tcp server和tcp client测试。在上篇https://bbs.eeworld.com.cn/thread-1262011-1-1.html的基础上,添加tcp 代码。
一、代码
1.1、tcp_demo.c
/**
******************************************************************************
* [url=home.php?mod=space&uid=1307177]@File[/url] tcp_demo.c
* [url=home.php?mod=space&uid=252314]@version[/url] V1.2
* [url=home.php?mod=space&uid=311857]@date[/url] 2017-11-01
* [url=home.php?mod=space&uid=159083]@brief[/url] TCP ÑÝʾº¯Êý
******************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include "tcp_demo.h"
#include "W5500_conf.h"
#include "w5500.h"
#include "socket.h"
#include "utility.h"
uint8 buff[2048]; /*¶¨ÒåÒ»¸ö2KBµÄ»º´æ*/
/**
*@brief TCP Server»Ø»·ÑÝʾº¯Êý¡£
*@param ÎÞ
*[url=home.php?mod=space&uid=784970]@return[/url] ÎÞ
*/
void do_tcp_server(void)
{
uint16 len=0;
switch(getSn_SR(SOCK_TCPS)) /*»ñÈ¡socketµÄ״̬*/
{
case SOCK_CLOSED: /*socket´¦ÓڹرÕ״̬*/
socket(SOCK_TCPS ,Sn_MR_TCP,local_port,Sn_MR_ND); /*´ò¿ªsocket*/
break;
case SOCK_INIT: /*socketÒѳõʼ»¯×´Ì¬*/
listen(SOCK_TCPS); /*socket½¨Á¢¼àÌý*/
break;
case SOCK_ESTABLISHED: /*socket´¦ÓÚÁ¬½Ó½¨Á¢×´Ì¬*/
if(getSn_IR(SOCK_TCPS) & Sn_IR_CON)
{
setSn_IR(SOCK_TCPS, Sn_IR_CON); /*Çå³ý½ÓÊÕÖжϱê־λ*/
}
len=getSn_RX_RSR(SOCK_TCPS); /*¶¨ÒålenΪÒѽÓÊÕÊý¾ÝµÄ³¤¶È*/
if(len>0)
{
recv(SOCK_TCPS,buff,len); /*½ÓÊÕÀ´×ÔClientµÄÊý¾Ý*/
// buff[len]=0x00; /*Ìí¼Ó×Ö·û´®½áÊø·û*/
// printf("%s\r\n",buff);
send(SOCK_TCPS,buff,len); /*ÏòClient·¢ËÍÊý¾Ý*/
}
break;
case SOCK_CLOSE_WAIT: /*socket´¦Óڵȴý¹Ø±Õ״̬*/
close(SOCK_TCPS);
break;
}
}
/**
*@brief TCP Client»Ø»·ÑÝʾº¯Êý¡£
*@param ÎÞ
*@return ÎÞ
*/
void do_tcp_client(void)
{
uint16 len=0;
switch(getSn_SR(SOCK_TCPC)) /*»ñÈ¡socketµÄ״̬*/
{
case SOCK_CLOSED: /*socket´¦ÓڹرÕ״̬*/
socket(SOCK_TCPC,Sn_MR_TCP,local_port++,Sn_MR_ND);
break;
case SOCK_INIT: /*socket´¦ÓÚ³õʼ»¯×´Ì¬*/
connect(SOCK_TCPC,remote_ip,remote_port); /*socketÁ¬½Ó·þÎñÆ÷*/
break;
case SOCK_ESTABLISHED: /*socket´¦ÓÚÁ¬½Ó½¨Á¢×´Ì¬*/
if(getSn_IR(SOCK_TCPC) & Sn_IR_CON)
{
setSn_IR(SOCK_TCPC, Sn_IR_CON); /*Çå³ý½ÓÊÕÖжϱê־λ*/
}
len=getSn_RX_RSR(SOCK_TCPC); /*¶¨ÒålenΪÒѽÓÊÕÊý¾ÝµÄ³¤¶È*/
if(len>0)
{
recv(SOCK_TCPC,buff,len); /*½ÓÊÕÀ´×ÔServerµÄÊý¾Ý*/
buff[len]=0x00; /*Ìí¼Ó×Ö·û´®½áÊø·û*/
printf("%s\r\n",buff);
send(SOCK_TCPC,buff,len); /*ÏòServer·¢ËÍÊý¾Ý*/
}
break;
case SOCK_CLOSE_WAIT: /*socket´¦Óڵȴý¹Ø±Õ״̬*/
close(SOCK_TCPC);
break;
}
}
1.2、tcp_demo.h
#ifndef __TCP_DEMO_H
#define __TCP_DEMO_H
#include "types.h"
extern uint16 W5500_tcp_server_port;
void do_tcp_server(void);//TCP Server»Ø»·ÑÝʾº¯Êý
void do_tcp_client(void);//TCP Clinet»Ø»·ÑÝʾº¯Êý
void setKPALVTR(SOCKET i,uint8 val);
#endif
1.3、main.c
tcp server测试
#include "main.h"
#include "led/led.h"
#include "delay/delay.h"
#include "key/key.h"
#include "usart/usart.h"
#include "sram/sram.h"
#include "STM32F723E-Discovery/stm32f723e_discovery_lcd.h"
#include "STM32F723E-Discovery/stm32f723e_discovery_qspi.h"
#include "i2c/i2c.h"
#include "spi/spi.h"
#include "tim/tim.h"
#include "w5500_conf.h"
#include "w5500.h"
#include "ping.h"
#include "tcp_demo.h"
static void MPU_Config(void);
static void SystemClock_Config(void);
static void CPU_CACHE_Enable(void);
int main(void)
{
uint16_t i=0;
uint8_t txbuf[10];
uint8_t rxbuf[10];
HAL_Init();
MPU_Config(); /* Configure the MPU attributes */
CPU_CACHE_Enable(); /* Enable the CPU Cache */
SystemClock_Config(); /* Configure the system clock to 216 MHz */
delay_init(216);
led_init();
usart_init(115200);
init_spi();
BSP_LCD_Init();
gpio_for_w5500_config();
reset_w5500();
PHY_check(); /*¼ì²éÍøÏßÊÇ·ñ½ÓÈë*/
set_w5500_mac(); /*ÅäÖÃMACµØÖ·*/
set_w5500_ip(); /*ÅäÖÃIPµØÖ·*/
socket_buf_init(txsize, rxsize); /*³õʼ»¯8¸öSocketµÄ·¢ËͽÓÊÕ»º´æ´óС*/
printf(" tcp server test! \r\n");
LED5(0);
LED6(1);
BSP_LCD_Clear(LCD_COLOR_RED);
printf(" tcp server port: %d! \r\n",local_port);
while (1)
{
do_tcp_server(); /*TCP_Server Êý¾Ý»Ø»·²âÊÔ³ÌÐò*/
}
}
1.4、main.c
tcp client测试
#include "main.h"
#include "led/led.h"
#include "delay/delay.h"
#include "key/key.h"
#include "usart/usart.h"
#include "sram/sram.h"
#include "STM32F723E-Discovery/stm32f723e_discovery_lcd.h"
#include "STM32F723E-Discovery/stm32f723e_discovery_qspi.h"
#include "i2c/i2c.h"
#include "spi/spi.h"
#include "tim/tim.h"
#include "w5500_conf.h"
#include "w5500.h"
#include "ping.h"
#include "tcp_demo.h"
static void MPU_Config(void);
static void SystemClock_Config(void);
static void CPU_CACHE_Enable(void);
int main(void)
{
uint16_t i=0;
uint8_t txbuf[10];
uint8_t rxbuf[10];
HAL_Init();
MPU_Config(); /* Configure the MPU attributes */
CPU_CACHE_Enable(); /* Enable the CPU Cache */
SystemClock_Config(); /* Configure the system clock to 216 MHz */
delay_init(216);
led_init();
usart_init(115200);
init_spi();
BSP_LCD_Init();
gpio_for_w5500_config();
reset_w5500();
PHY_check(); /*¼ì²éÍøÏßÊÇ·ñ½ÓÈë*/
set_w5500_mac(); /*ÅäÖÃMACµØÖ·*/
set_w5500_ip(); /*ÅäÖÃIPµØÖ·*/
socket_buf_init(txsize, rxsize); /*³õʼ»¯8¸öSocketµÄ·¢ËͽÓÊÕ»º´æ´óС*/
printf(" tcp client test! \r\n");
LED5(0);
LED6(1);
BSP_LCD_Clear(LCD_COLOR_RED);
printf("server ip:%d.%d.%d.%d\r\n",remote_ip[0],remote_ip[1],remote_ip[2],remote_ip[3]);
printf("port :%d \r\n",remote_port);
while (1)
{
do_tcp_client();
}
}
二、运行结果
2.1、tcp server 测试
串口输出
网络调试助手
开发板回复网络调试助手发送的内容
2.2、tcp client 测试
先打开网络调试助手,创建服务器,然后给开发板上电,建立网络连接
建立连接后,网络调试助手发送消息后,收到开发板的回复,同时串口输出收到的消息。
以上是tcp server 和tcp client测试的内容。
|