Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
465 stars 174 forks source link

ModbusTCPServer #230

Closed vilelam closed 5 years ago

vilelam commented 5 years ago

Hello, I'm creating a Modbus TCP Server with the following code and from a client, I'm connecting to the server and calling the readHoldingRegisters method; however, it seems that ModbusTCPServer is not yet developed to reply to readHoldingRegisters. Is that correct? Maybe I'm doing something wrong here, but it seems that the Server is never emitting the 'readHoldingRegisters' event.

'use strict'

let net = require('net')
let modbus = require('jsmodbus')
let netServer = new net.Server()
let holding = Buffer.alloc(10000)
let server = new modbus.server.TCP(netServer, {
  holding: holding
})

server.on('connection', function (client) {
  console.log('New Connection')
})

server.on('readCoils', function (request, response, send) {
  /* Implement your own */

  response.body.coils[0] = true
  response.body.coils[1] = false

  send(response)
})

server.on('readHoldingRegisters', function (request, response, send) {

  /* Implement your own */

})

server.on('preWriteSingleRegister', function (value, address) {
  console.log('Write Single Register')
  console.log('Original {register, value}: {', address, ',', server.holding.readUInt16BE(address), '}')
})

server.on('WriteSingleRegister', function (value, address) {
  console.log('New {register, value}: {', address, ',', server.holding.readUInt16BE(address), '}')
})

server.on('writeMultipleCoils', function (value) {
  console.log('Write multiple coils - Existing: ', value)
})

server.on('postWriteMultipleCoils', function (value) {
  console.log('Write multiple coils - Complete: ', value)
})

/* server.on('writeMultipleRegisters', function (value) {
  console.log('Write multiple registers - Existing: ', value)
}) */

server.on('postWriteMultipleRegisters', function (value) {
  console.log('Write multiple registers - Complete: ', holding.readUInt16BE(0))
})

server.on('connection', function (client) {

  /* work with the modbus tcp client */

})

server.coils.writeUInt16BE(0x0000, 0)
server.coils.writeUInt16BE(0x0000, 2)
server.coils.writeUInt16BE(0x0000, 4)
server.coils.writeUInt16BE(0x0000, 6)

server.discrete.writeUInt16BE(0x5678, 0)

server.holding.writeUInt16BE(0x0000, 0)
server.holding.writeUInt16BE(0x0000, 2)

server.input.writeUInt16BE(0xff00, 0)
server.input.writeUInt16BE(0xff00, 2)

console.log(process.argv[2])
netServer.listen(process.argv[2] || 8502)
stefanpoeter commented 5 years ago

Hey @vilelam

you've started the server with the holding Buffer


let holding = Buffer.alloc(10000)
let server = new modbus.server.TCP(netServer, {
  holding: holding
})

In that case jsmodbus fires a 'preReadHoldingRegisters' and a 'postReadHoldingRegisters' event to manipulate the holding buffers up front or do something after the request has been handled.

If you start the modbus server without a holding buffer the readHoldingRegisters event will be fired.

vilelam commented 5 years ago

Thanks, @stefanpoeter.

Even if we start the server without the holding Buffer, it seems that the readHoldingRegisters event will not be fired because in the Modbuserver class constructor method we have the following code. Can I create a pull request with some minor contribution to the code documentation? I would like to be engaged in an open source project and I feel that I found a topic that I like :)

I'm working with ABB in Latin America and Leading our Software Presales Practice in both Central and South America. Not working as a programmer but passioned about Nodejs and IoT.

class ModbusServer extends EventEmitter {
  constructor (options) {
    super()

    this._options = options || {}

    this._coils = this._options.coils || Buffer.alloc(1024)
    this._discrete = this._options.discrete || Buffer.alloc(1024)
    this._holding = this._options.holding || Buffer.alloc(1024)
    this._input = this._options.input || Buffer.alloc(1024)
  }
stefanpoeter commented 5 years ago

Of course you can. But keep in mind to be compatible to the current API specification and of course write tests.

vilelam commented 5 years ago

is there any guide I can follow? I will probably need to learn and gain some confidence first. I will try to start with some simple contribution, probably to the README documentation file

stefanpoeter commented 5 years ago

You can find a lot on the GitHub Help Site. Regarding the tests, just inspect the existing ones. Pull Requests do not hurt since I am the one who decides to merge them.

vilelam commented 5 years ago

thanks, @stefanpoeter .