PCR2 Payload Decoder Code Example (NodeJS)

This sample code is provided “as is” without warranty of any kind, either expressed or implied. You can freely use this code as a starting point.

However do not use this for productive environments as it may contain bugs.


const TYPE_TEMP = 0x01; // temp 2 bytes -3276.8°C -->3276.7°C
const TYPE_LTR = 0x0A;  // 2 bytes left-to-right counter
const TYPE_RTL = 0x16; // 2 bytes right-to-left counter 

function bin16dec(bin) {
     var num = bin & 0xFFFF;
     if (0x8000 & num) num = -(0x010000 - num);
     return num;
}

function hexToBytes(hex) {
     for (var bytes = [], c = 0; c < hex.length; c += 2)
     {
          bytes.push(parseInt(hex.substr(c, 2), 16));
     }
     return bytes;
}

function DecodePCR2Payload(payload_hex) {
     var obj = new Object();
     for (var i = 0; i < payload_hex.length; i++) {
        switch (payload_hex[i]) {
             case TYPE_TEMP: // CPU temperature
                 var temp = (payload_hex[i + 1] << 8) | (payload_hex[i + 2]);
                 temp = bin16dec(temp);
                 obj.Temp = Math.floor(temp / 10);
                 i += 2;
                 break;
             case TYPE_LTR: // left-to-right counter
                 obj.LTR = (payload_hex[i + 1] << 8) | (payload_hex[i + 2]);
                 i += 2;
                 break;
             case TYPE_RTL: // right-to-left counter
                 obj.RTL = (payload_hex[i + 1] << 8) | (payload_hex[i + 2]);
                 i += 2;
                 break;
             default: //something is wrong with data
                 i = payload_hex.length;
                 break;
         }
     }
     return obj;
}



Last Updated July 15, 2019
  • On 2019-05-05