BiancoRoyal / node-red-contrib-modbus

maintained by PLUS for Node-RED - https://plus4nodered.com
https://www.npmjs.com/package/node-red-contrib-modbus
BSD 3-Clause "New" or "Revised" License
295 stars 108 forks source link

modbus server , address set error while using , and I fixed it #464

Open wei1224hf opened 4 months ago

wei1224hf commented 4 months ago

Which node-red-contrib-modbus version are you using?

/home/gene/nodejs/userDir1881

What happened?

using modbus server , inject data from input , with funcion content:

msg.payload = { 'value': 1, 'register': 'holding', 'address': 1, 'disableMsgOutput': 0 }; return msg;

after sending this to modbus-server node , the address 1 did not change , but the address 8 changed.

Server

Modbus-Server Node

How can this be reproduced?

deploy a modbus server node , send data : msg.payload = { 'value': 1, 'register': 'holding', 'address': 1, 'disableMsgOutput': 0 };

What did you expect to happen?

using another software ModScan32.exe , monitoring data , the address 1 data of the modbus server from node-red should changed to 1, but it did not , address 8 changed to 1 instead.

Other Information

I have already fixed this bug by

modbus-server-core.js , line 130:

de.biancoroyal.modbus.core.server.writeToModbusBuffer = function (node, msg) { function setBit(buffer, i, bit, value){ if(value == 0){ buffer[i] &= ~(1 << bit); }else{ buffer[i] |= (1 << bit); } } switch (msg.payload.register) { case 'holding': node.modbusServer.holding.writeUInt16BE(msg.bufferPayload, msg.bufferAddress/4) break case 'coils': const address = msg.bufferAddress/8; const idx = Math.floor(address/8) ; const idx2 = address%8; setBit(node.modbusServer.coils,idx,idx2,msg.bufferPayload); break case 'input': const address = msg.bufferAddress/8; const idx = Math.floor(address/8) ; const idx2 = address%8; setBit(node.modbusServer.input,idx,idx2_,msg.bufferPayload); break case 'discrete': node.modbusServer.discrete.writeUInt8(msg.bufferPayload, msg.bufferAddress/4) break default: return false } return true }

biancode commented 3 months ago

Hi, can I ask you to test again with version 5.40+?

github-actions[bot] commented 1 week ago

This issue is stale because it has been open 60 days with no activity. It will be closed in 30 days, but can be saved by removing the stale label or commenting.