查看: 2058|回复: 0

[原创] 【赚周年币】Fireduino A12.WiF UDP测试

[复制链接]
  • TA的每日心情
    开心
    2024-3-3 21:23
  • 签到天数: 2449 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2017-1-8 11:04:36 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 limale 于 2017-1-17 20:29 编辑

    先前测试了TCP,今天我们来了解及测试一下另外一种数据传输协议UDP。我也是边学边用还是先脑补一下吧。

    UDP(User Data Protocol,用户数据报协议)
    (1) UDP是一个非连接的协议,传输数据之前源端和终端不建立连接,当它想传送时就简单地去抓取来自应用程序的数据,并尽可能快地把它扔到网络上。在发送端,UDP传送数据的速度仅仅是受应用程序生成数据的速度、计算机的能力和传输带宽的限制;在接收端,UDP把每个消息段放在队列中,应用程序每次从队列中读一个消息段。
    (2) 由于传输数据不建立连接,因此也就不需要维护连接状态,包括收发状态等,因此一台服务机可同时向多个客户机传输相同的消息。
    (3) UDP信息包的标题很短,只有8个字节,相对于TCP的20个字节信息包的额外开销很小。
    (4) 吞吐量不受拥挤控制算法的调节,只受应用软件生成数据的速率、传输带宽、源端和终端主机性能的限制。
    (5)UDP使用尽最大努力交付,即不保证可靠交付,因此主机不需要维持复杂的链接状态表(这里面有许多参数)。
    (6)UDP是面向报文的。发送方的UDP对应用程序交下来的报文,在添加首部后就向下交付给IP层。既不拆分,也不合并,而是保留这些报文的边界,因此,应用程序需要选择合适的报文大小。
    我们经常使用“ping”命令来测试两台主机之间TCP/IP通信是否正常,其实“ping”命令的原理就是向对方主机发送UDP数据包,然后对方主机确认收到数据包,如果数据包是否到达的消息及时反馈回来,那么网络就是通的。
    UDP的包头结构:
    源端口 16位
    目的端口 16位
    长度 16位
    校验和 16位

    Fireduino WiFi UDP流程
    第一步:
    创建一个 WiFiUDP对象,接着调用对象函数begin函数,初始化UDP对象以及开始监听端口,参数为需要监听的端口号。
    第二步:
    数据的接受
    如果UDP端口接到到远程发送的数据,通过调用对象parsePacket函数可以获取数据包大小。之后通过调用对象函数read 读取数据。
    数据的发送
    UDP 数据包的发送需要通过对象函数beginPacket指定远程主机的IP地址和端口号,之后调用对象函数write写入数据后,调用对象函数endPacket完成组包后扔到网络发送。
    第三步:
    完成数据传输后可以调用UDP对象 stop 函数 释放UDP资源。

    小结TCP与UDP的区别:
    1.基于连接与无连接;
    2.对系统资源的要求(TCP较多,UDP少);
    3.UDP程序结构较简单;
    4.流模式与数据报模式 ;
    5.TCP保证数据正确性,UDP可能丢包,TCP保证数据顺序,UDP不保证。

    首先实例化UDP对象并且指定本地端口号为2390。
    QQ截图20170108105148.jpg
    这里指定本地端口启动UDP服务,电脑作为UDP Client。
    QQ截图20170108105214.jpg
    如果有接收到Client发送的消息,通过串口打印相关的内容,同时Server向Client发送反馈消息通信结束,依次循环。
    QQ截图20170108105331.jpg
    QQ截图20170108104554.jpg
    QQ截图20170108104624.jpg
    主要代码:
    1. #include <Arduino.h>
    2. #include "Wire.h"
    3. #include "oled.h"
    4. #include "oledfont.h"
    5. #include "TF.h"
    6. #include "Audio.h"
    7. #include <WiFi.h>
    8. #include <WiFiUdp.h>

    9. //板载led
    10. #define LED_GREEN   13
    11. #define LED_BLUE    3

    12. #define Trigger_T_measuremnet_hold_master         0xe3
    13. #define Trigger_RH_measuremnet_hold_master        0xe5
    14. extern int16_t SHT20_Read_value(uint8_t value);
    15. uint16_t SHT20_val;
    16. float SHT20_val_temp;
    17. float SHT20_val_humi;
    18. char str[15] = {};
    19. char music1[10] = {"love.MP3"};
    20. char music2[10] = {"sara.MP3"};

    21. //char ssid[] = {"network ssid"};       //your network SSID(name)
    22. //char pwd[]  = {"123456"};             //your network password
    23. int status = WL_IDLE_STATUS;
    24. unsigned int localPort = 2390;      // local port to listen on
    25. char packetBuffer[255]; //buffer to hold incoming packet
    26. char  ReplyBuffer[] = "hello!!!";       // a string to send back
    27. WiFiUDP Udp;

    28. boolean alreadyConnected = false;

    29. void printWifiStatus();

    30. void Sys_Init(void)
    31. {
    32.   pinMode(OLED_GND, OUTPUT);
    33.   pinMode(OLED_VCC, OUTPUT);
    34.   pinMode(OLED_CLK, OUTPUT);
    35.   pinMode(OLED_DIN, OUTPUT);
    36.   pinMode(OLED_RES, OUTPUT);
    37.   pinMode(OLED_DC, OUTPUT);
    38.   pinMode(OLED_CS, OUTPUT);
    39.   digitalWrite(OLED_GND,LOW);
    40.   digitalWrite(OLED_VCC,HIGH);
    41.   
    42.   pinMode(LED_GREEN, OUTPUT);
    43.   digitalWrite(LED_GREEN,HIGH);
    44.   Wire.begin();
    45.   OLED_Init();
    46.   OLED_Clear();
    47.   
    48.   delay(500);
    49.   Serial.begin(115200);
    50.   Serial.print("\r\narduino setup ...\r\n");
    51.   
    52.   if(!SD.begin())
    53.   {
    54.     Serial.println("sd init err\r\n");
    55.     while(1);
    56.   }
    57.   // check for the presence of the shield:
    58.   if (WiFi.status() == WL_NO_SHIELD) {
    59.     Serial.println("WiFi shield not present");
    60.     // don't continue:
    61.     while (true);
    62.   }
    63.   String fv = WiFi.firmwareVersion();
    64.   if (fv != "1.1.0") {
    65.     Serial.println("Please upgrade the firmware");
    66.   }
    67.   // attempt to connect to Wifi network:
    68.   while (status != WL_CONNECTED) {
    69.     Serial.print("Attempting to connect to SSID: ");
    70.     Serial.println(ssid);
    71.     // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    72.     status = WiFi.begin(ssid, pwd);
    73.     // wait 10 seconds for connection:
    74.     // delay(10000);
    75.   }
    76.   Serial.println("Connected to wifi");
    77.   printWifiStatus();
    78.   Serial.println("\nStarting connection to server...");
    79.   // if you get a connection, report back via serial:
    80.   Udp.begin(localPort);
    81. }

    82. void Main(void)
    83. {
    84.   char str1[] = {"eeboard"};
    85.   char str2[] = {"www.eeboard.com"};  
    86.   uint8_t i = 4;
    87.   
    88.   OLED_ShowCHinese(i, 0, 0);
    89.   OLED_ShowCHinese(i+16, 0, 1);
    90.   OLED_ShowString(i+32, 0, str1, 16);
    91.   OLED_ShowCHinese(i+88,0, 2);
    92.   OLED_ShowCHinese(i+104,0, 3);
    93. //  OLED_ShowString(4,2,str2,16);
    94.   while(1)
    95.   {
    96.       SHT20_val = SHT20_Read_value(Trigger_RH_measuremnet_hold_master);
    97.       SHT20_val &= ~0x0003;
    98.       SHT20_val_humi = -6.0 + 125.0 / 65536 * (float)SHT20_val;
    99.       sprintf(str, "= %5.2f RH ", SHT20_val_humi);
    100.       OLED_ShowCHinese(0, 4, 4);
    101.       OLED_ShowCHinese(16, 4, 5);
    102.       OLED_ShowString(32, 4, str, 16);
    103.       
    104.       SHT20_val = SHT20_Read_value(Trigger_T_measuremnet_hold_master);
    105.       SHT20_val &= ~0x0003;
    106.       SHT20_val_temp = -46.85 + 175.72 / 65536 * (float)SHT20_val;
    107.       sprintf(str, "= %5.2f C ", SHT20_val_temp);
    108.       OLED_ShowCHinese(0, 6, 6);
    109.       OLED_ShowCHinese(16, 6, 7);
    110.       OLED_ShowString(32, 6, str, 16);

    111.       // if there's data available, read a packet
    112.       int packetSize = Udp.parsePacket();
    113.       if (packetSize) {
    114.       Serial.print("Received packet of size ");
    115.       Serial.println(packetSize);
    116.       Serial.print("From ");
    117.       IPAddress remoteIp = Udp.remoteIP();
    118.       Serial.print(remoteIp);
    119.       Serial.print(", port ");
    120.       Serial.println(Udp.remotePort());
    121.       // read the packet into packetBufffer
    122.       int len = Udp.read(packetBuffer, 255);
    123.       if (len > 0) {
    124.         packetBuffer[len] = 0;
    125.       }
    126.       Serial.println("Contents:");
    127.       Serial.println(packetBuffer);
    128.       // send a reply, to the IP address and port that sent us the packet we received
    129.       Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    130.       Udp.write(ReplyBuffer);
    131.       Udp.endPacket();
    132.     }
    133.   }  
    134. }
    135. void printWifiStatus() {
    136.   // print the SSID of the network you're attached to:
    137.   Serial.print("SSID: ");
    138.   Serial.println(WiFi.SSID());
    139.   // print your WiFi shield's IP address:
    140.   IPAddress ip = WiFi.localIP();
    141.   Serial.print("IP Address: ");
    142.   Serial.println(ip);
    143.   // print the received signal strength:
    144.   long rssi = WiFi.RSSI();
    145.   Serial.print("signal strength (RSSI):");
    146.   Serial.print(rssi);
    147.   Serial.println(" dBm");
    148. }
    复制代码
    程序源码: UDP.zip (9.03 KB, 下载次数: 0)

    评分

    参与人数 1 +20 收起 理由
    loveeeboard + 20

    查看全部评分

    回复

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-25 13:57 , Processed in 0.131199 second(s), 22 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.