Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
456 stars 169 forks source link

SimpleServer.js example not working - listeners not called #337

Closed TimT1919 closed 1 month ago

TimT1919 commented 2 months ago

I was trying to run your SimpleServer example. (examples/javascript/tcp/SimpleServer.ts) I was trying to use the readHoldingRegisters listener defined in the example. The issue is that with the defined options for the server these listeners get never called. This is the case for:

Reason:

When the server.holding option is defined the event gets not triggered.

if (!this._server.coils) {
    debug('no coils buffer on server, trying readCoils handler');
    this._server.emit('readCoils', request, cb);
    return;
}

see modbus-server-response-handler.js:55

Solution/Workaround:

You can define the server options to undefined. Then the events will be emitted and the listeners get called. Can you plaese change this in the example or at least add a comment to the example?

const server = new modbus.server.TCP(netServer, {
  coil: undefined,
  holding: undefined
})
stefanpoeter commented 1 month ago

Hi @TimT1919 , thanks for the hint and the workaround. Since this is an open source project I welcome every Merge Request developers are submitting. It would be great if you provide one for this case too.