From 18d2bc84aedd7ed29c2f0ac5f7dd6e1a7793c474 Mon Sep 17 00:00:00 2001 From: maochaoying <925670706@qq.com> Date: Tue, 9 May 2023 16:18:09 +0800 Subject: [PATCH] solve parse error --- index.html | 55 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 5cf0bf3..dc53ee4 100644 --- a/index.html +++ b/index.html @@ -339,6 +339,45 @@ } else { } }; + + const transValToName = (val) => { + switch (val) { + case 0x00: + return "kSuccess"; + case 2: + return "kRegNotFound"; + case 3: + return "kRegNotWritable"; + case 4: + return "kRegNotReadable"; + case 5: + return "kIllegalValue"; + case 6: + return "kDeviceBusy"; + case 7: + return "kDeviceException"; + case 8: + return "kChannelDeviceException"; + case 9: + return "kParameterError"; + case 10: + return "kOvertime"; + case 11: + return "kOperationInvalid"; + case 12: + return "kunsupportOperation"; + case 13: + return "kIllegalOperation"; + case 14: + return "kPermissionDenied"; + case 15: + return "koutOfRange"; + case 16: + return "kmotorNotRunToHome"; + case 17: + return "kParameterOutOfLimit"; + } + }; const connectWs = () => { const input = document.querySelector("#ws_connnect_input"); const status = document.querySelector("#status"); @@ -352,7 +391,6 @@ }; ws.onmessage = function (evt) { - console.log(evt.data); const messageWrap = document.querySelector("#message_wrap"); const div = document.createElement("div"); div.classList = ["message"]; @@ -362,7 +400,7 @@ messageWrap.scrollHeight - messageWrap.clientHeight; // 对消息进行处理 try { - const messageBody = JSON.parse(evt.data); + const { message: messageBody } = JSON.parse(evt.data); const { type, ctrlPointAddr, ctrlPointVal } = messageBody; const receiptMessage = document.querySelector("#receipt_message"); const receiptAddress = document.querySelector("#receipt_address"); @@ -377,13 +415,16 @@ if (type == "error_receipt") { receiptMessage.value = "错误"; receiptAddress.value = ctrlPointAddr; - receiptVal.value = ctrlPointVal; + // 错误回执需要吧值和英文进行对应 + receiptVal.value = transValToName(ctrlPointVal); } if (type == "report") { reportAddress.value = ctrlPointAddr; reportVal.value = ctrlPointVal; } - } catch (error) {} + } catch (error) { + console.log(error); + } }; ws.onclose = function () { @@ -416,9 +457,9 @@ protocol, message: { ...message, - targetId: id.value, - ctrlPointAddr: base.value + off.value, - ctrlPointVal: value.value, + targetId: parseInt(id.value), + ctrlPointAddr: parseInt(base.value) + parseInt(off.value), + ctrlPointVal: parseInt(value.value), type, }, });