Read/write serial communication, bitwise

I am trying to read/write values ​​to a heat pump. How the communication works is described at 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 };

Disclaimer, i know nothing about bitwise operations, but would the buffer parser node help you out here ? I believe it takes away the heavy lifting.

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.

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]) 

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