SOURCE

//盖勒克GLS-B40+型激光测距传感器
function Decode(fPort, bytes) {
    var jsonobj;
    var myDate = new Date();
    var asciiData = "";
    var result = 0;
    var current_time = myDate.toLocaleString()
    var rawdata = "";
    for(var i = 0; i < bytes.length; i++) {
        rawdata += bytes[i] + " ";
    }
  
    // 心跳包
    if(fPort == -1) {
       jsonobj = {
            "Flag": -1,
            "SensorType": 40,
            "SensorNumberThird": -1,
            "Timestamp": current_time
        };     
    }
	if (bytes.length > 0) {

        // 数据在4-10字节
        for (var i = 0; i < bytes.length; i++) {
            if (i < 3 || i > 10) {
                continue;
            }
            // 过滤ASCII控制字符
            if(bytes[i] <= 31) {
                asciiData = asciiData + '0';
                continue;
            }
            // 十进制转16进制
            var hexValue = dec2hex(bytes[i]);
            // 16进制转ASCII
            
            var asciiValue = hexToASCII(hexValue);
            asciiData = asciiData + asciiValue;
        }
        // 去除unicode空字符串(\u0000)
        var asciiDataArr = asciiData.split("");
        var newValue = '';
        for (var i = 0; i < asciiDataArr.length; i++) {
            if (asciiDataArr[i] != '\u0000') {
                newValue += asciiDataArr[i];
            }
        }
        // 米转换毫米
        var floatValue = parseFloat(newValue * 1000).toFixed(1);

        jsonobj = {
            "Flag": 1,
            "SensorType": 40,
            "SensorNumberThird": 1,
            "Value": +(floatValue),
            "Timestamp": current_time,
            "Rawdata":rawdata
        };
    } else {
        jsonobj = {
            "Flag": -1,
            "SensorType": 40,
            "SensorNumberThird": -1,
            "Timestamp": current_time
        };
    }
    return jsonobj;
}

// 十进制转16进制
function dec2hex(val){
  return ("0" + (Number(val).toString(16))).slice(-2).toUpperCase()
}

// 16进制转ASCII
function hexToASCII(hexCharCodeStr) {
    var trimedStr = hexCharCodeStr.trim();
    var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
    var len = rawStr.length;
    if (len % 2 !== 0) {
        console.info("存在非法字符!");
        return "";
    }
    var curCharCode;
    var resultStr = [];
    for (var i = 0; i < len; i = i + 2) {
        curCharCode = parseInt(rawStr.substr(i, 2), 16);
        resultStr.push(String.fromCharCode(curCharCode));
    }
    return resultStr.join("");
}

var bytes = [4,0,2,94,16,74,34,3,255,177,0,65,23,100,14,16,21,0,0,0,0,50,1,64,2,60];
var bytes2 = [1,6,130,48,48,57,46,49,54,57,16];
var bytes3 = [1,6,130,48,48,55,46,55,56,54,13];
var bytes4 = [1,6,130,30,48,49,46,48,49,48,54,241]
console.log(Decode(8, bytes2));
console 命令行工具 X clear

                    
>
console