I'm writing this app and it is getting really messy, as such I am trying to encapsulate the various parts of the program into modules, but I'm finding some issues.
I have a class SerialPortController, that deals with the serial port, at the moment this class is only exposing two methods dataSend() and dataReceived().
I then have a module which controls the application, implemented as a IIFE which within it creates a new serialPortController object. At the moment, every time data received is called inside the serialport.on(data) I am just printing the data to the console.
everything works fine locally, the AppController creates the new serialPortController object and data is received no problem, but what I really want is to fire a function in my main AppController module everytime I receive data. All the methods I tried don't work and give me all sort of errors. Mainly all related to the fact that the new object doesn't know anything about the object that just created it. This needs some sort of callback pattern but I just can't get it to work. Does this needs to be turned into an event or something like that?
I'm sure this is a common problem but I am struggling to find any info on how to implementing it. Any help would be appreciated.
This is a minimal version of my code:
function SerialPortController(serialportSettings){
var portSettings = {
portName: "COM5",
baudRate: 115200,
stopBits: 1,
dataBits: 8,
parity: "none",
packetStart: '(',
packetEnd: ')'
};
//var data;
const SerialPort = require('serialport');
const DelimiterParser = SerialPort.parsers.Delimiter;
portSettings.portName = serialportSettings.portName;
portSettings.baudRate = serialportSettings.baudRate;
portSettings.stopBits = serialportSettings.stopBits;
portSettings.dataBits = serialportSettings.dataBits;
portSettings.parity = serialportSettings.parity;
portSettings.packetStart = serialportSettings.packetStart;
portSettings.packetEnd = serialportSettings.packetEnd;
const myPort = new SerialPort(portSettings.portName, {
baudRate: portSettings.baudRate,
stopBits: portSettings.stopBits,
dataBits: portSettings.dataBits,
parity: portSettings.parity
});
const delimiterParser = myPort.pipe(new DelimiterParser({delimiter: ')'}));
myPort.on('open', ()=>{
console.log("Serial Port is Open");
});
delimiterParser.on('data', (data)=>{
dataReceived(data);
//console.log(data.toString());
});
myPort.on('err', (err)=>{
console.log(err.message);
});
/* Just to expose data to the outside Modules */
var dataReceived = (data)=>{
console.log(data.toString());
};
/* writes data to the serial port. needs to report errors */
function sendData(data) {
myPort.write(data, onError);
};
return {
sendData: sendData,
dataReceived: dataReceived
};
};
var AppController = ((UICtrl)=>{
var portSettings = {
portName: "COM5",
baudRate: 115200,
stopBits: 1,
dataBits: 8,
parity: "none",
packetStart: '(',
packetEnd: ')'
}
var serialPortController = new SerialPortController(portSettings);
})(UIController);
var UIController =(()=>{
var writeDataToText = (data)=>{
var text1 = document.getElementById('text1');
text1.value = data;
};
return{
writeDataToText: writeDataToText
}
})();
Why not just provide a simply callback while creating the SerialPortController object?
// Do this in your AppController:
var serialPortController = new SerialPortController(portSettings, function(data) {
console.log("data received:", data);
});
// Change your SerialPortController the following way:
function SerialPortController(serialportSettings, dataCallback) {
...
...
...
var dataReceived = data => {
console.log(data.toString());
dataCallback(data);
};
...
...
...
}