tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace TProduct.Link.Kedi
{
    /// <summary>
    /// 科迪 SI
    /// </summary>
    public class SiKdMsgHelper
    {
        /// <summary>
        /// 构建查询类
        /// </summary>
        /// <param name="startRegSet"></param>
        /// <param name="numRegSet"></param>
        /// <returns></returns>
        public static byte[] BuildQueryMsg(int startRegSet, int numRegSet)
        {
            /*
发送 02 30 31 30 30 30 30 36 03 35 41
返回 02 30 30 30 30 30 34 30 30 30 31 30 30 03 34 38 
 
解析发送
02 30 固定
31 30 30 30 开始地址
30 36 数量*2
03 //固定
35 41 校验
 
解析返回 
02 
30 30 30 30  //3412
30 34 30 30 
30 31 30 30 
03 
34 38 
 
   out(0) = &H2        '读取固定位
   out(1) = &H30
 
   out(2) = &H31      '1000+起始地址*2 转ASCII      5*2=10 对应A    +1000  =100A   转 31 30 30 41
   out(3) = &H30
   out(4) = &H30
   out(5) = &H30
 
   out(6) = &H30       '寄存器个数  读一个的话 都要*2   读10个 20 就是14 就是31 34
   out(7) = &H36
 
   out(8) = &H3        '固定位
            */
 
 
 
            byte[] commandSend = new byte[11];
            commandSend[0] = 0x02;
            commandSend[1] = 0x30;
 
            //开始位 '1000+起始地址*2 转ASCII      5*2=10 对应A    +1000  =100A   转 31 30 30 41
            var bStartRegSet = ToByte2(startRegSet * 2);
            var sStartRegSet = BitConverter.ToString(bStartRegSet, 0, bStartRegSet.Length);
            var split_sStartRegSet = sStartRegSet.Split('-');
            var h_startRegtSet = ToAsciiByte(split_sStartRegSet[1]);
            var l_startRegtSet = ToAsciiByte(split_sStartRegSet[0]);
            commandSend[2] = 0x31;
            commandSend[3] = h_startRegtSet[0];
            commandSend[4] = l_startRegtSet[1];
            commandSend[5] = l_startRegtSet[0];
 
            //寄存器个数  读一个的话 都要*2   读10个 20 就是14 就是31 34
            var bNumRegSet = ToByte2(numRegSet * 2);
            var sNumRegSet = BitConverter.ToString(bNumRegSet, 0, bNumRegSet.Length);
            var split_sNumRegSet = sNumRegSet.Split('-');
            var l_numRegSet = ToAsciiByte(split_sNumRegSet[0]);
            commandSend[6] = l_numRegSet[1];
            commandSend[7] = l_numRegSet[0];
 
            commandSend[8] = 0x03;
            long num = 0;
            for (int i = 1; i <= 8; i++)
            {
                num += commandSend[i];
            }
            long hex_h; long hex_l;
            CreateCRC(num, out hex_h, out hex_l);
 
            commandSend[9] = Convert.ToByte(hex_h);
            commandSend[10] = Convert.ToByte(hex_l);
 
            return commandSend;
        }
 
 
        /// <summary>
        /// 抽取值
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <param name="valveIndex">从0开始</param>
        /// <returns></returns>
        public static int AbstactByteValue(IEnumerable<byte> byteMessage, int valveIndex)
        {
            int m1, m2, m3, m4;
            m1 = byteMessage.ElementAt(1 + valveIndex * 4) - 48;
            m2 = byteMessage.ElementAt(2 + valveIndex * 4) - 48;
            m3 = byteMessage.ElementAt(3 + valveIndex * 4) - 48;
            m4 = byteMessage.ElementAt(4 + valveIndex * 4) - 48;
 
            m1 = (m1 / 16) * 10 + (m1 % 16);
            m2 = (m2 / 16) * 10 + (m2 % 16);
            m3 = (m3 / 16) * 10 + (m3 % 16);
            m4 = (m4 / 16) * 10 + (m4 % 16);
 
            if (m1 >= 11) m1 = m1 - 1;
            if (m2 >= 11) m2 = m2 - 1;
            if (m3 >= 11) m3 = m3 - 1;
            if (m4 >= 11) m4 = m4 - 1;
 
            return m3 * (16 ^ 3) + m4 * (16 ^ 2) + m1 * 16 + m2;
        }
 
        private static byte[] ToByte4(int u)
        {
            byte[] b = new byte[4];
            b[0] = (byte)(u);
            b[1] = (byte)(u >> 8);
            b[2] = (byte)(u >> 16);
            b[3] = (byte)(u >> 24);
 
            return b;
        }
        private static byte[] ToByte2(int u)
        {
            byte[] b = new byte[2];
            b[0] = (byte)(u);
            b[1] = (byte)(u >> 8);
 
            return b;
        }
 
        private static byte[] ToAsciiByte(string b)
        {
            byte[] bb = new byte[2];
            bb[0] = ToCharByte(b[1]);//反过来
            bb[1] = ToCharByte(b[0]);
            return bb;
        }
        public static byte[] IntToBytes4(int value)
        {
            byte[] src = new byte[4];
            src[3] = (byte)((value >> 24) & 0xFF);
            src[2] = (byte)((value >> 16) & 0xFF);
            src[1] = (byte)((value >> 8) & 0xFF);
            src[0] = (byte)(value & 0xFF);
            return src;
        }
        public static byte[] IntToBytes2(int value)
        {
            byte[] src = new byte[2]; 
            src[1] = (byte)((value >> 8) & 0xFF);
            src[0] = (byte)(value & 0xFF);
            return src;
        }
        /// <summary>
        /// 按 3412的结构把b换算
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static byte[] ToAsciiByte4(int val)
        {
            byte[] b_arr = new byte[2];
            b_arr[0] = (byte)((val >> 8) & 0xFF);
            b_arr[1] = (byte)(val & 0xFF);
     
            var sss = BitConverter.ToString(b_arr, 0, b_arr.Length);
 
            return new byte[4] { 
                ToCharByte(sss[3]), 
                ToCharByte(sss[4]),//2 是 -
                ToCharByte(sss[0]),
                ToCharByte(sss[1]) }; 
        }
 
           
        public static byte ToCharByte(char b)
        {
            switch (b)
            {
                case '0':
                    return 0x30;
                case '1':
                    return 0x31;
                case '2':
                    return 0x32;
                case '3':
                    return 0x33;
                case '4':
                    return 0x34;
                case '5':
                    return 0x35;
                case '6':
                    return 0x36;
                case '7':
                    return 0x37;
                case '8':
                    return 0x38;
                case '9':
                    return 0x39;
                case 'A': //A
                case 'a':
                    return 0x41;
                case 'B'://B
                case 'b':
                    return 0x42;
                case 'C'://C
                case 'c':
                    return 0x43;
                case 'D'://D
                case 'd':
                    return 0x44;
                case 'E'://E
                case 'e':
                    return 0x45;
                case 'F'://F
                case 'f':
                    return 0x46;
            }
 
            return 0x30;
        }
 
        /// <summary>
        /// 产生校验码
        /// </summary>
        /// <param name="num"></param>
        /// <param name="hex_h"></param>
        /// <param name="hex_l"></param>
        public static void CreateCRC(long num, out long hex_h, out long hex_l)
        {
            /*
        If num >= 256 Then num = num Mod 256
    hex_h = num \ 16: hex_l = num Mod 16
    If hex_h >= 10 Then hex_h = hex_h + 1: hex_h = (hex_h \ 10) * 16 + (hex_h Mod 10)
    If hex_l >= 10 Then hex_l = hex_l + 1: hex_l = (hex_l \ 10) * 16 + (hex_l Mod 10)
    hex_h = 48 + hex_h: hex_l = 48 + hex_l                
             */
 
            if (num >= 256)
            {
                num = num % 256;
            }
 
            hex_h = num / 16;
            hex_l = num % 16;
 
 
            if (hex_h >= 10)
            {
                hex_h = hex_h + 1;
                hex_h = (hex_h / 10) * 16 + (hex_h % 10);
            }
            if (hex_l >= 10)
            {
                hex_l = hex_l + 1;
                hex_l = (hex_l / 10) * 16 + (hex_l % 10);
            }
            hex_h = 48 + hex_h;
            hex_l = 48 + hex_l;
        }
 
    }
}