node-red-contrib-modbus-tcp-ip
Advanced tools
Comparing version 1.0.7 to 1.1.0
@@ -40,108 +40,111 @@ const modbus = require("./module/modbus-stream/lib/modbus"); | ||
msg.payload = msg.payload; | ||
msg.ip = node.ip; | ||
msg.port = parseInt(node.port); | ||
msg.ip = node.ip ? node.ip : msg.payload.modbus_ip; | ||
msg.port = node.port ? parseInt(node.port) : parseInt(msg.payload.modbus_ip); | ||
if (!msg.payload.address && msg.payload.address !== 0) { | ||
done("Invaid Modbus Address"); | ||
} else if (!msg.payload.quantity) { | ||
done("Invaid Modbus Quantity"); | ||
} else if (!msg.payload.unitid) { | ||
done("Invaid Modbus Unit ID"); | ||
} else { | ||
// if (!connection) { | ||
try { | ||
node.status({ | ||
fill: "yellow", | ||
shape: "dot", | ||
text: "Connecting to MODBUS TCP/IP" | ||
}); | ||
connection = await getModbusConnection(node.ip, node.port); | ||
// node.log("Connection Established"); | ||
connection.on('error', (err) => { | ||
node.log(`Re-Connecting to ${node.ip}:${node.port}`); | ||
}); | ||
node.status({ | ||
fill: "green", | ||
shape: "dot", | ||
text: "Connection Established" | ||
}); | ||
} catch (err) { | ||
node.error(err, err.message); | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: "Connection Error" | ||
}) | ||
done(); | ||
} | ||
// } | ||
if (connection) { | ||
start(); | ||
node.status({ | ||
fill: "yellow", | ||
shape: "dot", | ||
text: "Sending Request" | ||
}) | ||
let responseCallBack = (err, res) => { | ||
if (!err) { | ||
node.status({ | ||
fill: "green", | ||
shape: "dot", | ||
text: "Response Received " + end() + " ms" | ||
}) | ||
msg.responseBuffer = {}; | ||
msg.responseBuffer.buffer = Buffer.concat(res.response.data); | ||
connection.close(() => { | ||
node.log("Connection closed"); | ||
}); | ||
send(msg); | ||
done(); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: "Error Getting Response" | ||
}) | ||
node.error(err, err.message) | ||
connection.close(() => { | ||
node.log("Connection closed"); | ||
}); | ||
done(); | ||
} | ||
if(!msg.ip || !msg.port) done("Invaid Modbus IP or PORT"); | ||
else{ | ||
if (!msg.payload.address && msg.payload.address !== 0) { | ||
done("Invaid Modbus Address"); | ||
} else if (!msg.payload.quantity) { | ||
done("Invaid Modbus Quantity"); | ||
} else if (!msg.payload.unitid) { | ||
done("Invaid Modbus Unit ID"); | ||
} else { | ||
// if (!connection) { | ||
try { | ||
node.status({ | ||
fill: "yellow", | ||
shape: "dot", | ||
text: "Connecting to MODBUS TCP/IP" | ||
}); | ||
connection = await getModbusConnection(msg.ip , msg.port); | ||
// node.log("Connection Established"); | ||
connection.on('error', (err) => { | ||
node.log(`Re-Connecting to ${node.ip}:${node.port}`); | ||
}); | ||
node.status({ | ||
fill: "green", | ||
shape: "dot", | ||
text: "Connection Established" | ||
}); | ||
} catch (err) { | ||
node.error(err, err.message); | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: "Connection Error" | ||
}) | ||
done(); | ||
} | ||
if (msg.payload.functioncode == 1) { | ||
connection.readCoils({ | ||
address: msg.payload.address, | ||
quantity: msg.payload.quantity, | ||
extra: { | ||
unitId: msg.payload.unitid | ||
// } | ||
if (connection) { | ||
start(); | ||
node.status({ | ||
fill: "yellow", | ||
shape: "dot", | ||
text: "Sending Request" | ||
}) | ||
let responseCallBack = (err, res) => { | ||
if (!err) { | ||
node.status({ | ||
fill: "green", | ||
shape: "dot", | ||
text: "Response Received " + end() + " ms" | ||
}) | ||
msg.responseBuffer = {}; | ||
msg.responseBuffer.buffer = Buffer.concat(res.response.data); | ||
connection.close(() => { | ||
node.log("Connection closed"); | ||
}); | ||
send(msg); | ||
done(); | ||
} else { | ||
node.status({ | ||
fill: "red", | ||
shape: "dot", | ||
text: "Error Getting Response" | ||
}) | ||
node.error(err, err.message) | ||
connection.close(() => { | ||
node.log("Connection closed"); | ||
}); | ||
done(); | ||
} | ||
}, responseCallBack) | ||
} else if (msg.payload.functioncode == 2) { | ||
connection.readDiscreteInputs({ | ||
address: msg.payload.address, | ||
quantity: msg.payload.quantity, | ||
extra: { | ||
unitId: msg.payload.unitid | ||
} | ||
}, responseCallBack) | ||
} else if (msg.payload.functioncode == 3) { | ||
connection.readHoldingRegisters({ | ||
address: msg.payload.address, | ||
quantity: msg.payload.quantity, | ||
extra: { | ||
unitId: msg.payload.unitid | ||
} | ||
}, responseCallBack) | ||
} else if (msg.payload.functioncode == 4) { | ||
connection.readInputRegisters({ | ||
address: msg.payload.address, | ||
quantity: msg.payload.quantity, | ||
extra: { | ||
unitId: msg.payload.unitid | ||
} | ||
}, responseCallBack) | ||
} | ||
if (msg.payload.functioncode == 1) { | ||
connection.readCoils({ | ||
address: msg.payload.address, | ||
quantity: msg.payload.quantity, | ||
extra: { | ||
unitId: msg.payload.unitid | ||
} | ||
}, responseCallBack) | ||
} else if (msg.payload.functioncode == 2) { | ||
connection.readDiscreteInputs({ | ||
address: msg.payload.address, | ||
quantity: msg.payload.quantity, | ||
extra: { | ||
unitId: msg.payload.unitid | ||
} | ||
}, responseCallBack) | ||
} else if (msg.payload.functioncode == 3) { | ||
connection.readHoldingRegisters({ | ||
address: msg.payload.address, | ||
quantity: msg.payload.quantity, | ||
extra: { | ||
unitId: msg.payload.unitid | ||
} | ||
}, responseCallBack) | ||
} else if (msg.payload.functioncode == 4) { | ||
connection.readInputRegisters({ | ||
address: msg.payload.address, | ||
quantity: msg.payload.quantity, | ||
extra: { | ||
unitId: msg.payload.unitid | ||
} | ||
}, responseCallBack) | ||
} | ||
} | ||
@@ -148,0 +151,0 @@ } |
{ | ||
"name": "node-red-contrib-modbus-tcp-ip", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "A simple node for realtime MODBUS TCP/IP Process with added features.", | ||
@@ -5,0 +5,0 @@ "main": "modbus-tcp-ip.js", |
Sorry, the diff of this file is not supported yet
123586
1790