PCR2 Payload Decoder Example (JavaScript)

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 DecodePCR2Payload(payload_hex) {
     var obj = new Object();
     if(post == 14) // Application payload only
     {
        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;
}