1
pulg
4 天以前 dc600c6298a3231e05875813ec0fa2fa18151d84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using HH.WCS.QingXigongchang.process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HH.WCS.QingXigongchang.device
{
    internal class PlcHelper
    {
        //public static List<string> ipList = new List<string>();
        public static Dictionary<string, bool> ipList = new Dictionary<string, bool>();
        public static object ojbq = new object();
        internal static void Receive(string ip, string msg)
        {
            //处理设备信号
            try
            {
                lock (ojbq)
                {
                    if (ipList.ContainsKey(ip))
                    {
                        if (ipList.TryGetValue(ip, out bool re))
                        {
                            if (re)
                                return;
                            else
                                ipList[ip] = true;
                        }
                        else return;
                    }
                    else
                    {
                        ipList.Add(ip, true);
                    }
                }
                DeviceProcess.Analysis(msg, ip);
                ipList[ip] = false;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message + "{" + $"{ip}-{msg}" + "}", ex);
                ipList[ip] = false;
            }
        }
        internal static bool SendHex(string ip, string msg)
        {
            LogHelper.Info("SendHex:" + ip + "->" + msg);
            return TcpServer.TcpServerSend(ip, Hex2Bytes(msg));
 
        }
        internal static void SendAscii(string ip, string msg)
        {
            TcpServer.TcpServerSend(ip, Encoding.ASCII.GetBytes(msg));
        }
 
        internal static byte[] Hex2Bytes(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
 
            return returnBytes;
        }
        internal static string Hex2Ascii(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
 
            return Encoding.ASCII.GetString(returnBytes);
        }
 
        #region 进制转换+CRC
        internal static byte[] CRC16LH(byte[] pDataBytes)
        {
            ushort crc = 0xffff;
            ushort polynom = 0xA001;
 
            for (int i = 0; i < pDataBytes.Length; i++)
            {
                crc ^= pDataBytes[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x01) == 0x01)
                    {
                        crc >>= 1;
                        crc ^= polynom;
                    }
                    else
                    {
                        crc >>= 1;
                    }
                }
            }
 
            byte[] result = BitConverter.GetBytes(crc);
            return result;
        }
        internal static byte[] CRC16HL(byte[] pDataBytes)
        {
            ushort crc = 0xffff;
            ushort polynom = 0xA001;
 
            for (int i = 0; i < pDataBytes.Length; i++)
            {
                crc ^= pDataBytes[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x01) == 0x01)
                    {
                        crc >>= 1;
                        crc ^= polynom;
                    }
                    else
                    {
                        crc >>= 1;
                    }
                }
            }
 
            byte[] result = BitConverter.GetBytes(crc).Reverse().ToArray();
            return result;
        }
 
        #endregion
    }
}