var layer; var form; $(function () { layui.use(["layer", "form"],function () { layer = layui.layer; form = layui.form; form.render(); }); }); function orderSend(id) { // var data = $("#"+id).val().replaceAll(" ", ""); var data = $("#"+id).val(); while (true){ if(data.indexOf(" ") == -1){ break; } data = data.replace(" ", ""); } if(!data){ // console.log("发送的数据不能是空!"); window.parent.notify("发送的数据不能是空!"); return false; } var param = { port: $("#device-port").val(), data: data }; console.log('---------发送的数据-----' + data); console.log(param); //渲染发送的数据 renderData($("#"+id).val(),"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{ data = "

[" + time + "][接收]:" + hexCharCodeToStr(result) + "

"; } $(".log-box").append(data); //设置滚动条始终位于最底端 $(".log-box").scrollTop($(".log-box")[0].scrollHeight); } //判断数据接收格式并进行转译 function hexCharCodeToStr(result) { var data = ""; if(form.val('form-port').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; }