var layer; var form; var debugStr = "modbus"; var objParam = {}; var modeStr = "normal"; $(function () { layui.use(["layer", "form"],function () { layer = layui.layer; form = layui.form; form.render(); }); renderMode_(); cleanByType(); //协议选择事件 $("#debug").change(function () { debugStr = $(this).val(); $(".div-sel").addClass("div-hide"); $(".div-code").addClass("div-hide"); $("#txt").val(""); if(debugStr == "snmp"){ $("#txt").prop("readonly",false); $(".div-type").addClass("div-hide"); $(".div-snmp").removeClass("div-hide"); }else{ $(".div-type").removeClass("div-hide"); renderSelType(debugStr); if(debugStr == "modbus"){ $("#txt").prop("readonly",true); $(".div-modbus").removeClass("div-hide"); }else if(debugStr == "string"){ $("#txt").prop("readonly",false); $(".div-string1").removeClass("div-hide"); $(".div-code").removeClass("div-hide"); }else{ $("#txt").prop("readonly",true); $(".div-dz").removeClass("div-hide"); } } }); //协议选择事件 $("#type").change(function () { var cur = $(this).val(); $(".div-sel").addClass("div-hide"); if(debugStr == "modbus"){ if(cur == "cmd" || cur == "com"){ $(".div-modbus").removeClass("div-hide"); } if(cur == "modbustcp" || cur == "tcp" || cur == "udp"){ $(".div-net-rtu").removeClass("div-hide"); } } if(debugStr == "powerbus"){ if(cur == "cmd" || cur == "com"){ $(".div-dz").removeClass("div-hide"); } if(cur == "tcp" || cur == "udp"){ $(".div-net-dz").removeClass("div-hide"); } } if(debugStr == "string"){ if(cur == "cmd" || cur == "com"){ $(".div-string1").removeClass("div-hide"); } if(cur == "tcp" || cur == "udp"){ $(".div-string2").removeClass("div-hide"); } } }); //点击事件 $(".send-btn").click(function () { if(modeStr == "normal"){ window.parent.parent.notify("当前模式是正常模式,不允许发送信息!"); return; } sendData(); }); }); function renderSelType(str) { var it = ''; it += ''; it += ''; it += ''; if(str == "modbus"){ it += ''; } $("#type").html(it); } //模式切换 function renderMode_() { $.get("./cgi-bin/device-debug/query-mode", function (data, status) { var str = ""; if(data.mode == "normal"){ str = "正常模式"; }else{ str = "调试模式"; } modeStr = data.mode; $(".set-mode").attr("name",data.mode); $("#modeName").text(str); }, "json"); //模式切换 $(".set-mode").click(function () { var name = $(this).attr("name"); // var str = ""; // if(name == "normal"){ // str = "正常模式"; // }else{ // str = "调试模式"; // } // $("#modeName").text(str); var obj = {}; if(name == "normal"){ obj.mode = "debug" }else{ obj.mode = "normal" } $.post("./cgi-bin/device-debug/set-mode",JSON.stringify(obj), function (data, status) { if(data.code == "success"){ var str = ""; if(obj.mode == "normal"){ str = "正常模式"; }else{ str = "调试模式"; } modeStr = obj.mode; $(".set-mode").attr("name",obj.mode); $("#modeName").text(str); }else{ window.parent.parent.notify("模式切换失败!"); } }, "json"); }); } function cleanByType() { $(".clean-i").click(function () { $("#txt").val(""); }); $(".clean-i-txt").click(function () { $(".txt-i").addClass("div-hide"); }); $(".clean-o-txt").click(function () { $(".txt-o").addClass("div-hide"); }); } function getObjData() { var res = {}; res.debug = debugStr; res.type = $("#type").val(); if(res.debug == "modbus" && (res.type == "cmd" || res.type == "com")){ res.ip = "0"; res.port = $("#modbus-com").val(); res.addr = $("#modbus-addr").val(); res.funCode = $("#modbus-funCode").val(); res.starReg = $("#modbus-starReg").val(); res.regCnt = $("#modbus-regCnt").val(); } if(res.debug == "modbus" && (res.type == "tcp" || res.type == "udp" || res.type == "modbustcp")){ res.ip = $("#net-rtu-ip").val(); res.port = $("#net-rtu-port").val(); res.addr = $("#net-rtu-addr").val(); res.funCode = $("#net-rtu-funCode").val(); res.starReg = $("#net-rtu-starReg").val(); res.regCnt = $("#net-rtu-regCnt").val(); } if(res.debug == "powerbus" && (res.type == "cmd" || res.type == "com")){ res.ip = "0"; res.port = $("#dz-com").val(); res.ver = $("#dz-ver").val(); res.addr = $("#dz-addr").val(); res.cid1 = $("#dz-cid1").val(); res.cid2 = $("#dz-cid2").val(); res.data = $("#dz-data").val(); } if(res.debug == "powerbus" && (res.type == "tcp" || res.type == "udp")){ res.ip = $("#net-dz-ip").val(); res.port = $("#net-dz-port").val(); res.ver = $("#net-dz-ver").val(); res.addr = $("#net-dz-addr").val(); res.cid1 = $("#net-dz-cid1").val(); res.cid2 = $("#net-dz-cid2").val(); res.data = $("#net-dz-data").val(); } if(res.debug == "string" && (res.type == "cmd" || res.type == "com")){ res.ip = "0"; res.port = $("#string1-com").val(); res.data = $("#txt").val(); } if(res.debug == "string" && (res.type == "tcp" || res.type == "udp")){ res.ip = $("#string2-ip").val(); res.port = $("#string2-port").val(); res.data = $("#txt").val(); } if(res.debug == "snmp"){ res.ip = $("#snmp-ip").val(); res.ver = $("#snmp-ver").val(); res.oid = $("#txt").val(); } objParam = res; return res; } function sendDebug() { objParam = {}; objParam = getObjData(); objParam.type = "cmd"; console.log(objParam); if(objParam.debug == "modbus"){ if(!objParam.ip || !objParam.port || !objParam.addr || !objParam.funCode || !objParam.starReg || !objParam.regCnt){ window.parent.parent.notify("传输参数不能为空!"); return ; } } if(objParam.debug == "powerbus"){ if(!objParam.ip || !objParam.port || !objParam.ver || !objParam.addr || !objParam.cid1 || !objParam.cid2){ window.parent.parent.notify("传输参数不能为空!"); return ; } } if(objParam.debug == "string"){ if(!objParam.ip || !objParam.port){ window.parent.parent.notify("传输参数不能为空!"); return ; } objParam.type = $("#type").val(); if(form.val('txt-code-send').code == "HEX"){ objParam.data = "0x" + objParam.data; } renderData(objParam.data,"send"); } if(objParam.debug == "snmp"){ if(!objParam.ip){ window.parent.parent.notify("传输参数不能为空!"); return ; } renderData(objParam.oid,"send"); } $.post("./cgi-bin/device-debug/debug", JSON.stringify(objParam), function (data, status) { if(objParam.debug == "snmp"){ renderData(data.recvData,"receive"); }else{ if(objParam.type != "cmd"){ renderData(data.recvData,"receive"); }else{ if ("success" == data.code) { renderData(data.sendData,"send"); sendDebug2(); } else { window.parent.parent.notify("接收数据时出错,请重新操作!"); } } } }, "json"); } function sendDebug2() { objParam = {}; objParam = getObjData(); if(objParam.debug == "modbus"){ if(!objParam.ip || !objParam.port || !objParam.addr || !objParam.funCode || !objParam.starReg || !objParam.regCnt){ window.parent.parent.notify("传输参数不能为空!"); return ; } } if(objParam.debug == "powerbus"){ if(!objParam.ip || !objParam.port || !objParam.ver || !objParam.addr || !objParam.cid1 || !objParam.cid2){ window.parent.parent.notify("传输参数不能为空!"); return ; } } if(objParam.debug == "string"){ if(!objParam.ip || !objParam.port){ window.parent.parent.notify("传输参数不能为空!"); return ; } } $.post("./cgi-bin/device-debug/debug", JSON.stringify(objParam), function (data, status) { if ("success" == data.code) { renderData(data.recvData,"receive"); } else { window.parent.parent.notify("接收数据时出错,请重新操作!"); } }, "json"); } function sendData() { sendDebug(); // if(debugStr != "string"){ // sendDebug(); // }else{ // getObjData(); // orderSend(); // } } function orderSend() { var data = $("#txt").val(); if(!data){ window.parent.parent.notify("发送的数据不能是空!"); return false; } var param = {data: data}; if(debugStr != "snmp"){ param.port = objParam.port; } if(debugStr != "string"){ param.data = "0x"+param.data; } console.log('---------发送的数据-----' + data); console.log(param); //渲染发送的数据 renderData(data,"send"); $.post("./cgi-bin/sys-com/send", JSON.stringify(param), function (data, status) { if ("success" == status) { console.log('---------接收到的数据--------' + data); // $("#device-port").val(data.port); //渲染接收的数据 renderData(data.data,"receive"); } else { window.parent.parent.notify("接收数据时出错,请重新操作!"); } }, "json"); }; function renderData(result, status) { var data; var time = new Date(); time = time.getFullYear() + "-" + ((time.getMonth()+1) < 10 ? ("0" + (time.getMonth() + 1)) : (time.getMonth() + 1)) + "-" + (time.getDate() < 10 ? ("0" + time.getDate()) : time.getDate()) + " " + (time.getHours() < 10 ? ("0" + time.getHours()) : time.getHours()) + ":" + (time.getMinutes() < 10 ? ("0" + time.getMinutes()) : time.getMinutes()) + ":" + (time.getSeconds() < 10 ? ("0" + time.getSeconds()) : time.getSeconds()); if(status == "send"){ data = "
[" + time + "][发送]:" + result + "
"; }else{ var s = result; if(debugStr == "string"){ s = hexCharCodeToStr(result) } data = "[" + time + "][接收]:" + s + "
"; } $(".log-box").append(data); //设置滚动条始终位于最底端 $(".log-box").scrollTop($(".log-box")[0].scrollHeight); } //判断数据接收格式并进行转译 function hexCharCodeToStr(result) { var data = ""; if(form.val('txt-code').code == "ASCII"){ while (true){ if(result.indexOf(" ") == -1){ break; } result = result.replace(" ", ""); } //去除字符串中的空格 // result = result.replaceAll(" ", ""); var len = result.length; if(len % 2 !== 0){ window.parent.parent.notify("接收的数据中存在非法字符!该条数据将不进行转译!"); return result; } var curCharCode; var resultStr = []; for (var i = 0; i < len; i = i + 2) { curCharCode = parseInt(result.substr(i, 2), 16); resultStr.push(String.fromCharCode(curCharCode)); } data = resultStr.join(""); }else{ data = result; } return data; }