962|2

862

帖子

2

TA的资源

纯净的硅(初级)

楼主
 

【得捷Follow me第4期】W5500-EVB-Pico PIO UDP收发测试 [复制链接]

  本帖最后由 wo4fisher 于 2024-3-2 17:58 编辑

1. 新建基础工程并添加Ethernet3库文件到工程目录

2. UDP协议收发代码编辑

Internet 协议集支持一个无连接的传输协议,该协议称为用户数据报协议(UDP,User Datagram Protocol)。UDP 为应用程序提供了一种无需建立连接就可以发送封装的 IP 数据包的方法。

即UDP基于无连接,通过IP地址和端口即可以收发数据。

上代码:

  • #include <Arduino.h>
  • #include <SPI.h> // needed for Arduino versions later than 0018
  • #include <Ethernet3.h>
  • #include <EthernetUdp3.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  • // Enter a MAC address and IP address for your controller below.
  • // The IP address will be dependent on your local network:
  • byte mac[] = {
  • 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  • };
  • IPAddress ip(192, 168, 2, 177);
  • unsigned int localPort = 8888; // local port to listen on
  • // buffers for receiving and sending data
  • char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  • char ReplyBuffer[] = "acknowledged"; // a string to send back
  • // An EthernetUDP instance to let us send and receive packets over UDP
  • EthernetUDP Udp;
  • void setup() {
  • Ethernet.setCsPin(17);
  • Ethernet.setRstPin(20);
  • // start the Ethernet and UDP:
  • Ethernet.begin(mac, ip);
  • Udp.begin(localPort);
  • Serial.begin(115200);
  • }
  • void loop() {
  • // if there's data available, read a packet
  • int packetSize = Udp.parsePacket();
  • if (packetSize)
  • {
  • Serial.print("Received packet of size ");
  • Serial.println(packetSize);
  • Serial.print("From ");
  • IPAddress remote = Udp.remoteIP();
  • for (int i = 0; i < 4; i++)
  • {
  • Serial.print(remote[i], DEC);
  • if (i < 3)
  • {
  • Serial.print(".");
  • }
  • }
  • Serial.print(", port ");
  • Serial.println(Udp.remotePort());
  • // read the packet into packetBufffer
  • Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  • Serial.println("Contents:");
  • Serial.println(packetBuffer);
  • // send a reply, to the IP address and port that sent us the packet we received
  • Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  • Udp.write(ReplyBuffer);
  • Udp.endPacket();
  • }
  • delay(10);
  • }

主要步骤:

第一步:定义mac地址、本机ip地址、端口号和EthernetUDP 实例。

第二步:在setup中

初始化/定义cs引脚和rst引脚,

Ethernet.begin(mac, ip);初始化网络参数

Udp.begin(localPort);启动UDP服务器

第三步:在loop中

监听Udp.parsePacket(),如果有数据,进行解析

Udp.read()读取数据并通过串口输出后,再通过Udp.write()函数进行应答。

other:

UDP客户端实例就会以上例子反过来,因为UDP为无连接协议。因此

大概思路就是:

实例化UDP

封装发送数据

通过

    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

    Udp.write(ReplyBuffer);

    Udp.endPacket();

进行发送

然后解析、读取应答数据

Udp.parsePacket();

Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);

需要的话增加超时判断(可能没有应答)即可。

 

点赞 关注
个人签名水不撩不知深浅 人不拼怎知输赢
 
 

回复
举报

862

帖子

2

TA的资源

纯净的硅(初级)

沙发
 
UDP 测试pc端口
 

 

个人签名水不撩不知深浅 人不拼怎知输赢
 
 
 

回复

862

帖子

2

TA的资源

纯净的硅(初级)

板凳
 

udp rp2040串口打印信息:

个人签名水不撩不知深浅 人不拼怎知输赢
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
福禄克有奖直播:高精度测温赋能电子制造 报名中!
直播时间:2025年2月28日(周五)上午10:00
直播主题:高精度测温赋能电子制造
小伙伴们儿快来报名直播吧~好礼等你拿!

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表