- 
                Notifications
    You must be signed in to change notification settings 
- Fork 34
Description
Hi:
I have a strange behaviour of EthernetClient client.write("somestring") returning zero bytes after a random number of calls.
My application is running on a Nano Every and a Enc28J60 shield (lots of code ommited for clarity):
#include <Arduino.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress clientIP(192, 168, 1, 177);
EthernetClient client;
int serverPort = 2323;
IPAddress serverIP(192, 168, 1, 80);
void setup() {
	if (initEth()) {
		isConnected = connectToFrame(serverIP, serverPort);
	}
	if (!isConnected) {
		Debug(msgNoEthLink);
               // FAULT
	}
}
long last_loop;
#define LOOP_PERIOD 50
void loop() {
	if (millis() > last_loop + LOOP_PERIOD) {
		last_loop = millis();
		reConnect(serverIP, serverPort); // in case the connection is lost
		readEncoder();  // when there is a change in the encoder value, it sends a message to the server
		readClient();     // receives then parses JSON from the server
		readPushBtns(); // when there is a change in the encoder value, it sends a message to the server
// HERE IS THE HACK WICH SOLVES MY PROBLEM
// BUT I DON'T KNOW WHY (!!?)
 		client.write((byte)0);
 
		}
	}
When a change is read in the encoder i send a message like this:
int sendCmd(const char *control, const char *param, int value) {
	int num = -1;
	int a, l;
	sprintf(strbuf, "{\"OP\":\"cmd\",\"Control\":\"%s\",\"%s\":%i}", control,
			param, value);
	num = client.println(strbuf);
        client.flush(); // TRIED THIS BUT NO CHANGE IN BEHAVIOUR
	sentCmd = true;
	return num;
}
After several sendCmd I get a return SMALLER than the length of the buffer sent (strbuf, which is a big enough char array) and the next one is zero, forever. I cannot recover from this fail. The client.write("string") returns after the 2000ms timeout without sending data.
I traced the error up to Enc28J60Network::allocBlock(UIP_SOCKET_DATALEN); which, apparently, returns NOBLOCK.
The only solution is to continuously client.write((byte)0); on the main loop, which is not really elegant and very brute-force.
I also tried to check client.availableForWrite() before sending a message, but no luck, it is always correct.
Any idea?