0

I am working on a machine with Arduino motors for rotation on yaw and pitch axis. Also I have a nodejs server implementation to send data to machine (list of coordinates). For this purpose I have implemented a TCPClient class in TCP_Client.js as following:

    var net = require('net');

/***********************************************************************************************************************
---------------------------------------------------Client TCP socket----------------------------------------------------
* *********************************************************************************************************************/

const port = 5303;
const host = 'localhost';

// creating a custom socket client and connecting it....
var client;
class TCPClient {

    constructor(host, port) {
        this.host = host;
        this.port = port;

        client = new net.Socket();

        client.connect({
            port: this.port,
            host: this.host
        });

        this.onConnect();
    }

    onConnect() {
        client.on('connect',function(){
            console.log('Client: connection established with server');

            console.log('---------client details -----------------');
            var address = client.address();
            var port = address.port;
            var family = address.family;
            var ipaddr = address.address;
            console.log('Client is listening at port' + port);
            console.log('Client ip :' + ipaddr);
            console.log('Client is IP4/IP6 : ' + family);

        });

        client.on('drain', function(){
            console.log("DRAIN");
        });

        this.setEncoding();

        client.on('data', function(data) {
            console.log("RECEIVED");
            client.end();
        });

        client.on('end', function(){
            console.log("END");
        });
    }

    setEncoding() {
        console.log("setEncoding");
        client.setEncoding('utf8');
    }

    onData(data) {
        console.log("DATA= ", data);
        client.write(data);
    }

    end() {
        client.end();
    }

    close() {
        client.close();
    }
}

//CREATE A TCP CLIENT OBJECT
exports.newTCPclient = function (host, port) {
    console.log("CONSTRUCT TCP CLIENT");
    var tc = new TCPClient(host, port);
    return tc;
}

In server.js I initialize a global TCPClient as follows:

const port = 5033;
const host = '192.168.100.20';
var tcp_client = tcpclient.newTCPclient(host, port);

I send the coordinates merged in a text variable named coords as follows:

tcp_client.onData(coords);

Everytime I move the arduino motors I repeat this but after a short while I encounter this error:

events.js:174
throw er; //Unhandled 'error' event

Error: read ECONNRESET
at TCP.onStreamRead(internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process. tckCallback (internal/process/next_tick.js:63:19)

I am not very fluent with connections. Is it related to the TCP client class? If so how can I fix it?

Thank you for all the help.

dramaticlook
  • 653
  • 1
  • 12
  • 39

1 Answers1

0

ECONNRESET means the other side of the TCP conversation abruptly closed its end of the connection. Make sure it's closing the connection properly at the receiving end. use --abort-on-uncaught-exception node option when running code, it will show you verbose report

Sandeep Patel
  • 4,815
  • 3
  • 21
  • 37
  • Thank you very much for the answer. What do you think is the proper way to handle this kind of error? I just want my server to be alive after the error. – dramaticlook Jan 15 '20 at 17:04
  • do you have the option to debug Arduino side code? As the problem seems to be the Arduino side. Make sure you are reading data properly and closing the connection properly. Also, tell us more about the issue. – Sandeep Patel Jan 16 '20 at 06:18