Go Down

Topic: UIPEthernet TCp Client sends data incorrectly if reading data at the same time (Read 474 times) previous topic - next topic

Eiroth

Basically, I'm trying to make a simple TCP client that can send and receive data from a Node.js server with high reliability.

This current example is connected to a simple echoserver and it works very well, until I uncomment the bottom part which is used to read the data sent back by the server, at which point the individual chars of the string start arriving seperately from each other in a random fashion.

I'm currently running the UIPEthernet library on an Arduino Nano using a ENCJ28J60 module.

I've done some testing, and I've so far considered the following solutions, but I don't know whether any of them will have any effect:

1. Switch to the Ethercard library
2.Try different Ethernet modules (Maybe a Nano shield)
3. Accept defeat

Here's the Arduino code:

Code: [Select]

#include <UIPEthernet.h>
#include <utility/logging.h>

EthernetClient client;


char data[] = "Hello server! Love, client";
int dlength = sizeof(data) / sizeof(char);

void setup() {
uint8_t mac[6] = {0x3E, 0x03, 0x44, 0x03, 0x22, 0x05};
  Ethernet.begin(mac);

}

void loop(){
if (client.connect(IPAddress(0, 0, 0, 0), 10000)) //Replace with servers IP-address
  {
while(!client){
    ; // wait until there is a client connected to proceed
  }

while (client.connected()) {
      delay(1000);

for (int o = 0; o < dlength-1; o++) {
        client.print(data[o]);
      }
client.println();
     client.flush();
      delay(100);


/*int size; //If uncommented, everything goes haywire
      while ((size = client.available()) > 0)
      {
        uint8_t* msg = (uint8_t*)malloc(size);
        size = client.read(msg, size);
        Serial.write(msg, size);
        free(msg);
      }*/
      //Ethernet.maintain();
      /*if (Serial.available() > 0) {
        serialFlush();
        reset = true;
        break;
      }*/


}


}
}



And here's the Node.js code:

Code: [Select]

var net = require('net');


var server = net.createServer(function (socket) {
    socket.write('I am an echoserver\r\n');
    socket.on('data', function (data) {
        console.log("Incoming data:");
        console.log(data.toString());
        socket.write(data);
        console.log("And that is all she wrote");
  

    });
    socket.on('error', errorhandler);
});

server.listen(10000);

server.on('listening', function () {
    console.log("Server listening!");

});


server.on('error', errorhandler);






function errorhandler(err) {
    console.log("You have an error!");
    console.log(err);
    console.log("Probably just an unexpected disconnect, carry on...");
}


pylon

Quote
This current example is connected to a simple echoserver and it works very well, until I uncomment the bottom part which is used to read the data sent back by the server, at which point the individual chars of the string start arriving seperately from each other in a random fashion.
What does that mean? Is just the timing random or is the order of the bytes changed?

Can you post an example output?

Eiroth

The order of the bytes is generally maintained, it just arrives in differently sized chunks, like this:

Hell
o
Server
!
Love,
cli
en
t

The socket.on('data') event is fired once for every one of the above lines and the data becomes illegible, although it is technically conserved

pylon

This is technically OK, it's up to the UIPEthernet library with how many bytes it will send a packet. Keep in mind that a UNO has 2kB of RAM so sending larger chunks would waste a lot of memory. The WizNet library sends every byte in a separate packet.

Eiroth

I'm just wondering if there's a way to structure the data receive section of the code to avoid splitting the message into separate packages, since it works perfectly if the data receive section is commented out...

pylon

Quote
I'm just wondering if there's a way to structure the data receive section of the code to avoid splitting the message into separate packages, since it works perfectly if the data receive section is commented out...
Was the above output on the server or on the Arduino? What does that mean "it works perfectly"? In that case the transfer is only one way. It might be that the message is always split, without the reception code it's simply faster sent so that the OS on the PC reassembles the packages before the application gets the content. Did you check that with a network analyzer?

What's the problem if the message is split into multiple packages? Even if you find a way that the example message is not split there will be a limit where it must be split. So explain where the problem is.

Go Up