Converting an array of bytes into a int16 to write in a modbus server(holding register)

Here's a part of a modbus table I'm working with :

Every this 16 boolean needs to be modify separetly but the problem is that the modbus server only takes UINT16.

So for example if you want the radiator to start (second BOOL):
1- You put it to 1
2 - Then the 16 booleans are gathered to do a binary number 0000000000000010
3 - Finally this binary number is converted to a UINT number. For this example 2
4 - The other automate plugged into the modbus server read this value and convert it back to a boolean

And I need to do the same first 3 step on Nodered

I thought this might be where you are heading BUT when you write to bit 1, you will reset bits 0 & 2, 3 → 15

Is it a problem if you write the value 0000 0000 0000 0010 (AKA 0x0002 AKA 2) to ModBus register 60 ?

This is not a problem because they will change the parameters once at a time.

I wasn't sure if the server would like It but I can try right now.

I just need to put the 16 numbers together because he won't accept an array (that's why I tried to convert it directly to a UINT.)

It doesn't work with the 0000 0000 0000 0010

I will have to do the convert It

So is the buffer parser still an option ? Because I don't know how to use it properly

What do you mean exaclty...

  1. User will change all necessary values THEN press the button to compute a 16bit value & write that to ModBus
    OR
  2. User will change 1 value THEN press the button to compute a 16bit value & write that to ModBus

The reason I ask..

If it is option 2 then changing 60.1 to on then 60.2 to on will write 0x0002 then write 0x0004

That means

Button Press set value computed 16 bit value HEX
1 1 0000 0000 0000 0000 0010 0x0002
2 1 0000 0000 0000 0000 0100 0x0004

In simple terms, converting 16 bits with only 1 set will reset the others.

Yes it is option 2

So is it a problem that when the user changes 60.3 it will write 0000 0000 0000 1000 (`0x0008) meaning it will RESET 60.2 and 60.1 (and all the others) back to zero?

No it's not a problem

ok, 5 mins thinking!

1 Like

You're the best I swear

So, based on my understanding of what you want (I still think there is one missing piece but hey ho)..

chrome_juhv0IuMNq

The demo flow...

[{"id":"d25e7704f45533a2","type":"function","z":"fb49eb9df4ba7fc2","name":"Adressage","func":"\nconst num = msg.payload.S1; //Chiffre allant de 0 Ă  79\nconst val =  msg.payload.B1; // boolĂŠen\n\nconst parsedInput = { WORD_OFFSET: Math.round(num / 16), BIT: num % 16, BIT_VAL: !!val }\nconst value = updateBit(0, parsedInput.BIT, parsedInput.BIT_VAL);\n\nmsg.payload = {\n    inputData: msg.payload,\n    parsedInput: parsedInput,\n    register: 60 + parsedInput.WORD_OFFSET,\n    value: value,\n};\nreturn msg\n\n\n\nfunction getBit(number, bitPosition) {\n    return (number & (1 << bitPosition)) === 0 ? 0 : 1;\n}\nfunction setBit(number, bitPosition) {\n    return number | (1 << bitPosition);\n}\nfunction clearBit(number, bitPosition) {\n    const mask = ~(1 << bitPosition);\n    return number & mask;\n}\nfunction updateBit(number, bitPosition, bitValue) {\n    const bitValueNormalized = bitValue ? 1 : 0;\n    const clearMask = ~(1 << bitPosition);\n    return (number & clearMask) | (bitValueNormalized << bitPosition);\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":554,"y":256,"wires":[["928692da614a222d"]]},{"id":"691ae780d1dc0dec","type":"inject","z":"fb49eb9df4ba7fc2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"S1\":2,\"B1\":0}","payloadType":"json","x":350,"y":208,"wires":[["d25e7704f45533a2"]]},{"id":"4910d876dc0a21cb","type":"inject","z":"fb49eb9df4ba7fc2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"S1\":4,\"B1\":1}","payloadType":"json","x":352,"y":256,"wires":[["d25e7704f45533a2"]]},{"id":"a5183058fbef4f74","type":"inject","z":"fb49eb9df4ba7fc2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"S1\":79,\"B1\":1}","payloadType":"json","x":352,"y":304,"wires":[["d25e7704f45533a2"]]},{"id":"928692da614a222d","type":"debug","z":"fb49eb9df4ba7fc2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":730,"y":256,"wires":[]},{"id":"2967c630b3030608","type":"comment","z":"fb49eb9df4ba7fc2","name":"Test data","info":"","x":332,"y":160,"wires":[]},{"id":"4b856a9f60ddccc2","type":"comment","z":"fb49eb9df4ba7fc2","name":"Details for modbus","info":"","x":742,"y":208,"wires":[]}]

Hope it helps.

3 Likes

I implement It and I give you a feedback but I'm almost sure It's the way

That's the solution :+1: : , I'm going to study it to understand what you did exactly
Thank you for eveything, I see you're helping on a lot of forums and It's very cool to have people like you around :grin:

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.