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.