msvargas / node-logix

Node.js package to read/write data from Allen Bradley Compact/Control Logix PLC's
MIT License
13 stars 3 forks source link

node-logix

Nodejs package to handle PLC as Micro800 of Allen Bradley or Logix

Features

Install package

  npm install node-logix

or

  yarn add node-logix

Protocol

Example with events, read and write tags

const PLC = require("node-logix").default;

const comm = new PLC("192.168.100.9");

comm.on("connect", () => {
  console.log("PLC connected successful! ");
  comm
    .write("TEST_USINT", 127)
    .then(() => {
      return comm.read("TEST_USINT").then(console.log);
    })
    .catch(console.error);
});

comm.on("connect_error", e => {
  console.log("Fail to connect PLC", e);
});

comm.on("disconnect", reason => {
  console.log("PLC disconnected reason:", reason);
});

Example: Discover devices

Find devices using dgram socket

const PLC = require("node-logix").default;

PLC.discover().then(devices => {
  console.log("Devices:", devices);
});

Example: Arduino mode

comm
  .digitalRead(0)
  .then(result => {
    console.timeEnd("reading digital");
    console.log("value of:", result.valueOf(), "tagName:", result.tagName);
  })
  .catch(console.error);
[0, 1, 2, 3, 4, 5, 6].forEach(p => {
  comm
    .digitalWrite(p, false)
    .then(console.log)
    .catch(e => console.error("error write", e));
});

Default Options

PLC.defaultOptions = {
  allowHalfOpen: true, // Socket option nodejs, keep open TCP socket
  Micro800: false,     // use path for Micro800
  port: 44818,        // default port EIP
  connectTimeout: 3000,
  arduinoMode: true,  // Enable Arduino functions only Micro800
  pool: { // options generic-pool
    min: 0,
    max: 3,
    Promise : Bluebird, //bluebird
    priorityRange: 2,
    fifo: false,
    testOnBorrow: true,
    evictionRunIntervalMillis: 17000,
    idleTimeoutMillis: 30000
  }
comm.pinMapping = {
  digital: {
    output: "_IO_EM_DO_{dd}",
    input: "_IO_EM_DI_{dd}"
  },
  analog: {
    input: "_IO_EM_AI_{dd}",
    output: "_IO_EM_AO_{dd}"
  }
};
/**
 * @description replace pin mapping
 * @param {String} str
 * @param {Number} pin
 * @param {String}
 */
function _replacePin(str = "", pin) {
  if (typeof str !== "string")
    throw new TypeError("Pin must be a string not a " + typeof str);
  if (typeof pin === "string" && !/\d{1,}/.test(pin))
    throw new TypeError("Pin must has number to assing pin value: " + pin);
  const match = str.match(/{(d+)}/);
  if (match === null)
    throw new PinMappingError(`Replace: ${str} no match with {d} or {dd}`);
  if (match.index > 0) {
    return str.replace(match[0], String(pin).padStart(match[1].length, "0"));
  }
  return str;
}

More Examples

NOTE:

Connection size:

Inpired by projects

TODO

License

This project is licensed under the MIT License - see the LICENCE file for details