var cmd1 = "FE FE FE 7C 00 09 01 ff ff 00 00 00 00 ** ** 16"; var cmd2 = "FE FE FE 7C 00 09 02 00 FF 00 01 00 00 ** ** 16"; var cmd3 = "FE FE FE 7C 00 18 03 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 00 00 ** ** 16"; var cmd4 = "FE FE FE 7C 00 09 04 00 FF 00 01 00 00 ** ** 16"; var startWith = "FE FE FE 7C"; function getCmd(msg){ var mingling = msg.type; var sendMsg = ''; switch(mingling) { case '01': console.log("开始请求第一条通信协议"); sendMsg = getSendMsg(mingling,"ff ff"," 00 00 00 00"); break; case '02': console.log("开始请求第二条通信协议"); sendMsg = getSendMsg(mingling,"00 ff 00 01"," 00 00 00 00"); break; case '03': console.log("开始请求第三条通信协议"); sendMsg = getSendMsg(mingling,msg.sbid); break; case '04': console.log("开始请求第四条通信协议"); sendMsg = getSendMsg(mingling,msg.sbid); break; default: } console.log(sendMsg); } /** * 数字转16进制码 * @param {*} num */ function toHex16(num){ console.log("dayin"+num) var hex = num.toString(16).toUpperCase(); var length = hex.length; switch(length){ case 1: hex = "00 "+"0"+hex; break; case 2: hex = "00 "+hex; break; case 3: hex = "0 "+hex; hex = hex.substring(0, 2) + hex.substring(2, 4); break; case 4: hex = hex.substring(0, 2) +" "+ hex.substring(2, 4); break; default: break; } console.log(hex); return hex; } /** * 检查是否是Hex * @param {*} dataStr */ function checkHexStr(dataStr) { dataStr = dataStr.replace(/\s/g, ""); if (dataStr.length % 2 != 0) { return ""; } if (/[0-9A-Fa-f]/g.test(dataStr)) { return dataStr; } return ""; } /** * 组装发送数据 * @param {*} mingling * @param {*} sbid */ function getSendMsg(mingling,sbid,mess){ var cmd = mingling+" "+sbid+mess; var length = toHex16(cmd.split(" ").length); cmd = length +" "+cmd; var crc = CRC.ToModbusCRC16(cmd,false); cmd = startWith+" "+cmd+" "+crc.substring(0, 2) +" "+ crc.substring(2, 4)+" 16"; return cmd; } function bin_to_hex(str) { let hex_array = [{key:0,val:"0000"},{key:1,val:"0001"},{key:2,val:"0010"},{key:3,val:"0011"},{key:4,val:"0100"},{key:5,val:"0101"},{key:6,val:"0110"},{key:7,val:"0111"}, {key:8,val:"1000"},{key:9,val:"1001"},{key:'a',val:"1010"},{key:'b',val:"1011"},{key:'c',val:"1100"},{key:'d',val:"1101"},{key:'e',val:"1110"},{key:'f',val:"1111"}] let value = ''; let list=[]; if(str.length%4!==0){ let a = "0000" let b=a.substring(0,4-str.length%4) str = b.concat(str) } while (str.length > 4) { list.push(str.substring(0, 4)) str = str.substring(4); } list.push(str); for(let i=0;i 0) { var crc = 0xFFFF; for (var i = 0; i < len; i++) { crc = (crc ^ (data[i])); for (var j = 0; j < 8; j++) { crc = (crc & 1) != 0 ? ((crc >> 1) ^ 0xA001) : (crc >> 1); } } var hi = ((crc & 0xFF00) >> 8); //高位置 var lo = (crc & 0x00FF); //低位置 return [hi, lo]; } return [0, 0]; }; CRC.isArray = function (arr) { return Object.prototype.toString.call(arr) === '[object Array]'; }; CRC.ToCRC16 = function (str, isReverse) { return CRC.toString(CRC.CRC16(CRC.isArray(str) ? str : CRC.strToByte(str)), isReverse); }; CRC.ToModbusCRC16 = function (str, isReverse) { return CRC.toString(CRC.CRC16(CRC.isArray(str) ? str : CRC.strToHex(str)), isReverse); }; CRC.strToByte = function (str) { var tmp = str.split(''), arr = []; for (var i = 0, c = tmp.length; i < c; i++) { var j = encodeURI(tmp[i]); if (j.length == 1) { arr.push(j.charCodeAt()); } else { var b = j.split('%'); for (var m = 1; m < b.length; m++) { arr.push(parseInt('0x' + b[m])); } } } return arr; }; CRC.convertChinese = function (str) { var tmp = str.split(''), arr = []; for (var i = 0, c = tmp.length; i < c; i++) { var s = tmp[i].charCodeAt(); if (s <= 0 || s >= 127) { arr.push(s.toString(16)); } else { arr.push(tmp[i]); } } return arr; }; CRC.filterChinese = function (str) { var tmp = str.split(''), arr = []; for (var i = 0, c = tmp.length; i < c; i++) { var s = tmp[i].charCodeAt(); if (s > 0 && s < 127) { arr.push(tmp[i]); } } return arr; }; CRC.strToHex = function (hex, isFilterChinese) { hex = isFilterChinese ? CRC.filterChinese(hex).join('') : CRC.convertChinese(hex).join(''); //清除所有空格 hex = hex.replace(/\s/g, ""); //若字符个数为奇数,补一个0 hex += hex.length % 2 != 0 ? "0" : ""; var c = hex.length / 2, arr = []; for (var i = 0; i < c; i++) { arr.push(parseInt(hex.substr(i * 2, 2), 16)); } return arr; }; CRC.padLeft = function (s, w, pc) { if (pc == undefined) { pc = '0'; } for (var i = 0, c = w - s.length; i < c; i++) { s = pc + s; } return s; }; CRC.toString = function (arr, isReverse) { if (typeof isReverse == 'undefined') { isReverse = true; } var hi = arr[0], lo = arr[1]; return CRC.padLeft((isReverse ? hi + lo * 0x100 : hi * 0x100 + lo).toString(16).toUpperCase(), 4, '0'); };