立方门禁TCP二次开发

Posted hanszimmer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了立方门禁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; }
    } 

 

技术图片

 

以上是关于立方门禁TCP二次开发的主要内容,如果未能解决你的问题,请参考以下文章

Java二次开发海康SDK-对接门禁机

实时控制软件第二次作业--停车场门禁控制系统状态机

为啥模板测试不丢弃片段?

代码门禁

全双工物联网远程控制门禁设计方案

简易全双工物联网远程控制门禁设计方案