A Node.js package developed by Pylogix a software development company https://pylogix.com/ specializing in PLC systems, for handling Logix or Micro800 PLCs.
Features
- Promise-based responses using the bluebird package
- Connection management for reading and writing tags using generic-pool
- Events for connecting, connect errors, disconnections, and closing, as well as discovering devices
- Arduino mode
- Autoclosing of EIP Socket sessions
- Option to enable or disable identity on connection with ignoreIdentity parameter
- Support for reading and writing strings and arrays
- Support for TypeScript
- Support for Micro800
- API Documentation
Package Installation
Copynpm install pylogix-plc
or
Copyyarn add pylogix-plc
Protocol
- EtherNet/IP
Code Examples
Connecting, Reading, and Writing Tags with Events
Copy`const PLC = require("pylogix-plc").default;
const comm = new PLC("192.168.100.9");
comm.on("connect", () => { console.log("Successfully connected to the PLC."); comm .write("TEST_USINT", 127) .then(() => { return comm.read("TEST_USINT").then(console.log); }) .catch(console.error); });
comm.on("connect_error", e => { console.log("Failed to connect to the PLC", e); });
comm.on("disconnect", reason => { console.log("PLC disconnected:", reason); });`
Discovering Devices
Copy`const PLC = require("pylogix-plc").default;
PLC.discover().then(devices => { console.log("Discoverd Devices:", devices); });`
Arduino Mode
Copy`comm .digitalRead(0) .then(result => { console.timeEnd("reading digital"); console.log("Value of:", result.valueOf(), "Tag Name:", 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 writing to pin", e)); });`
Default Options
CopyPLC.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 }
- ArduinoMode only working with Micro800 enable, to working with other PLC, yout must be create custom pinMapping JSON and replacePin function:
Copy``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; }``
Additional Examples
NOTE:
Maximum Connection Size:
-
Packets have a ~500 byte limit, so be cautious not to exceed that in reads. It's difficult to anticipate how many bytes reads will take up, as the send packet will depend on the length of the tag name and the reply will depend on the data type. Strings are a lot longer than DINTs, for example.
-
Micro800 has limited CIP protocol. Check examples to check the functioning of the alternate functions.
Projects That Inspired This Package
TODO
- Unit testing
License
This project is licensed under the MIT License - see the LICENSE.txt file for details.