|
@@ -10,7 +10,7 @@ namespace Jdas_Mbic
|
10
|
10
|
|
11
|
11
|
class Crc16
|
12
|
12
|
{
|
13
|
|
- private readonly ushort[] Table_CRC16 = {
|
|
13
|
+ private ushort[] Table_CRC16 = {
|
14
|
14
|
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
|
15
|
15
|
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
|
16
|
16
|
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
|
|
@@ -53,74 +53,32 @@ namespace Jdas_Mbic
|
53
|
53
|
//-----------------------------------------------
|
54
|
54
|
//UART CRC üũ �Լ�
|
55
|
55
|
//-----------------------------------------------
|
56
|
|
- public ushort CRC16_Generate(byte[] buf_ptr, int len)
|
|
56
|
+ /*public ushort crc16(byte[] crcdata, uint len)
|
57
|
57
|
{
|
58
|
|
- byte dt = 0;
|
59
|
|
- ushort crc16 = 0;
|
60
|
|
- ushort index = 4;
|
61
|
|
- len *= 8;
|
62
|
|
- for (crc16 = (ushort)0x0000; len >= 8; len -= 8)
|
63
|
|
- {
|
64
|
|
- crc16 = (ushort)(Table_CRC16[(crc16 >> 8) ^ (ushort)(buf_ptr[index++])] ^ (crc16 << 8));
|
65
|
|
- }
|
66
|
|
-
|
67
|
|
- if (len != 0)
|
68
|
|
- {
|
69
|
|
- dt = (byte)( buf_ptr[index] << 8);
|
|
58
|
+ ushort crc = 0;
|
|
59
|
+ for (int counter = 0; counter < len; counter++)
|
|
60
|
+ crc = (crc << 8) ^ Table_CRC16[(((crc >> 8) ^ crcdata[counter]) & 0x00FF)];
|
|
61
|
+ return crc;
|
|
62
|
+ }*/
|
|
63
|
+
|
70
|
64
|
|
71
|
|
- while (len != 0)
|
72
|
|
- {
|
73
|
|
- len--;
|
|
65
|
+ public bool CRC16_Check(byte[] buf_ptr, int len,ushort checksum)
|
|
66
|
+ {
|
|
67
|
+ ushort crc = 0;
|
|
68
|
+ for (int counter = 0; counter < len; counter++)
|
|
69
|
+ crc = (ushort)((crc << 8) ^ Table_CRC16[(int)(((crc >> 8) ^ buf_ptr[counter]) & 0x00FF)]);
|
74
|
70
|
|
75
|
|
- if (((crc16 ^ dt) & ((ushort)1 << 15)) != 0)
|
76
|
|
- {
|
77
|
|
- crc16 = (ushort)(crc16 << 1);
|
78
|
|
- crc16 = (ushort)(crc16 ^ 0x1021);
|
79
|
|
- }
|
80
|
|
- else
|
81
|
|
- {
|
82
|
|
- crc16 = (ushort)(crc16 << 1);
|
83
|
|
- }
|
84
|
|
- dt = (byte)(dt << 1);
|
85
|
|
- }
|
86
|
|
- }
|
87
|
|
- return (crc16);
|
|
71
|
+ return (crc == checksum ? false : true);
|
88
|
72
|
}
|
89
|
|
-
|
90
|
|
- public EtError CRC16_Check(byte[] buf_ptr, int len, ushort checksum)
|
|
73
|
+ public ushort CRC16_Generate(byte[] buf_ptr, int len)
|
91
|
74
|
{
|
92
|
|
- byte dt = 0;
|
93
|
|
- ushort crc16 = 0;
|
94
|
|
- ushort index = 4;
|
95
|
|
-
|
96
|
|
- len *= 8;
|
97
|
|
- for (crc16 = (ushort)0x0000; len >= 8; len -= 8, index++)
|
98
|
|
- {
|
99
|
|
- crc16 = (ushort)(Table_CRC16[(crc16 >> 8) ^ (ushort)(buf_ptr[index])] ^ (crc16 << 8));
|
100
|
|
- }
|
101
|
|
-
|
102
|
|
- if (len != 0)
|
103
|
|
- {
|
104
|
|
- dt = (byte)(buf_ptr[index] << 8);
|
105
|
|
-
|
106
|
|
- while (len != 0)
|
107
|
|
- {
|
108
|
|
- len--;
|
|
75
|
+ ushort crc = 0;
|
|
76
|
+ for (int counter = 0; counter < len; counter++)
|
|
77
|
+ crc = (ushort)((crc << 8) ^ Table_CRC16[(int)(((crc >> 8) ^ buf_ptr[counter]) & 0x00FF)]);
|
109
|
78
|
|
110
|
|
- if (((crc16 ^ dt) & ((ushort)1 << 15)) != 0)
|
111
|
|
- {
|
112
|
|
- crc16 = (ushort)(crc16 << 1);
|
113
|
|
- crc16 = (ushort)(crc16 ^ 0x1021);
|
114
|
|
- }
|
115
|
|
- else
|
116
|
|
- {
|
117
|
|
- crc16 = (ushort)(crc16 << 1);
|
118
|
|
- }
|
119
|
|
- dt = (byte)(dt << 1);
|
120
|
|
- }
|
121
|
|
- }
|
122
|
|
- return (crc16 == checksum ? EtError.CHECKSUM_ERROR : EtError.NO_ERROR);
|
|
79
|
+ return crc;
|
123
|
80
|
}
|
|
81
|
+
|
124
|
82
|
public bool STH30_CheckCrc(byte[] data, byte nbrOfBytes, byte checksum)
|
125
|
83
|
{
|
126
|
84
|
byte bit; // bit mask
|