UNPKG

n8n-nodes-modbus-fccomplete

Version:

n8n nodes for industrial automation with complete Modbus support (FC1-FC4) and intuitive data conversion with scaling options

232 lines 10.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Modbus = void 0; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("./GenericFunctions"); class Modbus { constructor() { this.description = { displayName: 'MODBUS', name: 'modbus', icon: 'file:modbus.svg', group: ['input'], version: 1, description: 'Read and write to MODBUS devices', eventTriggerDescription: '', defaults: { name: 'MODBUS', }, usableAsTool: true, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'modbusApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', options: [ { name: 'Read', value: 'read', }, { name: 'Write', value: 'write', }, ], default: 'read', noDataExpression: true, }, { displayName: 'Function Code', name: 'functionCode', type: 'options', displayOptions: { show: { operation: ['read'], }, }, options: [ { name: 'FC1 - Read Coils', value: 'FC1', description: 'Read discrete output coils (1-bit values)', }, { name: 'FC2 - Read Discrete Inputs', value: 'FC2', description: 'Read discrete input contacts (1-bit values)', }, { name: 'FC3 - Read Holding Registers', value: 'FC3', description: 'Read holding registers (16-bit values)', }, { name: 'FC4 - Read Input Registers', value: 'FC4', description: 'Read input registers (16-bit values)', }, ], default: 'FC3', noDataExpression: true, }, { displayName: 'Memory Address', name: 'memoryAddress', type: 'number', default: 1, description: 'The memory address to read from or write to', }, { displayName: 'Quantity', displayOptions: { show: { operation: ['read'], }, }, name: 'quantity', type: 'number', default: 1, description: 'The number of registers or coils to read', }, { displayName: 'Value', displayOptions: { show: { operation: ['write'], }, }, name: 'value', type: 'number', typeOptions: { maxValue: 32767, minValue: -32768, }, default: 1, description: 'The value to write to the holding register', }, { displayName: 'Unit ID', name: 'unitId', type: 'number', default: 1, description: 'The Modbus unit/slave ID (0-255). Use 0 for devices that require it.', typeOptions: { maxValue: 255, minValue: 0, }, }, ], }; } async execute() { let responseData = {}; const credentials = await this.getCredentials('modbusApi'); const client = await (0, GenericFunctions_1.createClient)(credentials); const memoryAddress = this.getNodeParameter('memoryAddress', 0); const operation = this.getNodeParameter('operation', 0); const unitId = this.getNodeParameter('unitId', 0); if (operation === 'read') { const functionCode = this.getNodeParameter('functionCode', 0); const quantity = this.getNodeParameter('quantity', 0); await new Promise((resolve) => { switch (functionCode) { case 'FC1': client.readCoils({ address: memoryAddress, quantity, extra: { unitId } }, (err, data) => { if (err) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'MODBUS FC1 Error: ' + err.message); } const returnData = { functionCode: 'FC1', address: memoryAddress, quantity: quantity, data: (data === null || data === void 0 ? void 0 : data.response.data) || [], }; responseData = returnData; resolve(responseData); }); break; case 'FC2': client.readDiscreteInputs({ address: memoryAddress, quantity, extra: { unitId } }, (err, data) => { if (err) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'MODBUS FC2 Error: ' + err.message); } const returnData = { functionCode: 'FC2', address: memoryAddress, quantity: quantity, data: (data === null || data === void 0 ? void 0 : data.response.data) || [], }; responseData = returnData; resolve(responseData); }); break; case 'FC3': client.readHoldingRegisters({ address: memoryAddress, quantity, extra: { unitId } }, (err, data) => { var _a; if (err) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'MODBUS FC3 Error: ' + err.message); } const returnData = { functionCode: 'FC3', address: memoryAddress, quantity: quantity, data: (_a = data === null || data === void 0 ? void 0 : data.response.data) === null || _a === void 0 ? void 0 : _a.map((value) => value.readInt16BE(0)), }; responseData = returnData; resolve(responseData); }); break; case 'FC4': client.readInputRegisters({ address: memoryAddress, quantity, extra: { unitId } }, (err, data) => { var _a; if (err) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'MODBUS FC4 Error: ' + err.message); } const returnData = { functionCode: 'FC4', address: memoryAddress, quantity: quantity, data: (_a = data === null || data === void 0 ? void 0 : data.response.data) === null || _a === void 0 ? void 0 : _a.map((value) => value.readInt16BE(0)), }; responseData = returnData; resolve(responseData); }); break; default: throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid function code: ' + functionCode); } }); } if (operation === 'write') { const value = this.getNodeParameter('value', 0); const buffer = Buffer.alloc(2); buffer.writeInt16BE(value); await new Promise((resolve) => { client.writeSingleRegister({ address: memoryAddress, value: buffer, extra: { unitId } }, (err, data) => { if (err) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'MODBUS Write Error: ' + err.message); } const returnData = { functionCode: 'FC6', address: memoryAddress, value: value, data: data.response, }; responseData = returnData; resolve(responseData); }); }); } return [this.helpers.returnJsonArray(responseData)]; } } exports.Modbus = Modbus; //# sourceMappingURL=Modbus.node.js.map