Пример #1
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;
        }
Пример #2
0
        static void Main(string[] args)
        {
            // var s = "0xFF, 0xAA, 0x3E, 0x00, 0x3C, 0xBB, 0x7A, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x32, 0x31, 0x34, 0x30, 0x39, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x04, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x32, 0x00, 0x01, 0x00, 0x00, 0x00";

            var s   = "FF AA 50 00 AD 16 F0 60 00 00 00 00 00 00 00 00 33 32 31 34 30 39 30 30 30 30 30 33 04 00 09 00 26 00 00 00 00 00 4C 54 4C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 AC FD CE B3 56 FE 01 00 00 00";
            var arr = s.Replace("0x", "").Replace("  ", " ").Trim().Split(" ,".ToArray(), StringSplitOptions.RemoveEmptyEntries).Select(o => byte.Parse(o, NumberStyles.AllowHexSpecifier)).ToArray();
            var p   = new TPKGHead();
            var b   = p.Parse(arr);
            // string requestText = "{\"Serv_Code2\":1,\"Equip_Id\":\"12345678\",\"Data\":50,\"Type\":2,\"Serv_Type\":6,\"Serv_Code\":1}";

            //  SensorNetwork.Common.ZeroMQ.ZMQRequest("tcp://192.168.0.128:56789", requestText);
            // SensorNetwork.Common.ZeroMQ.ZMQRequest("tcp://202.103.209.2:6789", requestText);


            //传感器硬件设备数据主动上报
            //string strHex = "FF AA 53 00 08 3A 31 04 00 00 00 00 00 00 00 00 33 32 31 34 30 39 30 30 30 30 30 30 06 00 05 00 26 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0E 0E 11 5F C4 01 00 53 4E 06 00 04 03 00 17 24 00 00 00 00";
            string strHex = "FF AA 5B 00 99 66 5F 9F 00 00 00 00 00 00 00 00 33 32 31 34 30 39 30 30 30 30 30 30 06 00 05 00 26 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0E 0E 11 5F C4 11 00 53 4E 0E 00 04 04 0F 06 11 13 28 00 2E 00 01 41 E7 02 00 00 00";


            //login
            //  string strHex = "FF AA 6E 00 8F D7 1D EC 00 00 00 00 00 00 00 00 33 32 31 34 30 39 30 30 30 30 30 30 02 00 01 00 20 00 00 00 00 00 33 32 31 34 6D 79 6E 61 6D 65 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6D 79 70 61 73 73 77 6F 72 64 00 00 00 00 00 00 6D 79 41 70 70 55 73 65 72 49 44 00 00 00 00 00 01 00 00 00";
            // string strHex = "FF AA 3D 00 08 9C 96 C9 00 00 00 00 00 00 00 00 33 32 31 34 30 39 30 30 30 30 30 30 06 00 01 00 20 00 00 00 00 00 31 32 33 34 35 36 37 38 00 00 00 00 00 00 00 00 1E 00 00 01 00 00 00";
            string strHex2 = strHex;

            strHex = strHex.Replace(" ", "");
            Console.WriteLine("Frame_Head:FFAA");//HexToStr(strHex.Substring(0,2*2))
            Console.WriteLine("Total_Len:" + HexToInt32(strHex.Substring(4, 2 * 2).TrimEnd('0')));
            Console.WriteLine("CRC32:" + HexToInt32(strHex.Substring(8, 4 * 2).TrimEnd('0')));
            Console.WriteLine("Seq_Id:" + HexToInt32(strHex.Substring(16, 4 * 2).TrimEnd('0')));
            Console.WriteLine("Comm_Type:" + HexToInt32(strHex.Substring(24, 1 * 2)));
            Console.WriteLine("Enc_Type:" + HexToInt32(strHex.Substring(26, 1 * 2)));
            Console.WriteLine("Major_Ver:" + HexToInt32(strHex.Substring(28, 1 * 2).TrimEnd('0')));
            Console.WriteLine("Minor_Ver:" + HexToInt32(strHex.Substring(30, 1 * 2).TrimEnd('0')));
            Console.WriteLine("Term_Code:" + HexToStr(strHex.Substring(32, 12 * 2)));
            Console.WriteLine("Serv_Type:" + HexToInt32(strHex.Substring(56, 2 * 2).TrimEnd('0')));
            Console.WriteLine("Serv_Code:" + HexToInt32(strHex.Substring(60, 2 * 2).TrimEnd('0')));
            Console.WriteLine("Flags:" + HexToInt32(strHex.Substring(64, 1 * 2)));
            Console.WriteLine("Priority:" + HexToInt32(strHex.Substring(66, 1 * 2)));
            Console.WriteLine("Reserved:" + HexToStr(strHex.Substring(68, 4 * 2)));

            //#region 用户登录
            //Console.WriteLine("Cloud_Code:" + HexToStr(strHex.Substring(76, 4 * 2)));
            //Console.WriteLine("Username:"******"Password:"******"AppUserID:" + HexToStr(strHex.Substring(180, 16 * 2)));
            //#endregion
            #region 蓝牙传感设备数据(主动上报)
            Console.WriteLine("Equip_UID:" + HexToStr(strHex.Substring(76, 24 * 2)));
            Console.WriteLine("Equip_MAC:" + strHex.Substring(124, 6 * 2));
            Console.WriteLine("Frame_Num:" + HexToInt32(strHex.Substring(136, 2 * 2).TrimEnd('0')));
            //   Console.WriteLine("Equ_Data:" + HexToStr(strHex.Substring(140, 9*2)));//53 4E 06 00 04 03 00 17 24
            #endregion


            //  string strHex = "48 65 6C 6C 6F 20 57 6F 72 6C 64 21";
            //string[] hexValuesSplit = strHex2.Split(' ');
            //foreach (String hex in hexValuesSplit)
            //{
            //    // Convert the number expressed in base-16 to an integer.
            //    int value = Convert.ToInt32(hex, 16);
            //    // Get the character corresponding to the integral value.
            //    string stringValue = Char.ConvertFromUtf32(value);
            //    char charValue = (char)value;
            //    Console.WriteLine("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}", hex, value, stringValue, charValue);
            //}

            // Console.WriteLine(HexToStr(strHex));


            SocketSend("www.gosafenet.net", 20000, strHex);


            Console.ReadLine();
        }
Пример #3
0
        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 { }
        }
        /// <summary>
        /// 开始接收网关上传的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void socketServer_OnDataReceived(object sender, ReceivedEventArgs <byte[]> e)
        {
            AsyncSocketConnection conn = (AsyncSocketConnection)sender;
            TPKGHead packet            = new TPKGHead();
            bool     isOk = false;

            try
            {
                isOk = packet.Parse(e.Data);//解析收到的数据包
                log.Info($"SensorNetwork.Server.Management.Framework中接收的包,Serv_Type:" + packet.Serv_Type + ",Serv_Code:" + packet.Serv_Code + "包数据域Data:\r\n{" + packet.Data.ToBytes().ToHexString() + "}");
                if (isOk == false)
                {
                    log.Error($"包解析错误:\r\n{BitConverter.ToString(e.Data).Replace("-", " ")}");
                }
                else
                {
                    log.Info($"包解析成功(16进制):\r\n{BitConverter.ToString(e.Data).Replace("-", " ")}");
                    log.Info($"包数据域:\r\n{packet.Data.ToString()}");
                }

                bool isNew = isOk && string.IsNullOrWhiteSpace(conn.TerminalId) && string.IsNullOrWhiteSpace(packet.Term_Code) == false;
                if (isNew)
                {
                    AddLog(packet.Term_Code, "设备已连接。");
                }
                if (string.IsNullOrWhiteSpace(packet.Term_Code) == false)
                {
                    conn.TerminalId             = packet.Term_Code;
                    lastAlive[packet.Term_Code] = Tuple.Create(conn.Token, DateTime.Now);
                    //if (conn.Protocol == null)
                    //    conn.Protocol = ProtocolManager.Find(packet.Term_Code);
                    var s = Newtonsoft.Json.JsonConvert.SerializeObject(packet.Data);
                    AddLog(packet.Term_Code, packet.Data, "收到数据包:Data:{0}", packet.Data.ToString());
                }
                Interlocked.Increment(ref receivedMessageCount);
                conn.LastAlive = DateTime.Now;//更新连接的最后活动时间
                //else
                //{
                //    var json = Newtonsoft.Json.JsonConvert.SerializeObject(packet.Data, Newtonsoft.Json.Formatting.Indented);
                //    log.DebugFormat("{0}\nMACID: {1}\n{2}", packet., packet.Id, json);
                //}


                if (isOk)                                         //如果解析成功
                {
                    ackQueue.Enqueue(Tuple.Create(conn, packet)); //将接收到的数据包放到队列中
                    if (isNew)                                    //如果是一条新的连接,则创建一条新的连接写入DB中
                    {
                        newConnections.Enqueue(new TerminalConnectionLog()
                        {
                            id           = conn.Token,//guid
                            connected    = conn.Created,
                            ipaddress    = conn.RemoteEndPoint.ToString(),
                            server_id    = ServerId,
                            terminal_id  = conn.TerminalId,
                            disconnected = conn.Created,
                        });
                        conn.Closed += AsyncSocketConnection_Closed;
                        log.DebugFormat("new client: {0}", conn.TerminalId);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error($"包解析/处理异常:\r\n{BitConverter.ToString(e.Data).Replace("-", " ")}");
                log.Error(ex);
            }
        }