【问题标题】:Arduino sending inconsistent UDP messagesArduino 发送不一致的 UDP 消息
【发布时间】:2018-06-30 13:54:19
【问题描述】:

我正在尝试使用 UDP 和 Arduino UNO + 以太网板与 PC 进行通信,但无法正常进行。

事实:

  • Arduino 代码有 2 个块:第一个块每 5 秒向 PC 发送一条恒定消息。第二个块已作为 ECHO 实现(返回接收的内容)
  • UDP 已正确初始化
  • 一开始,Arduino 开始发送消息“Hello PC!”每 5 秒一次,但 UDP.beginPacket 不返回 1(正如它正常工作时应该做的那样)。
  • 在 PC 端,我有 wireshark 和 PacketSender 来检查 tx/rx UDP 通信。
  • Wireshart 在重置后不会检测到任何来自 Arduino 的传入包。
  • 一个 UDP 包“你好 arduino!”通过 PacketSender 从 PC 发送到 Arduino。这个包装在 Arduino 中被正确接收并发送回 PC。 Wireshark 识别来自 PC 的 tx UDP 数据包和来自 Arduino 的相同 rx UDP 数据包。这可以正常工作。
  • Buuuuuuuuuttttttt...... 然后,在从 PC 发送 UDP 消息并接收 ECHO 后,Arduino 每隔 5 秒发送的消息开始到达 PC。
  • 在 ECHO 之后在 PC 上收到的第一条消息有 21 个字节(虽然发送的消息应该有 11 个字节)。收到的消息是“C!hello hellhello PC!”,因此存在某种带有“hello PC!”的缓冲区垃圾。最后。
  • 奇怪的是,在这之后,PC每5秒接收一个UDP数据包,每次长9个字节(你好PC!是9个字节的消息),就像从Arduino收到的第一条消息一样,在结束时消息你可以找到“你好PC!”。这是收到的第 9 条消息“PC!hello PC!!lhello PC!45hello!llo!helhello PC!hellhello!ohello PC!hello Phello PC!”的示例(95 字节长度)。
  • 我使用标准的 Arduino 以太网库,所以我不知道发生了什么以及为什么在第一条消息到达 Arduino 之前它不发送任何内容(并且不知道为什么每次发送它都会增加)。
  • 我用 Raspbian 测试了 Arduino 和 Raspberry,以确保问题不在 PC 端,并且 Raspberry 收到了同样不稳定的 UDP 行为。

Arduino 代码:

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

byte mac\[\] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x3D};
IPAddress ip(192, 168, 1, 6); //IP of the arduino ethernet board
char link_IP\[\] = {192, 168, 1, 3}; //IP of the PC
unsigned int localPort = 51115;      // local port to listen on


char message\[\] = "hello PC!"; //message intended to sended from the arduino
char packetBuffer\[UDP_TX_PACKET_MAX_SIZE\]; // UDP_TX_PACKET_MAX_SIZE = 24
unsigned long millis_before = millis(); //variable for delaying 5 seconds the message
EthernetUDP Udp;

void setup() 
{
  Serial.begin(9600); //serial com for debugging
  Ethernet.begin(mac, ip); //starting Ethernet
  if (Udp.begin(localPort)==1) Serial.println("port UDP is open"); //starting UDP on 51115
  else while(1); //if UDP not open, infinte bucle
}

void loop() 
{
  if (millis_before + 5000 < millis()) //loop for waiting 5 seconds between messages
  {
    if (Udp.beginPacket(link_IP, 51115)==1) Serial.print("TX ready - "); // UDP packet to PC port 51115, if beginPacket returns 1 is OK
    Serial.print("Lenght:");
    Serial.print(Udp.write(message, 9)); //returns the length of the message
    if (Udp.endPacket()==1) Serial.println(" - Sent!"); //if endPacket returns 1, is OK
    millis_before = millis(); //reset timer
  }
  delay(10);
  int packetSize = Udp.parsePacket(); //check size of UDP packet 
  if (packetSize) 
  {
    IPAddress remote = Udp.remoteIP(); //save remote IP
    for (int i = 0; i < 4; i++)  //bucle for printing the remote IP
    { 
      Serial.print(remote\[i\], DEC);
      if (i < 3) Serial.print(".");
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort()); //printing the remote port
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); // read the packet into packetBufffer

    // send a echo to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(packetBuffer, packetSize);
    Udp.endPacket();
  }
}

在这里您可以找到在向 Arduino 发送第一条消息并接收 ECHO 后的 PacketSender 和 Wireshark 捕获:

wireshark capture

packet sender capture

感谢任何帮助。在此先感谢

【问题讨论】:

    标签: arduino udp


    【解决方案1】:

    解决了。问题在于传送数据包的 IP 的 char 数组格式。我将其定义为 4 char 数组,但它应该定义为 text char 数组:

    char link_IP[] = "192.168.1.3"; //correct !!
    
    char link_IP[] = {192, 168, 1, 3}; // Wrong
    

    【讨论】:

      猜你喜欢
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多