zoukankan      html  css  js  c++  java
  • 立方门禁TCP二次开发

    一:主函数

            static Socket client; 
            static CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
            static void Main(string[] args)
            {
                //01 C4 6A F0 
                IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("192.168.1.129"), 16666);
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    //连接服务器
                    client.Connect(ipe);
                    Console.WriteLine("Connnect to {0}", client.RemoteEndPoint.ToString());
    
                    Task.Factory.StartNew(ReceiveMessage, cancelTokenSource.Token);
                    SendLogin();
                    Task.Factory.StartNew(SendHeartBeat, cancelTokenSource.Token);
                }
                catch (Exception e)
                {
                    Console.WriteLine("发生错误:"+e.Message);
                }
                Console.ReadKey();
            }

    二:接收方法

     private static void ReceiveMessage()
            {
                while (!cancelTokenSource.IsCancellationRequested)
                {
                    try 
                    {
                        byte[] buffer = new byte[1024];
                        int length = client.Receive(buffer);
                        if (length > 0)
                        {
                            StringBuilder sOutput = new StringBuilder(buffer.Length);
                            for (int i = 0; i< buffer.Length; i++)
                            {
                                sOutput.Append(buffer[i].ToString("X2"));
                            }
                            var result = ByteArrayToHexString(buffer);
                            if (result.Contains("00 00 00 13 01 00 12"))
                            {
                                Console.WriteLine("登录成功");
                            }
                            else if (result.Contains("00 00 00 13 01 00 03"))
                            {
                                Console.WriteLine("接收心跳");
                            }
                            else if (result.Contains("00 00 00 18 00 02 00"))
                            {
                                Console.WriteLine("门状态变化");
                            }
                            else if (result.Contains("00 00 00 18 00 02 01"))
                            {
                                Console.WriteLine("控制器状态");
                            }
                            else if (result.Contains("00 00 00 18 00 02 02"))
                            {
                                Console.WriteLine("刷卡记录");
                            }
                            else if (result.Contains("00 00 00 18 00 03 00"))
                            {
                                Console.WriteLine("访客刷卡");
                            }
                            else
                            {
                                string datapacket = "00000013010200000000000000000101C46AF0";
                                byte[] array = HexStringToByteArray(datapacket);
                                client.Send(array);
    
                                byte[] newByte = LongToShort(buffer); 
                                string res = Encoding.UTF8.GetString(newByte);
                                LiFangMsg lf = JsonConvert.DeserializeObject<LiFangMsg>(res);
                                Console.WriteLine(res);                  
                            } 
                        } 
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine("发生错误:" + e.Message);   
                    }
                }
            }

    三:登录方法

    private static void SendLogin()
            {
                string datapacket = "00000013000000000000000000000101C46AF0";
                byte[] array = HexStringToByteArray(datapacket);
                client.Send(array); 
            } 

    四:发送心跳保持长连接

    private static void SendHeartBeat()
            {
                while (!cancelTokenSource.IsCancellationRequested)
                {
                    string datapacket = "00000013000002000000000000000201C46AF0";
                    byte[] array = HexStringToByteArray(datapacket);
                    client.Send(array);
                    Thread.Sleep(10000);
                }           
            }

    五:数据转换方法

            public static byte[] HexStringToByteArray(string s)
            {
                s = s.Replace(" ", "");
                byte[] buffer = new byte[s.Length / 2];
                for (int i = 0; i < s.Length; i += 2)
                    buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
                return buffer;
            }
            public static string ByteArrayToHexString(byte[] data)
            {
                StringBuilder sb = new StringBuilder(data.Length * 3);
                foreach (byte b in data)
                    sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
                return sb.ToString().ToUpper();
            }
            public static byte[] LongToShort(byte[] data)
            {
                int byteLength = 0;
                int startIndex = 19;
                for (int x = startIndex; x < data.Length; x++)
                {
                    if (data[x] == 0 && data[x + 1] == 0 && data[x + 2] == 0)
                    {
                        byteLength = x;
                        break;
                    }
                }
                byte[] newByte = new byte[byteLength - startIndex];
                for (int i = 0; i < newByte.Length; i++)
                {
                    newByte[i] = data[startIndex + i];
                }
                return newByte;
            }

    六:辅助提取数据类

    public class LiFangMsg  
        {
            public int doorStatus { get; set; }  
            public string doorName { get; set; }
            public int cardType { get; set; }  
            public int doorId { get; set; }
            public string emplyName { get; set; }  
            public string eventDate { get; set; }
            public string serial { get; set; }
        } 

  • 相关阅读:
    Linq to SQL -- 入门篇
    进程和线程(线程是轻量级进程)(上)
    复制文件夹中的所有文件夹与文件到另一个文件夹
    C# 特性(Attribute)
    文件的输入与输出
    正则表达式
    预处理指令
    String类
    可空类型(Nullable)
    参数传递
  • 原文地址:https://www.cnblogs.com/HansZimmer/p/13373201.html
Copyright © 2011-2022 走看看