Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UDP read() doesn't work in conjunction with the Arduino String library #19

Open
sstaub opened this issue Mar 20, 2021 · 0 comments
Open

Comments

@sstaub
Copy link

sstaub commented Mar 20, 2021

Following code doesn't work with the Native Ethernet library.
The code works with STM32duino with LWIP and with the WIZNet library

#include "Arduino.h"
#include <TeensyID.h>
#include <NativeEthernet.h>
#include <NativeEthernetUdp.h>

uint8_t mac[6];
IPAddress localIP(10, 101, 1, 201);
IPAddress subnet(255, 255, 0, 0);
uint16_t localPort = 8001;
IPAddress remoteIP(10, 101, 1, 100);
uint16_t remotePort = 8000;

EthernetUDP udp;

void setup() {
	teensyMAC(mac);
	Serial.begin(9600);
	Ethernet.begin(mac, localIP);
	udp.begin(localPort);
	}

void loop() {
	String message;
	int size;
        size = udp.parsePacket();
	if (size > 0) {
		// Fill the msg with all of the available bytes
	                while (size--) {
			message += (char)(udp.read());
			}
                Serial.println(message);
		message = String();
		}
	}

The application stop working when reaching message += (char)(udp.read());
If using the standard C++ string library it works as expected. Also when using a char buffer array.
This example works

#include "Arduino.h"
#include <TeensyID.h>
#include <NativeEthernet.h>
#include <NativeEthernetUdp.h>
#include <string>

using namespace std;

uint8_t mac[6];
IPAddress localIP(10, 101, 1, 201);
IPAddress subnet(255, 255, 0, 0);
uint16_t localPort = 8001;
IPAddress remoteIP(10, 101, 1, 100);
uint16_t remotePort = 8000;

EthernetUDP udp;

void setup() {
	teensyMAC(mac);
	Serial.begin(9600);
	Ethernet.begin(mac, localIP);
	udp.begin(localPort);
	}

void loop() {
	string message;
	int size;
        size = udp.parsePacket();
	if (size > 0) {
		// Fill the msg with all of the available bytes
	                while (size--) {
			message += (char)(udp.read());
			}
                Serial.println(message.c_str());
		message.erase();
		}
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant