public IotCommand(string command, string dataHex, byte[] data = null) { Name = command; DataHex = dataHex; Data = data ?? EncodingHelper.HexToBytes(dataHex); Value = NaryHelper.HexToInt(command); }
private object GetParamValue(string dataHex, IotParam param) { try { var length = param.Type == "C" ? 1 : param.Length; var val = dataHex.Substring(2 * param.Start, 2 * length); if (val != "EEEE" && val != "EE") { switch (param.Type) { case "I": return(NaryHelper.YmHexToInt(val)); case "B": return(NaryHelper.HexToInt(val) > 0); case "C": return(BinaryHelper.GetHexBit(val, param.Length)); default: return(NaryHelper.YmHexToDouble(val)); } } return("异常"); } catch { return(null); } }
private async void btnConn_Click(object sender, EventArgs e) { this.btnConn.Enabled = false; this.Cursor = Cursors.WaitCursor; filter = new IotReceiveFilter(header.Text.Trim(), Convert.ToInt32(key.Text.Trim(), 16)); client = new EasyClient(); client.Closed += (s, arg) => Output("Client-Conn: disonnected from server."); client.Error += (s, arg) => Output($"Error: {arg.Exception.GetMessage()}"); client.Initialize(filter, req => { Output($"Rece-加密数据: {req.HeaderHex}-{req.Length.ToString("{00}")} {req.EncryptedBody.ToHex()}"); Output($"Rece-解密数据: {req.HeaderHex}-{req.Length.ToString("{00}")} {req.BodyHex}"); Output($"Rece-拆包解验: {req.HeaderHex}-{req.Length.ToString("{00}")} {req.Device.Mac}-{req.SessionId} {req.Command} {req.Crc16}"); if (req.Command.IsResponse) { var cmd = req.Command.Name; if (cmd == "80F1") { if (req.Command.DataHex.StartsWith("00")) { sessions[req.Device.Mac] = req.SessionId; Output($"Client-Login: Success with SessionId-{req.SessionId}"); } else { Output($"Client-Login: Fail with response Code-{req.Command.DataHex}"); } } } else { var body = ""; switch (req.Command.Name) { case "00A1": SetBox(txtOn, NaryHelper.HexToInt(req.Command.DataHex).ToString()); break; case "00A2": SetBox(txtSpeed, NaryHelper.HexToInt(req.Command.DataHex).ToString()); break; case "00A3": SetBox(txtMode, NaryHelper.HexToInt(req.Command.DataHex).ToString()); break; case "00A7": var type = req.Command.DataHex.Substring(0, 2); if (type == "01") { body = int.Parse(txtNetDay.Text).ToYmHex(4); } else if (type == "02") { SetBox(txtNetDay, NaryHelper.HexToInt(req.Command.DataHex.Substring(2)).ToString()); } else { txtNetDay.Text = "200"; } break; case "00AB": SetBox(txtHumSet, NaryHelper.HexToInt(req.Command.DataHex).ToString()); break; case "00AD": type = req.Command.DataHex.Substring(0, 2); if (type == "01") { body = txtMenu.Text.Trim(); } else { SetBox(txtMenu, req.Command.DataHex.Substring(2)); } break; case "00AE": type = req.Command.DataHex.Substring(0, 2); if (type == "01") { body = txtTime.Text.Trim(); } else { SetBox(txtTime, req.Command.DataHex.Substring(2)); } break; case "00AF": type = req.Command.DataHex.Substring(0, 2); if (type == "01") { body = txtShuang.Text.Trim(); } else { SetBox(txtShuang, req.Command.DataHex.Substring(2)); } break; } var res = req.CreateResponse("00" + body); Send(res); Send(); } }); var ip = IPAddress.Parse(txtIP.Text.Trim().Split(':')[0]); var port = int.Parse(txtIP.Text.Trim().Split(':')[1]); if (await client.ConnectAsync(new IPEndPoint(ip, port))) { Output($"Connect to server [{txtIP.Text.Trim()}] success!"); } else { Output($"Connect to server [{txtIP.Text.Trim()}] fail!"); } this.Cursor = Cursors.Default; this.btnConn.Enabled = true; }