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.
2 Answers 2
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);
-
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?BrainTrance– BrainTrance2020年07月07日 18:38:18 +00:00Commented Jul 7, 2020 at 18:38
-
-
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"BrainTrance– BrainTrance2020年07月07日 18:58:27 +00:00Commented Jul 7, 2020 at 18:58
-
Maybe it's the return value from Udp.read() actually. I haven't used the Yun for UDP before...Majenko– Majenko2020年07月07日 19:12:05 +00:00Commented Jul 7, 2020 at 19:12
-
And the packetSize is always printed as 1, that's weirdBrainTrance– BrainTrance2020年07月07日 20:12:58 +00:00Commented Jul 7, 2020 at 20:12
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);
}
}
Explore related questions
See similar questions with these tags.
udp_buffer[udp_received ] = '0円';
you send only 5 or 6 characters "test\r\n". the rest is random memory content