# Read/write serial communication, bitwise

**URL:** <https://discourse.nodered.org/t/read-write-serial-communication-bitwise/68525>\
**Category:** General\
**Tags:** function-node\
**Created:** [3 October 2022 21:08 UTC](https://discourse.nodered.org/t/read-write-serial-communication-bitwise/68525 "2022-10-03T21:08:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alveman](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@alveman](https://discourse.nodered.org/u/alveman)\
**Post date:** [3 October 2022 21:08 UTC](https://discourse.nodered.org/t/read-write-serial-communication-bitwise/68525/1 "2022-10-03T21:08:05Z")

</div>

I am trying to read/write values ​​to a heat pump. How the communication works is described at [https://rago600.sourceforge.net/](https://rago600.sourceforge.net/) and in these examples.

**For example:**  
Register 020B is sixteen bit number.  
Let's write it in binary form 0000 0010|0000 1011  
Now we can mark 7 groups from the end 00|0000100|0001011  
And now we have this number in 7 bit form 00 04 0B

We have received temperature 00 04 38  
Let's write 7 bit numbers in binary form 0000000|0000010|0111000  
We will mark 8 bit delimiters 00000001|00111000  
It is: 138(hex) 312(dec)  
In that case temperature is 31.2C

We have received temperature 03 7C 1D  
Let's write 7 bit numbers in binary form 0000011|1111100|0011101  
We will mark 8 bit delimiters 11111110|00011101  
It is: FE1D(hex) 65053(dec)-483(signed integer)  
Received temperature is -48.3C

I have succeeded in decoding the values ​​coming from the heat pump with this function but cannot get the reverse to work ie from dec to 7 bit hex.

## Decoding code:

## var data = Buffer.from(msg.payload); var decoded\_int = (data[1] \<\< 14) + (data[2] \<\< 7) + data[3]; var decoded\_buffer = Buffer.from([decoded\_int & 0xff, decoded\_int \>\> 8]); var decoded = decoded\_buffer.readInt16LE(); return { "decoded": decoded };

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [4 October 2022 07:56 UTC](https://discourse.nodered.org/t/read-write-serial-communication-bitwise/68525/2 "2022-10-04T07:56:32Z")

</div>

Disclaimer, i know nothing about bitwise operations, but would the [buffer parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser) node help you out here ? I believe it takes away the heavy lifting.

---

<div class="post-metadata">

**Author:** ![alveman](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@alveman](https://discourse.nodered.org/u/alveman)\
**Post date:** [6 October 2022 06:30 UTC](https://discourse.nodered.org/t/read-write-serial-communication-bitwise/68525/3 "2022-10-06T06:30:35Z")

</div>

I have managed to get a bit on the way (unfortunately not with the prettiest code) The function now returns an Array or Buffer which is correct. But I can't get to the calculation of the checksum, which should be 8bit Xor on the last 4 values.

```auto
var int = msg.payload;
function createBinaryString(nMask) {
    // nMask must be between -2147483648 and 2147483647
    for (var nFlag = 0, nShifted = nMask, sMask = ""; nFlag < 32;
        nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1);
    return sMask;
}
var bin = createBinaryString(int);
 var array = new Array();
    array = ["0x81","0x03","0x00","0x00","0x01"];
    array[7] = bin.slice(bin.length - 7);
    array[6] = bin.slice(bin.length-14, - 7);
    array[5] = "00000" + bin.slice(bin.length-16, - 14);
    array[7] = "0x" + parseInt(array[7], 2).toString(16);
    array[6] = "0x" + parseInt(array[6], 2).toString(16);
    array[5] = "0x" + parseInt(array[5], 2).toString(16);

const buf = Buffer.from(array);

var newMsg = [{ payload: buf }];
return newMsg;

//Checksum for -10 shell be 0x0B
//Checksum for 10 shell be 0X0B
//Result msg.payload = -10 ([0x81,0x3,0x0,0x0,0x1,0x3,0x7f,0x76])
//Result msg.payload = 10 ([0x81,0x3,0x0,0x0,0x1,0x0,0x0,0xa]) 
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 December 2022 06:31 UTC](https://discourse.nodered.org/t/read-write-serial-communication-bitwise/68525/4 "2022-12-05T06:31:20Z")

</div>

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