HJ212自定义组包发送数据

//ascii转16进制字符串 function convertToHexa(str) { var res = []; var len = str.length; for (var n = 0, l = len; n < l; n++) { //var hex = Number(str.charCodeAt(n)).toString(10);//10进制 var hex = Number(str.charCodeAt(n)).toString(16);//16进制 res.push(hex); } return res.join(''); } //十六进制字符串转换为数组 function hexStringToArray(str) { var pos = 0; var len = str.length; if (len % 2 != 0) { return null; } len /= 2; var arrBytes = new Array(); for (var i = 0; i < len; i++) { var s = "0x"; s += str.substr(pos, 2); arrBytes.push(s); pos += 2; } return arrBytes; } //年月日时分秒毫秒 function getYMDHMS(timestamp) { var time = new Date(timestamp); var year = time.getFullYear(); var month = time.getMonth() + 1; var date = time.getDate(); var hours = time.getHours(); var minute = time.getMinutes(); var second = time.getSeconds(); var mes = time.getMilliseconds(); if (month < 10) { month = "0" + month; } if (date < 10) { date = "0" + date; } if (hours < 10) { hours = "0" + hours; } if (minute < 10) { minute = "0" + minute; } if (second < 10) { second = "0" + second; } return ( year + "-" + month + "-" + date + " " + hours + ":" + minute + ":" + second+"."+mes ); } //只要年月日的时间 function getYMD(timestamp) { var time = new Date(timestamp); var year = time.getFullYear(); var month = time.getMonth() + 1; var date = time.getDate(); var hours = time.getHours(); var minute = time.getMinutes(); var second = time.getSeconds(); var mes = time.getMilliseconds(); if (month < 10) { month = "0" + month; } if (date < 10) { date = "0" + date; } return ( year.toString() + month.toString()+ date.toString() + "000000001" ); } //计算Body长度 function getBodyLen(len) { var fm = ""; if(len>=0 && len <10){fm = "000"+len} else if(len>=10 && len <100){fm = "00"+len} else if(len>=100 && len <1000){fm = "0"+len} else {fm=len} return fm; } //格式化CRC字符串 function formatCRCNum(str,len){ var strLength = len - str.length;//格式长度减去数字的长度,就是数字前补"0"的个数 for(var i=0; i<strLength;i++){ str = "0"+str; } return str; } //alert(getYMDHMS(new Date().getTime()));//当前时间 //减一天的时间 //alert(getYMDHMS(new Date().getTime()-86400000)); //上报当前时间减去前一天时间的数据,QN是当前时间,DataTime是前一天 var DT = new Date().getTime(); var QN = getYMD(DT); var DataTime = getYMD(DT - 86400000);//前一天的时间 DataTime = DataTime.substr(0, 14);//不需要毫秒 alert("日消耗 QN: "+QN+" "+"DataTime: "+DataTime); //var msg = "QN=20220701132624373;ST=32;CN=2031;PW=123456;MN=45007741004940;Flag=4;CP=&&DataTime=20220701132624;w00000-Rtd=0.000000&&"; //msg = [0x51,0x4E,0x3D,0x32,0x30,0x32,0x32,0x30,0x37,0x30,0x31,0x31,0x33,0x32,0x36,0x32,0x34,0x33,0x37,0x33,0x3B,0x53,0x54,0x3D,0x33,0x32,0x3B,0x43,0x4E,0x3D,0x32,0x30,0x31,0x31,0x3B,0x50,0x57,0x3D,0x31,0x32,0x33,0x34,0x35,0x36,0x3B,0x4D,0x4E,0x3D,0x34,0x35,0x30,0x30,0x37,0x37,0x34,0x31,0x30,0x30,0x34,0x39,0x34,0x31,0x3B,0x46,0x6C,0x61,0x67,0x3D,0x34,0x3B,0x43,0x50,0x3D,0x26,0x26,0x44,0x61,0x74,0x61,0x54,0x69,0x6D,0x65,0x3D,0x32,0x30,0x32,0x32,0x30,0x37,0x30,0x31,0x31,0x33,0x32,0x36,0x32,0x34,0x3B,0x77,0x30,0x30,0x30,0x30,0x30,0x2D,0x52,0x74,0x64,0x3D,0x30,0x2E,0x30,0x30,0x30,0x30,0x30,0x30,0x26,0x26]; var body = "QN="; body += QN; body += ";ST=32;CN=2031;PW=123456;MN=45007741004940;Flag=4;CP=&&DataTime="; body += DataTime; body += ";w00000-Cou="; var dayValue = parseFloat(ReadFromTag('s.Ch2.日.累计值')); dayValue = dayValue.toFixed(1); body += dayValue; body += "&&"; alert("日消耗body: "+body) var BodyHexStr = convertToHexa(body); //alert(BodyHexStr) var BodyHexArr = hexStringToArray(BodyHexStr); //alert(BodyHexStr); var hj212CRCDec = DoCRC16_HJ212(0,BodyHexArr.length,BodyHexArr);//十进制 //alert("hj212CRCDec: "+hj212CRCDec); var hj212CRCStr = formatCRCNum(hj212CRCDec.toString(16),4).toUpperCase();//转16进制,然后格式化两字节填充0,再转大写 alert("日消耗hj212CRCStr: "+hj212CRCStr); //组装报文 var msg = "##"; var bodyLen = getBodyLen(BodyHexArr.length); msg += bodyLen; msg += body; msg += hj212CRCStr; alert("日消耗msg: "+msg); var msgHexStr = convertToHexa(msg); var msgHexArr = hexStringToArray(msgHexStr); msgHexArr[msgHexArr.length+1] = 0x0D; msgHexArr[msgHexArr.length+2] = 0x0A; //上报TCP var recv = SendTextToTCP("192.168.1.123",5043,1000,msgHexArr);
2023-08-11
0 0