首页 文章

Arduino发送不一致的UDP消息

提问于
浏览
1

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

事实:

  • Arduino代码有2个块:第一个块每5秒向PC发送一个常量消息 . 第二个块已作为ECHO实现(返回收到的内容)

  • UDP已正确初始化

  • 开始时,Arduino开始每隔5秒发送一条消息"Hello PC!",但UDP.beginPacket不会返回1(因为它应该在它正常工作时执行) .
    在PC端

  • 我有wireshark和PacketSender来检查tx / rx UDP通信 .

  • Wireshart在重置后没有检测到Arduino的任何传入包 .

  • UDP包"Hello arduino!"通过PacketSender从PC发送到Arduino . 这个打包在Arduino中正确接收并发送回PC . Wireshark识别来自PC的tx UDP数据包和来自Arduino的相同rx UDP数据包 . 这工作正常 .

  • Buuuuuuuuttttttt ......然后,在从PC发送UDP消息并接收到ECHO后,来自Arduino的消息每5秒开始到达PC .

  • 在ECHO之后在PC上接收的第一条消息有21个字节(尽管发送的消息应该有11个字节) . 收到的消息是“ C!hello hellhello PC! ", so there is some kind of buffer trash with "你好PC!”在末尾 .

  • 奇怪的是,在此之后,每5秒钟PC收到一个UDP数据包,每次增加9个字节(你好PC!是9个字节的消息),就像从Arduino收到的第一个消息一样,在消息的末尾你可以找到"hello PC!" . 以下是收到的第9条消息“ PC!hello PC!!lhello PC!45hello!llo!helhello PC!hellhello!ohello PC!hello Phello PC! ”(长度为95字节)的示例 .

  • 我使用标准的Arduino以太网库,所以我不发送任何东西,直到第一条消息到达Arduino(并且不知道为什么每次发送它都会增加) .

  • 我用Raspberry用Raspberry测试了Arduino,以确保问题不在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

我赞成任何帮助 . 提前致谢

1 回答

  • 0

    解决了 . 问题在于传递数据包的IP的字符数组格式 . 我将它定义为4字符数组,但应将其定义为文本字符数组:

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

相关问题