2

Arduino Yun is set to receive UDP packets. It receives those data with some gibberish.

How is all set up: Arduino Yun (acts as a server) is connected through Ethernet to a PC which is (through WiFi) connected to the router. Another PC (acts as client), is also connected to the router (through Wifi).

Client uses netcat to connect to Arduino Yun. I write something and press enter. The packets are received by the Arduino Yun with some gibberish. For example, I write "test" and press enter, but this is what I receive on the Arduino Yun: enter image description here

Sometimes gibberish characters are different, but always exist. This is what Wireshark monitors when I send the "test" packet (with netcat) to my Arduino Yun: enter image description here

It shows that indeed I send only "test", so the gibberish is created on the Arduino side, I suppose.

This is my sketch (not the entire code, but the important part):

void loop() {
  int udp_received = Udp.parsePacket();
  if (udp_received) {
    char udp_buffer[16];
    udp_buffer[15] = '\0';
    Udp.read(udp_buffer, 15);
    Serial.println(udp_buffer);
  }
}

I use BridgeUdp.h Any insights appreciated.

8
  • 1
    udp_received is count of characters received. (and nice progress since previous question)
    – Juraj
    Jul 7 '20 at 13:49
  • 1
    udp_buffer[udp_received ] = '\0'; you send only 5 or 6 characters "test\r\n". the rest is random memory content
    – Juraj
    Jul 7 '20 at 13:51
  • If I'm not mistaken, UDP packets, when printed as strings, might look like gibberish because they are not ASCII but binary in format. You would need to check the documentation and find out what the packet format is or, lacking documentation, you'd need to try and get some idea what the data actually represents.
    – S. Imp
    Jul 7 '20 at 14:10
  • Thanks Juraj, I'm moving forward slowly. but steadily. So you say that I read more bytes than I actually receive? Jul 7 '20 at 16:12
  • @S.Imp, if you send "test", you receive "test"
    – Juraj
    Jul 7 '20 at 17:25
1

You are receiving a small packet of about 5 or 6 characters (depending on your line ending). That is being received into a 16 byte buffer which has contents unknown. So the first 5 or 6 bytes of the array have valid data in it. The rest is whatever was already in there.

You need to use the return value of Udp.parsePacket() to tell you how many of the bytes in your buffer are actually valid received bytes and only print those.

The simplest way:

Serial.write(udp_buffer, udp_received);
5
  • int udp_received = Udp.parsePacket(); if (udp_received) { char udp_buffer[udp_received]; Udp.read(udp_buffer, udp_received); Serial.write(udp_buffer, udp_received); Is that what you mean? Jul 7 '20 at 18:38
  • Sounds about right
    – Majenko
    Jul 7 '20 at 18:48
  • This doesn't work as expected. It prints only the first character of each line. For example if I send "test", ENTER, "test", ENTER", "hello", ENTER, Serial monitor prints "tth" Jul 7 '20 at 18:58
  • Maybe it's the return value from Udp.read() actually. I haven't used the Yun for UDP before...
    – Majenko
    Jul 7 '20 at 19:12
  • And the packetSize is always printed as 1, that's weird Jul 7 '20 at 20:12
1

You send only 5 or 6 characters "test\r\n". the rest is random memory content.

The return value of parsePacket should be the count of received characters, but the implementation in BridgeUdp is wrong and returns always 1 on success.

You you can use BridgeUdp.available() to get the count.

void loop() {
  if (Udp.parsePacket()) {
    int udp_received = Udp.available();
    char udp_buffer[16];
    Udp.read(udp_buffer, udp_received);
    udp_buffer[udp_received] = '\0';
    Serial.println(udp_buffer);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.