Tjiny
2025-05-20 74938606dd1dd8d7d77f053492b987f96a867338
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
using System;
using System.Linq;
using System.Text;
 
namespace HH.WCS.Mobox3.Template.Util.EquipmentCommunication
{
    internal class PlcHelper
    {
        internal static void Receive(string ip, string msg) {
            //处理设备信号
            // DeviceProcess.Analysis(msg, ip);
        }
        internal static bool SendHex(string ip, string 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 bool CheckCRC(string hex) {
            var result = false;
            var data = hex.Replace(" ", "");
            if (data.Length % 2 == 0) {
                var code1 = data.Substring(data.Length - 4, 4).ToLower();
                var code2 = BitConverter.ToString(CRC16LH(Hex2Bytes(data.Substring(0, data.Length - 4)))).Replace("-", "").Replace(" ", "").ToLower();
                result = code1 == code2;
            }
            return result;
        }
        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
    }
}