title: How to monitor and console log all serial port modbus traffic?
body: <p>I have been trying to use one node.js application to communicate to a PLC through the MODBUS RTU protocol. I have successfully communicated with the PLC through the MODBUS TCP protocol. However, when I revised the same code to MODBUS RTU, I have been experiencing difficulties to make the application and PLC find each other.</p>
  
  <p>the package I have been using is:
  serialport
  jsmodbus</p>
  
  <p>Currently, I have tried to use different Modbus emulators to communicate with the PLC. One of them which was called MODBUS SIM 32 worked, which proved the connection and PLC setup is just fine. However, when I aligned the settings of my node.js application with the PLC, it just ended time out error. I have tried with the Modbus TCP option from the same package, and it worked just fine. But when I switched to the RTU option from the same package, I always got the timeout except when the PLC unit was rebooted. Then I found out that it would receive something through the .data function, which is as far as I got.</p>
  
  <p>The code I have been using for the MODBUS RTU communication is as following:</p>
  
  <pre><code>const Modbus = require('jsmodbus');
  const SerialPort = require('serialport');
  
  function connectModbusRtu(baudRate,COMport, parity,dataBits,stopBits) {
    const options = {
      baudRate:baudRate,
      dataBits:dataBits,
      stopBits:stopBits,
      Parity:parity
    };
    const modbusPort = new SerialPort(COMport, options);
    const client = new Modbus.client.RTU(modbusPort, 1);
  
    modbusPort.on('open', function(){
  
      if(modbusPort.isOpen){
          console.log('Connection through modbus established.');
  
          client.writeSingleRegister(1,12)
          .then(function(resp){
              console.log('I wrote the answer.');
              console.log(resp);
          }).catch(function(err){
              console.log('I had it wrong.');
              console.log(err);
          });
      }else{
          console.log('COMport open failure.');
      }
  
    })
  
    modbusPort.on('data', function () {
      console.log('------------Data------------');
      console.log(arguments);
  })
  
    modbusPort.on('close',function() {
      console.log('------------CLose------------');
      console.log(arguments);
      modbusPort.open();
    });
  
    modbusPort.on('error', function() {
      console.log('------------General Error------------');
      console.error;
    });
  
    return client;
  }
  
  module.exports = connectModbusRtu;
  
  </code></pre>
  
  <p>and the following code is what I have been using for the Modbus TCP:</p>
  
  <pre><code>const Modbus = require('jsmodbus');
  const net = require('net');
  
  function connectModbusTcp(host, port) {
    const modbusSocket = new net.Socket();
    const client = new Modbus.client.TCP(modbusSocket);
    const options = {
      host,
      port
    };
  
    modbusSocket.on('connect', () =&gt;
      console.log('Connection through modbus established')
    );
  
    modbusSocket.on('error', console.error);
  
    modbusSocket.connect(options);
  
    return client;
  }
  
  module.exports = connectModbusTcp;
  
  </code></pre>
  
  <p>The main goal is after the main script called one of the above js scripts, it shall be able to pass a correct Modbus instruction through whichever the channel (RTU/serial port, or TCP/ethernet) to the PLC unit. Please see following for the main script:</p>
  
  <pre><code>const express = require('express');
  const connectModbusTcp = require('../../modbus/connectModbusTcp');
  const connectModbusRtu = require('../../modbus/connectModbusRtu');
  
  const router = express.Router();
  
  router.post('/', (req, res) =&gt; {
    console.log('Establishing modbus connection');
    const client = connectModbusRtu(9600,'COM4','NONE',8,1);
    //const client = connectModbusTcp('192.168.1.124', 502);
    req.app.set('modbusClient', client);
    res.json({ msg: 'Establishing modbus connection' });
  });
  
  module.exports = router;
  
  </code></pre>
  
  <p>However, I could get it flawless executed through the TCP code set, but not the serial port. Just wondering if anyone has experience in this arena or have an educated guess to start from.</p>
  
  <p>Currently, I am at a spot that I would literally try any opinion.</p>
  
  <p>Thanks!</p>
  
  <p>Roland</p>