public TPKGHead SendAndWaitTerminalResponse(TPKGHead data) { //log.Info($"向网关发送请求,Get其连接的蓝牙设备数据包1:{data.ToBytes().ToHexString()}"); if (data == null) { throw new Exception("数据不能等于空。"); } if (data.SessionId == null) { data.SetNextSessionId(); } var c = Server.GetConnectionByTerminalId(data.Term_Code); if (c == null) { throw new Exception("该设备不在线。"); } c.SendAsync(data.ToBytes()); log.Info($"向网关发送请求,Get其连接的蓝牙设备数据包2:"); log.Info($"{data.ToBytes().ToHexString()}"); var resp = WaitTerminalResponse(data.Term_Code, data.SessionId); if (resp == null) { throw new Exception("超时未响应。"); } return(resp); }
public Form1() { InitializeComponent(); txtId.Text = uid; var packet = new TPKGHead() { Term_Code = uid, Data = new SensorNetwork.Protocol.Packet.PQRY.SensPacketData() { Equip_Id = "Equip_Id", Temperature = 200, Humidity = 300 }, }; packet.SetNextSessionId(); var b = packet.ToBytes(); var r = string.Join(Environment.NewLine, BitConverter.ToString(b).Split('-') .Select((o, i) => new { o, i = i / 16 }) .GroupBy(o => o.i).Select(o => string.Join(" ", o.Select(v => v.o)))); var x = packet.Parse(b); int b2 = x ? 1 : 0; }
private void btnConnect_Click(object sender, EventArgs e) { if (client == null) { try { client = new TcpClient(txtIPAddress.Text, (int)numPort.Value); th = new Thread(Working); th.Start(); var packet = new TPKGHead() { Term_Code = uid, Data = new SensorNetwork.Protocol.Packet.LINK.LoginPacketData() { Cloud_Code = uid, Username = "******", Password = "******" }, }; packet.SetNextSessionId(); var bytes = packet.ToBytes(); client.GetStream().Write(bytes, 0, bytes.Length); packet = new TPKGHead() { Term_Code = uid, Data = new SensorNetwork.Protocol.Packet.PQRY.SensPacketData() { Temperature = (ushort)(numTemp.Value / 10) }, }; packet.SetNextSessionId(); bytes = packet.ToBytes(); client.GetStream().Write(bytes, 0, bytes.Length); txtIPAddress.Enabled = numPort.Enabled = false; btnConnect.Text = "断开(&D)"; } catch (Exception ex) { client = null; MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { } } else { isWorking = false; txtIPAddress.Enabled = numPort.Enabled = true; btnConnect.Text = "连接(&C)"; } }
public bool SendAsync(TPKGHead msg) { if (SessionId2 > 0 && !(msg.SessionId > 0)) { msg.SessionId = SessionId2; msg.Flags |= TPKGHeadFlags.SIDV; } var buffer = msg.ToBytes(); return(SendAsync(buffer)); }
public async Task <ApiResult <byte[]> > Test() { var p = new TPKGHead() { Term_Code = "321409000001", Data = new SensorNetwork.Protocol.Packet.LINK.LoginPacketData() { AppUserID = "gaohan", Cloud_Code = "3214", Username = "******", Password = "******" } }; return(BitConverter.ToString(p.ToBytes()).Replace("-", " ")); }
public bool SendAsync(TPKGHead data) { if (data == null) { throw new Exception("数据不能等于空。"); } if (data.SessionId == null) { data.SetNextSessionId(); } var c = Server.GetConnectionByTerminalId(data.Term_Code); if (c == null) { throw new Exception("该设备不在线。"); } return(c.SendAsync(data.ToBytes())); }
private void timer1_Tick(object sender, EventArgs e) { var value = Math.Round((rnd.NextDouble() * 10 + 20), 2); numTemp.Value = (decimal)value; try { if (bw != null) { var packet = new TPKGHead() { Term_Code = uid, Data = new SensorNetwork.Protocol.Packet.PQRY.SensPacketData() { Temperature = (ushort)(value * 10), Humidity = 971, }, }; packet.ToBytes(bw); } } catch { } }
public void Working() { Func <double> getTemp = () => (double)numTemp.Value; Func <double, double> setTemp = (v) => { if (v != 0) { if (chkRandom.Checked) { chkRandom.Checked = false; } numTemp.Value = (decimal)v; } else { if (chkRandom.Checked == false) { chkRandom.Checked = true; } } return(v); }; isWorking = true; TPKGHead packet; try { var ns = client.GetStream(); BinaryReader br = new BinaryReader(ns); bw = new BinaryWriter(ns); while (isWorking) { if (client.Available == 0) { Thread.Sleep(100); continue; } packet = new TPKGHead(); if (packet.Parse(br)) { if ((packet.Data as SensPacketData)?.Temperature > 0) { try { var value = (packet.Data as SensPacketData)?.Temperature / 10.0; this.Invoke(setTemp, value); } catch { } } var tmp = (double)this.Invoke(getTemp); packet.Data = new SensPacketData() { Temperature = (ushort)(tmp * 10) }; packet.ToBytes(bw); } } ; } catch { } try { client.Close(); client = null; bw = null; } catch { } }