---
title: "STM32 W5500 UDP"
url: "https://maker.wiznet.io/tonywang_/projects/stm32-w5500-udp/"
markdown_url: "https://maker.wiznet.io/tonywang_/projects/stm32-w5500-udp/md"
type: "UCC: User Created Content"
author: "tonywang_"
author_url: "https://maker.wiznet.io/tonywang_/"
original_author: "Mr_Johhny"
original_url: "https://blog.csdn.net/lnniyunlong99/article/details/105219644?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165391438916781483746346%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165391438916781483746346&bi"
published: "2022-11-02"
language: "en"
likes: 7
views: 1636
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# STM32 W5500 UDP

> This blog introduce how to use W5500 to setup UDP socket.

Original author: Mr_Johhny (source: https://blog.csdn.net/lnniyunlong99/article/details/105219644?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165391438916781483746346%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165391438916781483746346&bi)

## Article

STM32F103RC 驱动W5500，使用[UDP](https://so.csdn.net/so/search?q=UDP&spm=1001.2101.3001.7020) Client主动向指定IP和端口发数据测试。

1、首先保证STM32驱动驱动W5500入网可ping通，[查看文章《STM32F103RC驱动W5500入网，并可ping通》](https://blog.csdn.net/lnniyunlong99/article/details/100882032)

2、设置W5500发送和接收的缓冲区大小

3、创建UDP[套接字](https://so.csdn.net/so/search?q=%E5%A5%97%E6%8E%A5%E5%AD%97&spm=1001.2101.3001.7020)，并发送数据

测试的主函数如下：

#ifndef __STM32F10X_H
#define __STM32F10X_H
#include "stm32f10x.h"
#endif

#ifndef __Z_UTIL_TIME_H
#define __Z_UTIL_TIME_H
#include "z_util_time.h"
#endif

#ifndef __Z_HARDWARE_LED_H
#define __Z_HARDWARE_LED_H
#include "z_hardware_led.h"
#endif

#ifndef __Z_HARDWARE_SPI_H
#define __Z_HARDWARE_SPI_H
#include "z_hardware_spi.h"
#endif

#include "w5500.h"
#include "socket.h"
#include "w5500_conf.h"

uint8 buffer[128];

int main(void)
{
uint8 mac[6]={0, };
uint8 lip[4]={192,168,1,110};
uint8 sub[4]={255,255,255,0};
uint8 gw[4]={192,168,1,1};

uint8 remote_ip[4]={192,168,1,105};
uint16 remote_port=6000;
uint16 local_port=5000;
uint16 len;

init_led();
init_system_spi();
func_w5500_reset();

getMacByLockCode(mac);
setSHAR(mac);
setSUBR(sub);
setGAR(gw);
setSIPR(lip);

sysinit(txsize, rxsize);
setRTR(2000);
setRCR(3);

for(;;)
{
socket(0, Sn_MR_UDP, local_port, 0);
buffer[0] = 0x55;
buffer[1] = 0xAA;
len = 2;
sendto(0, buffer, len, remote_ip, remote_port);

func_led1_on();
delay_ms(500);
func_led1_off();
delay_ms(500);

}
}

程序不断向192.168.1.105的IP，6000的端口发送55 AA的十六进制数据。

然而可能会发生这种现象：

通过cmd命令窗口ping 192.168.1.110可以ping通，但是PC的网络工具收不到 W5500发来的数据，以为是程序写错了，没调好，其实不是这样的。

临时解决办法，通过PC网络工具向W5500设定的IP和端口主动发一组数据（随意发），PC网络助手就可以收到W5500发来的数据了。

出现该现象的原因是：W5500写的固定IP地址，并没有向路由器报备，这个时候路由器不会给路由器不识别的设备转发数据。

当PC网络助手主动向W5500设置IP和端口发数据后，路由器会认为有这个设备（W5500），再从W5500发出来的数据，路由器就会转发。

这个时候，登陆了路由器的管理页面也会看到多了一个有线连接的设备，未设置过设备名称的，路由器会显示未知设备，由于我设置过名称（通过DHCP设置的），所以路由器还是会记录录下来。附上一张图：

![](https://img-blog.csdnimg.cn/20200331122733279.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xubml5dW5sb25nOTk=,size_16,color_FFFFFF,t_70)

真正的解决办法是DHCP，动态向路由器申请IP，并报备设备信息，以后我会有文章描述。

程序的测试结果图：

![](https://img-blog.csdnimg.cn/20200331123451467.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xubml5dW5sb25nOTk=,size_16,color_FFFFFF,t_70)

UDP套接字的应用场景：DHCP、DNS、NTP等。

附上本次的测试代码：[工程测试代码《01_W5500_UDP_send.rar》](https://download.csdn.net/download/lnniyunlong99/12287584)。

再附上UDP回环测试的主函数代码，如下：

#ifndef __STM32F10X_H
#define __STM32F10X_H
#include "stm32f10x.h"
#endif

#ifndef __Z_UTIL_TIME_H
#define __Z_UTIL_TIME_H
#include "z_util_time.h"
#endif

#ifndef __Z_HARDWARE_LED_H
#define __Z_HARDWARE_LED_H
#include "z_hardware_led.h"
#endif

#ifndef __Z_HARDWARE_SPI_H
#define __Z_HARDWARE_SPI_H
#include "z_hardware_spi.h"
#endif

#include "w5500.h"
#include "socket.h"
#include "w5500_conf.h"

uint8 buffer[128];

int main(void)
{
uint8 mac[6]={0, };
uint8 lip[4]={192,168,1,110};
uint8 sub[4]={255,255,255,0};
uint8 gw[4]={192,168,1,1};

uint8 remote_ip[4]={192,168,1,105};
uint16 remote_port=6000;
uint16 local_port=5000;
uint16 len;

init_led();
init_system_spi();
func_w5500_reset();

getMacByLockCode(mac);
setSHAR(mac);
setSUBR(sub);
setGAR(gw);
setSIPR(lip);

sysinit(txsize, rxsize);
setRTR(2000);
setRCR(3);

for(;;)
{
switch(getSn_SR(0))
{
case SOCK_UDP:
delay_ms(100);
if(getSn_IR(0) & Sn_IR_RECV)
{
setSn_IR(0, Sn_IR_RECV);
}
if((len=getSn_RX_RSR(0))>0)
{
memset(buffer,0,len+1);
len = recvfrom(0,buffer, len, remote_ip,&remote_port);
sendto(0,buffer,len, remote_ip, remote_port);
}
break;
case SOCK_CLOSED:
socket(0,Sn_MR_UDP,local_port,0);
break;
}

func_led1_on();
delay_ms(500);
func_led1_off();
delay_ms(500);

}
}

回环测试效果如下：

![](https://img-blog.csdnimg.cn/20200331131216573.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xubml5dW5sb25nOTk=,size_16,color_FFFFFF,t_70)

---

Source: https://maker.wiznet.io/tonywang_/projects/stm32-w5500-udp/
