forked from GavinYellow/SharpSCADA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionMethods.cs
More file actions
811 lines (755 loc) · 31.9 KB
/
ExtensionMethods.cs
File metadata and controls
811 lines (755 loc) · 31.9 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
using System;
using System.Text;
using System.Net;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
namespace DataService
{
public static class ExtMethods
{
public static string GetExceptionMsg(this Exception e)
{
string err = string.Empty;
Exception exp = e;
while (exp != null)
{
err += string.Format("\n {0}", exp.Message);
exp = exp.InnerException;
}
err += string.Format("\n {0}", e.StackTrace);
return err;
}
public static bool ModifyItemName(this ITag tag, string name)
{
IDataServer server = tag.Parent.Server;
lock (server.SyncRoot)
{
int index = server.GetItemProperties(tag.ID);
if (index < 0) return false;
var meta = server.MetaDataList[index];
if (meta.Name == name) return true;
server.MetaDataList[index] = new TagMetaData(meta.ID, meta.GroupID, name, meta.Address, meta.DataType, meta.Size, meta.Archive, meta.Maximum, meta.Minimum, meta.Cycle);
server.RemoveItemIndex(meta.Name);
server.AddItemIndex(name, tag);
return true;
}
}
public static SubCondition FindSubConditon(this IAlarmServer server, string sourceName, SubAlarmType alarmType)
{
var conds = server.QueryConditions(sourceName);
if (conds == null) return SubCondition.Empty;
foreach (ICondition cond in conds)
{
SubCondition sub = cond.FindSubConditon(alarmType);
if (sub.SubAlarmType == alarmType)
return sub;
}
return SubCondition.Empty;
}
public static SubCondition FindSubConditon(this ICondition cond, SubAlarmType alarmType)
{
var subs = cond.SubConditions;
if (subs != null && subs.Count > 0)
{
foreach (var sub in subs)
{
if (sub.SubAlarmType == alarmType)
{
return sub;
}
}
}
return SubCondition.Empty;
}
public static bool HasScaling(this IDataServer server, string tagName)
{
ITag tag = server[tagName];
if (tag == null) return false;
int scaleid = server.GetScaleByID(tag.ID);
return scaleid >= 0;
}
public static bool HasAlarm(this IDataServer dserver, string sourceName)
{
IAlarmServer server = dserver as IAlarmServer;
if (server == null) return false;
List<ICondition> conds = server.ConditionList as List<ICondition>;
return conds == null || conds.Count == 0 ? false : conds.BinarySearch(new DigitAlarm(0, sourceName)) >= 0;
}
public static bool HasSubCondition(this IDataServer dserver, string sourceName, SubAlarmType alarmType)
{
IAlarmServer server = dserver as IAlarmServer;
if (server == null) return false;
var conds = server.QueryConditions(sourceName);
if (conds == null) return false;
foreach (ICondition cond in conds)
{
var subs = cond.SubConditions;
if (subs != null && subs.Count > 0)
{
foreach (var sub in subs)
{
if (sub.SubAlarmType == alarmType)
{
return true;
}
}
}
}
return false;
}
public static ItemData<object> ReadValueEx(this IReaderWriter reader, DeviceAddress address)
{
switch (address.VarType)
{
case DataType.BOOL:
var bit = reader.ReadBit(address);
return new ItemData<object>(bit.Value, bit.TimeStamp, bit.Quality);
case DataType.BYTE:
var bt = reader.ReadByte(address);
return new ItemData<object>(bt.Value, bt.TimeStamp, bt.Quality);
case DataType.WORD:
case DataType.SHORT:
var sh = reader.ReadInt16(address);
return new ItemData<object>(sh.Value, sh.TimeStamp, sh.Quality);
case DataType.TIME:
case DataType.INT:
var it = reader.ReadInt32(address);
return new ItemData<object>(it.Value, it.TimeStamp, it.Quality);
case DataType.FLOAT:
var fl = reader.ReadFloat(address);
return new ItemData<object>(fl.Value, fl.TimeStamp, fl.Quality);
case DataType.STR:
var str = reader.ReadString(address, address.DataSize);
return new ItemData<object>(str.Value, str.TimeStamp, str.Quality);
}
return new ItemData<object>(null, 0, QUALITIES.QUALITY_BAD);
}
public static int WriteValueEx(this IReaderWriter writer, DeviceAddress address, object value)
{
switch (address.VarType)
{
case DataType.BOOL:
return writer.WriteBit(address, Convert.ToBoolean(value));
case DataType.BYTE:
return writer.WriteBits(address, Convert.ToByte(value));
case DataType.WORD:
case DataType.SHORT:
return writer.WriteInt16(address, Convert.ToInt16(value));
case DataType.TIME:
case DataType.INT:
return writer.WriteInt32(address, Convert.ToInt32(value));
case DataType.FLOAT:
return writer.WriteFloat(address, Convert.ToSingle(value));
case DataType.STR:
return writer.WriteString(address, value.ToString());
}
return -1;
}
public static HistoryData[] BatchRead(DataSource source, params ITag[] itemArray)
{
int len = itemArray.Length;
HistoryData[] values = new HistoryData[len];
for (int i = 0; i < len; i++)
{
itemArray[i].Refresh(source);
values[i].ID = itemArray[i].ID;
values[i].Value = itemArray[i].Value;
values[i].TimeStamp = itemArray[i].TimeStamp;
}
return values;
}
public static int BatchWrite(IDictionary<ITag, object> items)
{
int rev = 0;
foreach (var tag in items)
{
if (tag.Key.Write(tag.Value) < 0)
rev = -1;
}
return rev;
}
public static List<PDUArea> AssignFromPDU(this ICache cacheReader, int PDU, params DeviceAddress[] addrsArr)
{
List<PDUArea> rangeList = new List<PDUArea>();
int count = addrsArr.Length;
if (count > 0)
{
//Array.Sort(addrsArr);
DeviceAddress start = addrsArr[0];
start.Bit = 0;
int bitCount = cacheReader.ByteCount;
if (count > 1)
{
int cacheLength = 0;//缓冲区的大小
int cacheIndexStart = 0;
int startIndex = 0;
DeviceAddress segmentEnd, tagAddress;
DeviceAddress segmentStart = start;
for (int j = 1, i = 1; i < count; i++, j++)
{
tagAddress = addrsArr[i];//当前变量地址
int offset1 = cacheReader.GetOffset(tagAddress, segmentStart);
if (offset1 > (PDU / cacheReader.ByteCount))
{
segmentEnd = addrsArr[i - 1];
int len = cacheReader.GetOffset(segmentEnd, segmentStart);
len += segmentEnd.DataSize <= bitCount ? 1 : segmentEnd.DataSize / bitCount;
tagAddress.CacheIndex = (ushort)(cacheIndexStart + len);
addrsArr[i] = tagAddress;
rangeList.Add(new PDUArea(segmentStart, len, startIndex, j));
startIndex += j; j = 0;
cacheLength += len;//更新缓存长度
cacheIndexStart = cacheLength;
segmentStart = tagAddress;//更新数据片段的起始地址
}
else
{
tagAddress.CacheIndex = (ushort)(cacheIndexStart + offset1);
addrsArr[i] = tagAddress;
}
if (i == count - 1)
{
segmentEnd = addrsArr[i];
int segmentLength = cacheReader.GetOffset(segmentEnd, segmentStart);
if (segmentLength > PDU / cacheReader.ByteCount)
{
segmentEnd = addrsArr[i - 1];
segmentLength = segmentEnd.DataSize <= bitCount ? 1 : segmentEnd.DataSize / bitCount;
}
tagAddress.CacheIndex = (ushort)(cacheIndexStart + segmentLength);
addrsArr[i] = tagAddress;
segmentLength += segmentEnd.DataSize <= bitCount ? 1 : segmentEnd.DataSize / bitCount;
rangeList.Add(new PDUArea(segmentStart, segmentLength, startIndex, j + 1));
cacheLength += segmentLength;
}
}
cacheReader.Size = cacheLength;
}
else
cacheReader.Size = start.DataSize <= bitCount ? 1 : start.DataSize / bitCount;//改变Cache的Size属性值将创建Cache的内存区域
}
return rangeList;
}
//调用前应对地址数组排序(是否加锁?)
public static ItemData<Storage>[] PLCReadMultiple(this IPLCDriver plc, ICache cache, DeviceAddress[] addrsArr)
{
if (addrsArr == null || cache == null || addrsArr.Length == 0) return null;
int len = addrsArr.Length;
ItemData<Storage>[] items = new ItemData<Storage>[len];
int offset = 0; long now = DateTime.Now.ToFileTime();
List<PDUArea> areas = cache.AssignFromPDU(plc.PDU, addrsArr);
foreach (PDUArea area in areas)
{
byte[] rcvBytes = plc.ReadBytes(area.Start, (ushort)area.Len);
Buffer.BlockCopy(rcvBytes, 0, cache.Cache, offset, rcvBytes.Length);
offset += rcvBytes.Length / cache.ByteCount;
}
for (int i = 0; i < len; i++)
{
switch (addrsArr[i].VarType)
{
case DataType.BOOL:
items[i].Value.Boolean = cache.ReadBit(addrsArr[i]).Value;
break;
case DataType.BYTE:
items[i].Value.Byte = cache.ReadByte(addrsArr[i]).Value;
break;
case DataType.WORD:
case DataType.SHORT:
items[i].Value.Int16 = cache.ReadInt16(addrsArr[i]).Value;
break;
case DataType.TIME:
case DataType.INT:
items[i].Value.Int32 = cache.ReadInt32(addrsArr[i]).Value;
break;
case DataType.FLOAT:
items[i].Value.Single = cache.ReadFloat(addrsArr[i]).Value;
break;
case DataType.STR:
var item = cache.ReadString(addrsArr[i], addrsArr[i].DataSize);
break;
}
items[i].Quality = QUALITIES.QUALITY_GOOD;
items[i].TimeStamp = now;
}
return items;
}
public static int PLCWriteMultiple(this IPLCDriver plc, ICache cache, DeviceAddress[] addrArr, object[] buffer, int limit)
{
if (cache == null || addrArr == null || buffer == null || addrArr.Length != buffer.Length) return -1;
if (addrArr.Length == 1) return plc.WriteValue(addrArr[0], buffer[0]);
lock (plc)//不锁定会有并发冲突问题;锁定也不能保障绝对安全,如有人现场操作会导致数据刷新
{
List<PDUArea> areas = cache.AssignFromPDU(plc.PDU, addrArr);
int offset = 0;
foreach (PDUArea area in areas)
{
byte[] rcvBytes = plc.ReadBytes(area.Start, (ushort)area.Len);
if (rcvBytes == null) return -1;
Buffer.BlockCopy(rcvBytes, 0, cache.Cache, offset, rcvBytes.Length);
offset += rcvBytes.Length / cache.ByteCount;
}
DeviceAddress start = addrArr[0];
int startIndex = 0;
int endIndex = 0;
while (endIndex < addrArr.Length)
{
if (start.Area != addrArr[endIndex].Area || start.DBNumber != addrArr[endIndex].DBNumber || endIndex - startIndex >= limit)
{
for (int i = startIndex; i < endIndex; i++)
{
cache.WriteValue(addrArr[i], buffer[i]);
}
int c1 = start.CacheIndex; int c2 = addrArr[endIndex - 1].CacheIndex;
byte[] bytes = new byte[cache.ByteCount * (c2 - c1 + 1)];
Buffer.BlockCopy(cache.Cache, c1, bytes, 0, bytes.Length);
if (plc.WriteBytes(start, bytes) < 0) return -1;
start = addrArr[endIndex];
startIndex = endIndex;
}
endIndex++;
}
}
return 0;
}
/// <summary>
/// string RightFrom
/// </summary>
/// <param name="text"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string RightFrom(this string text, int index)
{
return text.Substring(index + 1, text.Length - index - 1);
}
public static string Right(this string text, int length)
{
return text.Substring(text.Length - length, length);
}
/// <summary>
/// Convert to Datetime
/// </summary>
/// <param name="filetime"></param>
/// <returns></returns>
public static DateTime ToDateTime(this long filetime)
{
return filetime == 0 ? DateTime.Now : DateTime.FromFileTime(filetime);
}
public static Type ToType(this DataType dataType)
{
switch (dataType)
{
case DataType.BOOL:
return typeof(bool);
case DataType.BYTE:
return typeof(byte);
case DataType.WORD:
return typeof(ushort);
case DataType.SHORT:
return typeof(short);
case DataType.INT:
case DataType.TIME:
return typeof(int);
case DataType.FLOAT:
return typeof(float);
case DataType.STR:
return typeof(string);
default:
return typeof(object);
}
}
public static string ToFormatString(this int num, int len)
{
string str = num.ToString();
int off = len - str.Length;
return off > 0 ? string.Concat(new string('0', off), str) : str;
}
public static bool IsEquals(this byte[] b1, byte[] b2)
{
if (b1 == null || b2 == null) return false;
if (b1.Length != b2.Length) return false;
for (int i = 0; i < b1.Length; i++)
if (b1[i] != b2[i])
return false;
return true;
}
public static string ConvertToString(this byte[] bits)
{
char[] chars = new char[bits.Length];
for (int i = 0; i < bits.Length; i++)
{
chars[i] = (char)bits[i];
}
return new string(chars);
}
public static byte[] ConvertToArray(this string bits)
{
var chars = bits.ToCharArray();
byte[] arr = new byte[chars.Length];
for (int i = 0; i < chars.Length; i++)
{
arr[i] = (byte)chars[i];
}
return arr;
}
public static int BitSwap(this byte bit)
{
return (bit < 8 ? bit + 8 : bit - 8);
}
[Obsolete]
public static Storage ToStorage(this ITag tag, object obj)
{
Storage value = Storage.Empty;
var str = obj as string;
switch (tag.Address.VarType)
{
case DataType.BOOL:
value.Boolean = str == null ? Convert.ToBoolean(obj) : str == "0" ? false : str == "1" ? true : bool.Parse(str);
break;
case DataType.BYTE:
value.Byte = Convert.ToByte(obj);
break;
case DataType.WORD:
case DataType.SHORT:
value.Int16 = Convert.ToInt16(obj);
break;
case DataType.TIME:
case DataType.INT:
value.Int32 = Convert.ToInt32(obj);
break;
case DataType.FLOAT:
value.Single = Convert.ToSingle(obj);
break;
}
return value;
}
public static byte[] ToByteArray(this ITag tag)
{
switch (tag.Address.VarType)
{
case DataType.BOOL:
return new byte[] { tag.Value.Boolean ? (byte)1 : (byte)0 };
case DataType.BYTE:
return new byte[] { tag.Value.Byte };
case DataType.WORD:
case DataType.SHORT:
return BitConverter.GetBytes(tag.Value.Int16);
case DataType.INT:
return BitConverter.GetBytes(tag.Value.Int32);
case DataType.FLOAT:
return BitConverter.GetBytes(tag.Value.Single);
case DataType.STR:
return Encoding.ASCII.GetBytes(tag.ToString());
default:
return new byte[0];
}
}
public static byte[] ToByteArray(this ITag tag, Storage value)
{
switch (tag.Address.VarType)
{
case DataType.BOOL:
return new byte[] { value.Boolean ? (byte)1 : (byte)0 };
case DataType.BYTE:
return new byte[] { value.Byte };
case DataType.WORD:
case DataType.SHORT:
return BitConverter.GetBytes(value.Int16);
case DataType.INT:
return BitConverter.GetBytes(value.Int32);
case DataType.FLOAT:
return BitConverter.GetBytes(value.Single);
case DataType.STR:
return Encoding.ASCII.GetBytes(tag.ToString());
default:
return new byte[0];
}
}
public static object GetValue(this ITag tag, Storage value)
{
switch (tag.Address.VarType)
{
case DataType.BOOL:
return value.Boolean;
case DataType.BYTE:
return value.Byte;
case DataType.WORD:
case DataType.SHORT:
return value.Int16;
case DataType.TIME:
case DataType.INT:
return value.Int32;
case DataType.FLOAT:
return value.Single;
case DataType.STR:
return tag.ToString();
default:
return null;
}
}
public static float ValueToScale(this ITag tag, float value)
{
IDataServer srv = tag.Parent.Server;
int ind = srv.GetScaleByID(tag.ID);
Scaling meta = ind < 0 ? Scaling.Empty : srv.ScalingList[ind];
if (meta.ScaleType == ScaleType.None)
{
return value;
}
else
{
double temp = (value - meta.EULo) / (meta.EUHi - meta.EULo);
if (meta.ScaleType == ScaleType.SquareRoot)
temp = temp * temp;
return (meta.RawHi - meta.RawLo) * (float)temp + meta.RawLo;
}
}
public static float ScaleToValue(this ITag tag, Storage value)
{
DataType type = tag.Address.VarType;
if (type == DataType.BOOL) return value.Boolean ? 1f : 0f;
IDataServer srv = tag.Parent.Server;
int ind = srv.GetScaleByID(tag.ID);
Scaling meta = ind < 0 ? Scaling.Empty : srv.ScalingList[ind];
if (meta.ScaleType == ScaleType.None)
{
switch (type)
{
case DataType.BYTE:
return (float)value.Byte;
case DataType.WORD:
case DataType.SHORT:
return (float)value.Int16;
case DataType.INT:
return (float)value.Int32;
case DataType.FLOAT:
return value.Single;
case DataType.STR:
return float.Parse(tag.ToString());
default:
return 0f;
}
}
else
{
double temp;
switch (type)
{
case DataType.BYTE:
temp = (value.Byte - meta.RawLo) / (meta.RawHi - meta.RawLo);
break;
case DataType.WORD:
case DataType.SHORT:
temp = (value.Int16 - meta.RawLo) / (meta.RawHi - meta.RawLo);
break;
case DataType.INT:
temp = (value.Int32 - meta.RawLo) / (meta.RawHi - meta.RawLo);
break;
case DataType.FLOAT:
temp = (value.Single - meta.RawLo) / (meta.RawHi - meta.RawLo);
break;
default:
return 0f;
}
if (meta.ScaleType == ScaleType.SquareRoot)
temp = Math.Sqrt(temp);
return (meta.EUHi - meta.EULo) * (float)temp + meta.EULo;
}
}
public static float ScaleToValue(this ITag tag)
{
return ScaleToValue(tag, tag.Value);
}
public static string GetTagName(this ITag tag)
{
IDataServer srv = tag.Parent.Server;
int ind = srv.GetItemProperties(tag.ID);
return ind < 0 ? null : srv.MetaDataList[ind].Name;
}
public static string GetTagName(this IDataServer srv, short id)
{
int ind = srv.GetItemProperties(id);
return ind < 0 ? null : srv.MetaDataList[ind].Name;
}
public static TagMetaData GetMetaData(this ITag tag)
{
IDataServer srv = tag.Parent.Server;
int index = srv.GetItemProperties(tag.ID);
return index < 0 ? new TagMetaData() : srv.MetaDataList[index];
}
}
public static class Utility
{
private static readonly ushort[] crcTable = {
0X0000, 0XC0C1, 0XC181, 0X0140, 0XC301, 0X03C0, 0X0280, 0XC241,
0XC601, 0X06C0, 0X0780, 0XC741, 0X0500, 0XC5C1, 0XC481, 0X0440,
0XCC01, 0X0CC0, 0X0D80, 0XCD41, 0X0F00, 0XCFC1, 0XCE81, 0X0E40,
0X0A00, 0XCAC1, 0XCB81, 0X0B40, 0XC901, 0X09C0, 0X0880, 0XC841,
0XD801, 0X18C0, 0X1980, 0XD941, 0X1B00, 0XDBC1, 0XDA81, 0X1A40,
0X1E00, 0XDEC1, 0XDF81, 0X1F40, 0XDD01, 0X1DC0, 0X1C80, 0XDC41,
0X1400, 0XD4C1, 0XD581, 0X1540, 0XD701, 0X17C0, 0X1680, 0XD641,
0XD201, 0X12C0, 0X1380, 0XD341, 0X1100, 0XD1C1, 0XD081, 0X1040,
0XF001, 0X30C0, 0X3180, 0XF141, 0X3300, 0XF3C1, 0XF281, 0X3240,
0X3600, 0XF6C1, 0XF781, 0X3740, 0XF501, 0X35C0, 0X3480, 0XF441,
0X3C00, 0XFCC1, 0XFD81, 0X3D40, 0XFF01, 0X3FC0, 0X3E80, 0XFE41,
0XFA01, 0X3AC0, 0X3B80, 0XFB41, 0X3900, 0XF9C1, 0XF881, 0X3840,
0X2800, 0XE8C1, 0XE981, 0X2940, 0XEB01, 0X2BC0, 0X2A80, 0XEA41,
0XEE01, 0X2EC0, 0X2F80, 0XEF41, 0X2D00, 0XEDC1, 0XEC81, 0X2C40,
0XE401, 0X24C0, 0X2580, 0XE541, 0X2700, 0XE7C1, 0XE681, 0X2640,
0X2200, 0XE2C1, 0XE381, 0X2340, 0XE101, 0X21C0, 0X2080, 0XE041,
0XA001, 0X60C0, 0X6180, 0XA141, 0X6300, 0XA3C1, 0XA281, 0X6240,
0X6600, 0XA6C1, 0XA781, 0X6740, 0XA501, 0X65C0, 0X6480, 0XA441,
0X6C00, 0XACC1, 0XAD81, 0X6D40, 0XAF01, 0X6FC0, 0X6E80, 0XAE41,
0XAA01, 0X6AC0, 0X6B80, 0XAB41, 0X6900, 0XA9C1, 0XA881, 0X6840,
0X7800, 0XB8C1, 0XB981, 0X7940, 0XBB01, 0X7BC0, 0X7A80, 0XBA41,
0XBE01, 0X7EC0, 0X7F80, 0XBF41, 0X7D00, 0XBDC1, 0XBC81, 0X7C40,
0XB401, 0X74C0, 0X7580, 0XB541, 0X7700, 0XB7C1, 0XB681, 0X7640,
0X7200, 0XB2C1, 0XB381, 0X7340, 0XB101, 0X71C0, 0X7080, 0XB041,
0X5000, 0X90C1, 0X9181, 0X5140, 0X9301, 0X53C0, 0X5280, 0X9241,
0X9601, 0X56C0, 0X5780, 0X9741, 0X5500, 0X95C1, 0X9481, 0X5440,
0X9C01, 0X5CC0, 0X5D80, 0X9D41, 0X5F00, 0X9FC1, 0X9E81, 0X5E40,
0X5A00, 0X9AC1, 0X9B81, 0X5B40, 0X9901, 0X59C0, 0X5880, 0X9841,
0X8801, 0X48C0, 0X4980, 0X8941, 0X4B00, 0X8BC1, 0X8A81, 0X4A40,
0X4E00, 0X8EC1, 0X8F81, 0X4F40, 0X8D01, 0X4DC0, 0X4C80, 0X8C41,
0X4400, 0X84C1, 0X8581, 0X4540, 0X8701, 0X47C0, 0X4680, 0X8641,
0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040
};
/// <summary>
/// Converts an array of bytes to an ASCII byte array
/// </summary>
/// <param name="numbers">The byte array</param>
/// <returns>An array of ASCII byte values</returns>
public static string GetAsciiBytes(byte[] numbers)
{
string str = string.Empty;
for (int i = 0; i < numbers.Length; i++)
{
str += numbers[i].ToString("X2");
}
return str;
}
/// <summary>
/// Converts an array of UInt16 to an ASCII byte array
/// </summary>
/// <param name="numbers">The ushort array</param>
/// <returns>An array of ASCII byte values</returns>
public static string GetAsciiBytes(ushort[] numbers)
{
string str = string.Empty;
for (int i = 0; i < numbers.Length; i++)
{
str += numbers[i].ToString("X4");
}
return str;
}
/// <summary>
/// Converts a network order byte array to an array of UInt16 values in host order
/// </summary>
/// <param name="networkBytes">The network order byte array</param>
/// <returns>The host order ushort array</returns>
public static ushort[] NetworkBytesToHostUInt16(byte[] networkBytes)
{
if (networkBytes == null)
throw new ArgumentNullException("networkBytes");
if (networkBytes.Length % 2 != 0)
throw new FormatException("NetworkBytesNotEven");
ushort[] result = new ushort[networkBytes.Length / 2];
for (int i = 0; i < result.Length; i++)
result[i] = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt16(networkBytes, i * 2));
return result;
}
/// <summary>
/// Converts a hex string to a byte array.
/// </summary>
/// <param name="hex">The hex string</param>
/// <returns>Array of bytes</returns>
public static byte[] HexToBytes(string hex)
{
if (string.IsNullOrEmpty(hex))
throw new ArgumentNullException("hex");
if (hex.Length % 2 != 0)
throw new FormatException("HexCharacterCountNotEven");
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
return bytes;
}
/// <summary>
/// Calculate Longitudinal Redundancy Check.
/// </summary>
/// <param name="data">The data used in LRC</param>
/// <returns>LRC value</returns>
public static byte CalculateLrc(byte[] data, int len = 0)
{
if (data == null)
throw new ArgumentNullException("data");
if (len == 0) len = data.Length;
byte lrc = 0;
for (int i = 0; i < len; i++)
{ lrc += data[i]; }
lrc = (byte)((lrc ^ 0xFF) + 1);
return lrc;
}
/// <summary>
/// Calculate Cyclical Redundancy Check
/// </summary>
/// <param name="data">The data used in CRC</param>
/// <returns>CRC value</returns>
public static byte[] CalculateCrc(byte[] data, int len = 0)
{
if (data == null)
throw new ArgumentNullException("data");
if (len == 0) len = data.Length;
ushort crc = ushort.MaxValue;
for (int i = 0; i < len; i++)
{
byte tableIndex = (byte)(crc ^ data[i]);
crc >>= 8;
crc ^= crcTable[tableIndex];
}
return BitConverter.GetBytes(crc);
}
public static bool CheckSumCRC(byte[] frame)
{
int len = frame.Length;
byte[] chk = CalculateCrc(frame, len - 2);
return (chk[0] == frame[len - 2] && chk[1] == frame[len - 1]);
}
public static unsafe short NetToInt16(byte[] value, int startIndex)
{
if (value == null || startIndex > value.Length - 2)
{
throw new NotImplementedException();
}
fixed (byte* numRef = &(value[startIndex]))
{
return (short)((numRef[0] << 8) | numRef[1]);
}
}
public static unsafe int NetToInt32(byte[] value, int startIndex)
{
if (value == null || startIndex > value.Length - 4)
{
throw new NotImplementedException();
}
fixed (byte* numRef = &(value[startIndex]))
{
return (int)((numRef[0] << 24) | (numRef[1] << 16) | (numRef[2] << 8) | numRef[3]);
}
}
public static unsafe float NetToSingle(byte[] value, int startIndex)
{
int a = NetToInt32(value, startIndex);
return *(float*)&a;
}
public static string ConvertToString(byte[] bytes, int start = 0, int len = 0)
{
//西门子300、400
if (bytes == null || bytes.Length == 0)
return string.Empty;
var klen = bytes[start + 1];
return Encoding.ASCII.GetString(bytes, start + 2, klen).Trim((char)0);
}
}
}