单片机学习:lwip-udp

UDP编程虽然不难,但是有很多不懂得地方。
第一个就是UDP的块,udp_pcb主要是记录udp的信息,如本地IP,端口号,远程IP,远程端口号,recv函数,和自定义参数args。
这里写图片描述
一个程序中一般有多个控制块,这些控制块通过next指针连接在一起,但接收到一个数据块时就会遍历这些udp_pcb控制块,找到符合的控制块,接着调用recv中的回调函数进行处理。

第二个就是一些操作函数。

第一个就是udp_new()

udp_new() – Create a new UDP pcb

Synopsis

struct udp_pcb *udp_new(void);

Description

Creates a new connection identifier (PCB) which can be used for UDP
communication. The PCB is not active until it has either been bound to
a local address or connected to a remote address.

Return value

Returns the new PCB. If memory is not available for creating the new
PCB, NULL is returned.

(2)第二个是绑定函数udp_bind

udp_bind() – Bind PCB to local IP address and port

Synopsis

err_t udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port);

Description

Binds pcb to the local address indicated by ipaddr and port indicated
by port. ipaddr can be IP_ADDR_ANY to indicate that it should listen
to any local IP address. Port may be 0 for any port.

Return value

This function can return ERR_USE if all usable UDP dynamic ports are
used (only relevant if port is 0. Otherwise udp_bind() will always
return ERR_OK.

(3)第三个是连接函数udp_connect()

udp_connect() – Set remote UDP peer

Synopsis

err_t udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port);

Description

Sets the remote end of pcb. This function does not generate any
network traffic, but only sets the remote address of the pcb.

Return value

This function can return ERR_USE if all usable UDP dynamic
ports are used. Otherwise udp_connect() will always return ERR_OK.

(4)第四个是发送数据函数udp_send()

udp_send() – Send UDP packet

Synopsis

err_t udp_send(struct udp_pcb *pcb, struct pbuf *p);

Description

Sends the pbuf p to the remote host associated with pcb. The pbuf is
not deallocated.

Return value

This function returns ERR_OK on success; but may return ERR_MEM if
there is insufficient memory to prepend a UDP header, or ERR_RTE if no
suitable outgoing network interface could be found to route the packet
on.

send和sendto的区别是sendto需要添加远程ip地址和端口。

(5)第五个是udp_recv()函数,这是一个非常重要的函数。

udp_recv() – Set callback for incoming UDP data

Synopsis

void udp_recv(struct udp_pcb *pcb, err_t (*recv) (void *arg, struct
udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port), void
*recv_arg);

Description

Registers a callback function recv with the PCB pcb so that when a UDP
datagram is received, the callback is invoked. The callback argument
arg is set as the argument recv_arg to udp_recv(). The received
datagram packet buffer is held in p. The source address of the
datagram is provided in addr, and the source port in port. The
callback is expected to free the packet.

recv函数中的第二参数是一个函数指针,但接收到数据的时候,就会调用这个函数指针指向的函数,其实就是一个回调函数。这个回调函数有5个参数,分别是:arg表示传递给函数的自定义数据;pcb指向接收到报文的UDP控制块结构;p指向接收到的报文pbuf;addr表示发送该报文的源主机的IP地址;port表示发送该报文的源主机端口。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32F1是意法半导体公司推出的一款32位ARM Cortex-M3单片机系列产品,具有良好的性能和扩展能力。它采用了低功耗技术,集成了丰富的外设和内存,适用于广泛的应用领域。 寄存器是计算机体系结构中的重要组成部分,用于存储和操作数据。STM32F1芯片内部集成了大量的寄存器,包括通用寄存器、特殊功能寄存器和外设寄存器等。通过对寄存器的读写操作,可以实现对芯片内部各种功能的配置和控制。 LwIP-2.1.2是一个开源的轻量级网络协议栈,适用于嵌入式系统。它提供了TCP/IP协议栈的实现,支持各种网络协议和服务,例如IP、TCP、UDP、ARP、DHCP、DNS等。LwIP-2.1.2具有较小的内存占用和高性能的特点,适用于资源有限的嵌入式环境。 ENC28J60是一款低成本的SPI以太网控制器芯片,由微芯科技(Microchip Technology)公司推出。它支持10Mbps以太网通信,采用硬件SPI接口和内部缓存,能够有效减少主控制器的负担。ENC28J60与STM32F1可以通过SPI总线进行连接,用于实现嵌入式设备与以太网的通信。 综上所述,STM32F1是一款强大的单片机系列产品,具备丰富的外设和可编程寄存器,可以灵活配置和控制芯片内部功能。LwIP-2.1.2是一个轻量级的网络协议栈,用于实现嵌入式系统的网络通信。ENC28J60是一款低成本的以太网控制器芯片,可以与STM32F1通过SPI总线进行连接。这些技术的结合可以实现嵌入式设备的网络功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值