javascript和封装中的回调模式实现

时间:2018-03-16 08:14:51

标签: javascript node.js encapsulation node-serialport

我正在编写这个应用程序并且它变得非常混乱,因此我试图将程序的各个部分封装到模块中,但我发现了一些问题。

我有一个SerialPortController类,它处理串口,目前这个类只暴露了两个方法dataSend()和dataReceived()。

然后我有一个控制应用程序的模块,实现为IIFE,在其中创建一个新的serialPortController对象。目前,每次收到的数据都是在serialport.on(数据)内部调用的,我只是将数据打印到控制台。

本地一切正常,AppController创建新的serialPortController对象,接收数据没问题,但我真正想要的是每次收到数据时在我的主AppController模块中触发一个函数。 我尝试的所有方法都不起作用,并给我各种错误。主要是因为新对象对刚创建它的对象一无所知。 这需要某种回调模式,但我无法让它工作。这是否需要变成一个事件或类似的东西?

我确信这是一个常见的问题,但我很难找到有关如何实施它的任何信息。任何帮助将不胜感激。

这是我的代码的最小版本:

    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
  }
})();

1 个答案:

答案 0 :(得分:0)

为什么不在创建SerialPortController对象时提供简单的回调?

// 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);
  };
  ...
  ...
  ...
}