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);
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
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 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()));
        }