0.0.2 • Published 7 years ago

modbus-tcp-client v0.0.2

Weekly downloads
6
License
ISC
Repository
github
Last release
7 years ago

modbus-tcp-client

ModbusClient

ModbusClient manages connection and fetches data based on provided layout of modbus regusters and coils.

const ModbusClient = require('modbus-tcp-client');

const layout = ModbusClient.ModbusLayout.fromFile('./layout.json');
const host = 'localhost';
const port = 502;

const client = new ModbusClient(host, port, layout);

client.open();

client.on('open', () => client.update());
client.on('close', () => console.log('client closed'));
client.on('update', (layout) => console.log(layout));
client.on('error', (err) => console.error(err));

ModbusLayout

ModbusLayout class describes location and type of data stored on modbus registers and coils.

coils and discrete arrays should consist of objects containing field address. descrete and holding arrays should consist of objects containing fields address and type (available types are listed below). If type is omitted, uint16 is assumed.

In addition all objects can contain props property which will be attached to ModbusCoil and ModbusRegister objects created by ModbusLayout.

const ModbusLayout = require('modbus-tcp-client').ModbusLayout;

const layout1 = ModbusLayout.fromFile('./layout.json');

const layout2 = new ModbusLayout({
  coils: [ { address: 0 }, { address: 1 } ],
  discrete: [ { address: 10000 }, { address: 10001 } ],
  input: [ { address: 30000, type: 'uint16' } ],
  holding: [ { address: 40000, type: 'uint32' } ],
});

Example modbus layout file:

{
  "coils": [
    {
      "address": 0,
      "props": {
        "name": "Coil 1",
      }
    }, {
      "address": 1,
      "props": {
        "name": "Coil 2",
        "description": "Description of second coil",
      }
    }
  ],

  "discrete": [
    {
      "address": 10000,
      "props": {
        "name": "Discrete 1",
      }
    }, {
      "address": 10001,
    }
  ],

  "input": [
    {
      "address": 30000,
      "type": "string16",
      "props": {
        "name": "Name",
      }
    }, {
      "address": 30008,
      "type": "uint32",
      "props": {
        "name": "Age",
      }
    }
  ],

  "holding": [
    {
      "address": 40000,
      "type": "uint16",
      "props": {
        "name": "IP",
      }
    }
  ]
}

Available types

Following register types are supported:

identifierdescription
int1616 bit integer (2 bytes / 1 word)
int3232 bit integer (4 bytes / 2 words)
int6464 bit integer (8 bytes / 4 words)
uint1616 bit unsigned integer (2 bytes / 1 word)
uint3232 bit unsigned integer (4 bytes / 2 words)
uint6464 bit unsigned integer (8 bytes / 4 words)
floatsingle precision floating point number (4 bytes / 2 words)
doubledouble precision floating point number (8 bytes / 4 words)
stringXstring of specified byte length, where X is the number of bytes