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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
using FlyAd2021.Inc;
using FlyADBase;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FlyAd2021
{
public class FlyAd2021Core : IFlyAd2021Core
{
Logger logger = NLog.LogManager.GetCurrentClassLogger();
public event PropertyChangedEventHandler PropertyChanged;
#region 测量速度
public bool IsMeasuring { get; private set; }
/// <summary>
/// 通讯速度 byte/s
/// </summary>
public double CommSpeed { get; private set; }
/// <summary>
/// 传输速度 单位 pack/s
/// </summary>
public double PackSpeed { get; private set; }
#endregion
/// <summary>
/// 包出错次数
/// </summary>
public int ErrCnt { get; private set; }
/// <summary>
/// 连接成功;
/// 当命令多次发送失败,IsConnected = false
/// </summary>
public bool IsConnected { get; private set; }
/// <summary>
/// 有数据需要发送
/// </summary>
public event SendMsgEventHander SendMsgEvent;
/// <summary>
/// 数据推送事件
/// </summary>
public event PushDataEventHandler PushDataEvent;
/// <summary>
/// 运行状态推送事件
/// </summary>
public event PushRunResultEventHandler PushRunResultEvent;
List<byte> currPack = new List<byte>();
int packCnt = 0;
int recCnt = 0;
Stopwatch stopwatch = new Stopwatch();
CancellationTokenSource cancellationTokenSource;
/// <summary>
/// 全部指令类别
/// </summary>
Dictionary<COMMREQ_Type,COMMREQ> COMMREQs;
/// <summary>
/// 指令队列,必须等上1条指令回复了,才能发下条指令
/// </summary>
List<COMMREQ_Transaction> Transactions;
/// <summary>
/// 当前正在等待回复的指令
/// </summary>
COMMREQ_Transaction currTran;
/// <summary>
/// currTran 发送后,开始计时
/// </summary>
Stopwatch stopwatch_timeOut;
/// <summary>
/// currTran 重发次数
/// </summary>
int retryCnt = 0;
COMMREQ COMMREQ_RN;
public FlyAd2021Core()
{
//COMMREQ_RN 作为事件,会触发一次,作为指令,也会回复一次
COMMREQ_RN = new COMMREQ(COMMREQ_Type.RN, 5, (pack, dataIdx) =>
{
DRIVE_MAN_STATUS result = (DRIVE_MAN_STATUS)pack[dataIdx];
dataIdx += 1;
UInt32 serial = BitConverter.ToUInt32(pack, dataIdx);
dataIdx += 4;
return new GetRunResult_Reponse()
{
result = result,
serial = serial
};
});
var _COMMREQs = new List<COMMREQ>
{
COMMREQ_RN,
#region IOComm IO指令
new COMMREQ(COMMREQ_Type.IGI,2,(pack,dataIdx)=>BitConverter.ToUInt16(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.IGO,2,(pack,dataIdx)=>BitConverter.ToUInt16(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.IGP0,4,(pack,dataIdx)=>BitConverter.ToInt32(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.IGP1,4,(pack,dataIdx)=>BitConverter.ToInt32(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.IGPA,8,(pack,dataIdx)=>{
int pos1 = BitConverter.ToInt32(pack, dataIdx);
dataIdx += 4;
int pos2 = BitConverter.ToInt32(pack, dataIdx);
dataIdx += 4;
return new GetEncAll_Reponse()
{
pos1 = pos1,
pos2 = pos2
};
}),
new COMMREQ(COMMREQ_Type.ISO),
#endregion
#region RunComm 运行指令 SetRunParam
new COMMREQ(COMMREQ_Type.RPV),
new COMMREQ(COMMREQ_Type.RPS),
new COMMREQ(COMMREQ_Type.RPU),
new COMMREQ(COMMREQ_Type.RPD),
new COMMREQ(COMMREQ_Type.RP1),
new COMMREQ(COMMREQ_Type.RP2),
#endregion
#region RunComm 运行指令 GetRunParam
new COMMREQ(COMMREQ_Type.RpV,4,(pack,dataIdx)=>BitConverter.ToUInt32(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.RpS,4,(pack,dataIdx)=>BitConverter.ToUInt32(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.RpU,4,(pack,dataIdx)=>BitConverter.ToUInt32(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.RpD,4,(pack,dataIdx)=>BitConverter.ToUInt32(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.Rp1,4,(pack,dataIdx)=>BitConverter.ToUInt32(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.Rp2,4,(pack,dataIdx)=>BitConverter.ToUInt32(pack, dataIdx)),
#endregion
#region RunComm 运行指令
new COMMREQ(COMMREQ_Type.RR),
new COMMREQ(COMMREQ_Type.RO),
new COMMREQ(COMMREQ_Type.RF),
new COMMREQ(COMMREQ_Type.RB),
new COMMREQ(COMMREQ_Type.RS),
new COMMREQ(COMMREQ_Type.RT),
#endregion
#region 同步运行指令
new COMMREQ(COMMREQ_Type.RD0x80,new byte[]{ (byte)'R', (byte)'D',0x80}),
new COMMREQ(COMMREQ_Type.RD0x81,new byte[]{ (byte)'R', (byte)'D',0x81}),
new COMMREQ(COMMREQ_Type.RD0x02,new byte[]{ (byte)'R', (byte)'D',0x02}),
new COMMREQ(COMMREQ_Type.RD0x03,new byte[]{ (byte)'R', (byte)'D',0x03}),
new COMMREQ(COMMREQ_Type.RD0x04,new byte[]{ (byte)'R', (byte)'D',0x04}),
new COMMREQ(COMMREQ_Type.RD0xE0,new byte[]{ (byte)'R', (byte)'D',0xE0}),
new COMMREQ(COMMREQ_Type.RD0xE1,new byte[]{ (byte)'R', (byte)'D',0xE1}),
new COMMREQ(COMMREQ_Type.RD0xE2,new byte[]{ (byte)'R', (byte)'D',0xE3}),
new COMMREQ(COMMREQ_Type.RGD,2),
#endregion
#region SysParamComm 系统参数指令
new COMMREQ(COMMREQ_Type.ST,4,(pack,dataIdx)=>BitConverter.ToInt32(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.SI0,1,(pack,dataIdx)=> pack[dataIdx] == (byte)'O'),
new COMMREQ(COMMREQ_Type.SA,2,(pack,dataIdx)=>{
short ret = BitConverter.ToInt16(pack, dataIdx);
if(ret == 0)
return AREA_ERR.NO_ERR;
else if(ret == -105)
return AREA_ERR.DUP_ACCESS;
else// if(ret == -101)
return AREA_ERR.ERR_ACCESS;
}),
new COMMREQ(COMMREQ_Type.Sa,18,(pack,dataIdx)=>{
GetAccess_Reponse reponse = new GetAccess_Reponse();
int idx = dataIdx;
reponse.status = (AREA_STATUS)pack[idx];
idx++;
reponse.ret = (AREA_ERR)pack[idx];
idx++;
Array.Copy(pack, idx, reponse.code, 0, 6);
idx += 6;
reponse.surplus = BitConverter.ToUInt16(pack, idx);
idx += 2;
Array.Copy(pack, idx, reponse.access, 0, 8);
idx += 8;
return reponse;
}),
new COMMREQ(COMMREQ_Type.S1),
#endregion
#region GetSysParam 读运行参数
new COMMREQ(COMMREQ_Type.SpM,1,(pack,dataIdx)=>{
byte motorType = pack[dataIdx];
motorType &= 3;
return (MOTORTYPE)motorType;
}),
new COMMREQ(COMMREQ_Type.Spm,2,(pack,dataIdx)=>BitConverter.ToUInt16(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.SpE,2,(pack,dataIdx)=>BitConverter.ToUInt16(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.SpZ,2,(pack,dataIdx)=>BitConverter.ToInt16(pack, dataIdx)),
new COMMREQ(COMMREQ_Type.SpJ,4,(pack,dataIdx)=>BitConverter.ToUInt32(pack, dataIdx)),
#endregion
#region SetSysParam 读运行参数
new COMMREQ(COMMREQ_Type.SPM),
new COMMREQ(COMMREQ_Type.SPm),
new COMMREQ(COMMREQ_Type.SPE),
new COMMREQ(COMMREQ_Type.SPZ),
new COMMREQ(COMMREQ_Type.SPJ),
#endregion
};
COMMREQs = new Dictionary<COMMREQ_Type, COMMREQ>();
foreach (var commreq in _COMMREQs) {
COMMREQs.Add(commreq.CType, commreq);
}
Transactions = new List<COMMREQ_Transaction>();
stopwatch_timeOut = new Stopwatch();
}
#region 测量
public void StartMeasure()
{
if (IsMeasuring)
return;
IsMeasuring = true;
cancellationTokenSource = new CancellationTokenSource();
//启动线程,测量速度
Task.Factory.StartNew(MeasureTask, cancellationTokenSource.Token);
}
public void StopMeasure()
{
cancellationTokenSource.Cancel();
//停止线程
IsMeasuring = false;
}
async void MeasureTask()
{
stopwatch.Start();
while (!cancellationTokenSource.IsCancellationRequested)
{
if (stopwatch.Elapsed > TimeSpan.FromSeconds(1))
{
//1秒均值
CommSpeed = recCnt / stopwatch.Elapsed.TotalSeconds;
PackSpeed = packCnt / stopwatch.Elapsed.TotalSeconds;
recCnt = 0;
packCnt = 0;
stopwatch.Restart();
}
await Task.Delay(100);
}
stopwatch.Stop();
}
#endregion
/// <summary>
/// 发送指令的超时判断
/// </summary>
public void OnPoll_TimeOut()
{
//TODO 要处理 TimeOut / ParseFuncPack / GetSendMsg 线性同步问题
if (!IsConnected)
return;
if (currTran == null)
return;//没有指令
if (!stopwatch_timeOut.IsRunning)
return;//还没开始发送
if (stopwatch_timeOut.Elapsed < TimeSpan.FromSeconds(1))
return;//发送到现在,还没到1秒,继续等
//大于1秒也没回复,异常
//重试3次
retryCnt++;
stopwatch_timeOut.Stop();//停止,等下次发送
if (retryCnt >= 3)
{
//已经重试了3次,放弃
IsConnected = false;
currTran = null;
//清空 指令队列
Transactions.Clear();
return;
}
else {
//再发一次指令
SendMsgEvent?.Invoke(this);
}
}
public void RecMsg(byte[] recBuf)
{
IsConnected = true;
for (int i = 0; i < recBuf.Count(); i++)
{
if (recBuf[i] == 0x7e)
{
//找到头了
//结束之前的包
if (currPack.Count > 0)
{
var pack = currPack.ToArray();
ParsePack(pack);
currPack.Clear();
packCnt++;
}
}
currPack.Add(recBuf[i]);
}
//OnPoll_TimeOut();
}
/// <summary>
/// 获取 发送队列 第1条msg
/// </summary>
/// <returns></returns>
public byte[] GetSendMsg()
{
//TODO 要处理 TimeOut / ParseFuncPack / GetSendMsg 线性同步问题
if (currTran == null)
{
//当前没有指令正在发送
if (Transactions.Count() == 0)//队列没有需要发送的指令
return null;
currTran = Transactions.First();
retryCnt = 0;
Transactions.RemoveAt(0);
}
else
{
//发送出去中,等待回复
if (stopwatch_timeOut.IsRunning)
return null;//已经发送了,计时器都启动了
}
//找出 COMMREQ
var commReq = COMMREQs[currTran.ctype];
List<byte> pack = new List<byte>();
pack.AddRange(commReq.Prefix);
if (currTran.datas != null)
pack.AddRange(currTran.datas);
if(currTran.datasObj==null)
logger.Debug($"REQ {commReq.CType}");
else
logger.Debug($"REQ {commReq.CType} {Newtonsoft.Json.JsonConvert.SerializeObject(currTran.datasObj)}");
//获取需要发送出去的数据
var buf = GetSendPack(pack).ToArray();
//开始计时
stopwatch_timeOut.Restart();
return buf;
}
/// <summary>
/// 复位全部状态,通常由于通讯模块检测到连接断开导致的
/// </summary>
public void ResetMsg()
{
currTran = null;
stopwatch_timeOut.Stop();
recCnt = 0;
IsConnected = false;
Transactions.Clear();
}
/// 包解析
/// </summary>
void ParsePack(byte[] pack)
{
//第0个肯定是7E
if (pack.Count() == 1)
{
//只有一个 7E
return;
}
//转义数据
if (!ProtocolCommon.Pdu2Data(pack, out List<byte> datas))
{
//异常
return;
}
pack = datas.ToArray();
//解析后的 datas 没有了 帧包装 7E
byte crc8 = Misc.CRC.CRC8(pack, 0, pack.Count() - 1);
if (pack.Last() != crc8)
{
//CRC8 出错
ErrCnt++;
return;
}
byte B0 = pack[0];
bool PT = Misc.MyBase.CHECKBIT(B0, 7);
if (!PT)
{
ParseDataPack(pack);
}
else
{
ParseFuncPack(pack);
}
}
void ParseDataPack(byte[] pack) {
PushDataEventArgs eventArgs = new PushDataEventArgs();
int ReponseLen = 4;
if (ReponseLen > pack.Count())
{
//失败,指令长度不对!!
ErrCnt++;
return;
}
byte systick = pack[0];
Misc.MyBase.CLEARBIT(ref systick, 7);
eventArgs.SysTick = systick;
byte B1 = pack[1];
bool hasIn = Misc.MyBase.CHECKBIT(B1, 7);
bool hasENC1 = Misc.MyBase.CHECKBIT(B1, 6);
bool hasENC2 = Misc.MyBase.CHECKBIT(B1, 5);
bool hasOut = Misc.MyBase.CHECKBIT(B1, 4);
var buf = pack;
int index = 2;
eventArgs.AD = BitConverter.ToUInt16(buf, index);
index += 2;
if (hasENC1)
{
ReponseLen += 2;
if (ReponseLen > pack.Count())
{
//失败,指令长度不对!!
ErrCnt++;
return;
}
eventArgs.ENC1 = BitConverter.ToUInt16(buf, index);
index += 2;
}
if (hasENC2)
{
ReponseLen += 2;
if (ReponseLen > pack.Count())
{
//失败,指令长度不对!!
ErrCnt++;
return;
}
eventArgs.ENC2 = BitConverter.ToUInt16(buf, index);
index += 2;
}
if (hasIn)
{
ReponseLen += 4;
if (ReponseLen > pack.Count())
{
//失败,指令长度不对!!
ErrCnt++;
return;
}
eventArgs.In = BitConverter.ToUInt16(buf, index);
index += 2;
eventArgs.InChange = BitConverter.ToUInt16(buf, index);
index += 2;
}
if (hasOut)
{
ReponseLen += 2;
if (ReponseLen > pack.Count())
{
//失败,指令长度不对!!
ErrCnt++;
return;
}
eventArgs.Out = BitConverter.ToUInt16(buf, index);
index += 2;
}
PushDataEvent?.Invoke(this, eventArgs);
}
void ParseFuncPack_PushRunResultEvent(byte[] pack) {
PushRunResultEventArgs eventArgs = new PushRunResultEventArgs();
var retData = COMMREQ_RN.ParseFuncPack(pack, 1 + COMMREQ_RN.Prefix.Count()) as GetRunResult_Reponse;
eventArgs.Status = retData.result;
eventArgs.Serial = retData.serial;
logger.Debug($"REQ RN {Newtonsoft.Json.JsonConvert.SerializeObject(retData)}");
byte systick = pack[0];
Misc.MyBase.CLEARBIT(ref systick, 7);
eventArgs.SysTick = systick;
PushRunResultEvent?.Invoke(this, eventArgs);
}
/// <summary>
/// 功能包解析
/// </summary>
/// <param name="datas"></param>
void ParseFuncPack(byte[] pack)
{
//TODO 要处理 TimeOut / ParseFuncPack / GetSendMsg 线性同步问题
//优先处理 PushRunResultEvent 事件
if (IsMatch(COMMREQ_RN, pack))
{
//还有B0
if (COMMREQ_RN.ReponseLen+1 > pack.Count())
{
//失败,指令长度不对!!
ErrCnt++;
return;
}
ParseFuncPack_PushRunResultEvent(pack);
}
if (currTran == null)
{
//没有请求。。。
return;
}
var commReq = COMMREQs[currTran.ctype];
if (!IsMatch(commReq, pack))
{
logger.Error($"ACK expect:{commReq.CType} ,but reponse: {Newtonsoft.Json.JsonConvert.SerializeObject(pack)}");
//回复对不上请求
return;
}
//还有B0
if (commReq.ReponseLen+1 > pack.Count())
{
//失败,指令长度不对!!
ErrCnt++;
return;
}
//处理指令
//还有B0
object retData = null;
if (commReq.ParseFuncPack != null)//解析回复数据
retData = commReq.ParseFuncPack(pack, commReq.Prefix.Count() + 1);
if (retData == null)
logger.Debug($"ACK {commReq.CType}");
else
logger.Debug($"ACK {commReq.CType} {Newtonsoft.Json.JsonConvert.SerializeObject(retData)}");
//有很多指令是没有回复数据的, 回调只是通知 指令已经执行了而已
//调用回调
currTran.asyncDelegate?.Invoke(currTran.asyncContext, retData);
stopwatch_timeOut.Stop();
currTran = null;
if (Transactions.Count() > 0)
{
//队列还有需要发送的指令
SendMsgEvent?.Invoke(this);
}
}
bool IsMatch(COMMREQ commreq, byte[] datas)
{
//datas[0] 是B0, 不应该用于判断
if (datas.Count() < commreq.Prefix.Length+1)
return false;
for (int i = 0; i < commreq.Prefix.Length; i++)
{
if (commreq.Prefix[i] != datas[1+i])
{
return false;
}
}
return true;
}
/// <summary>
/// 数据发送,不需要包含开头的7E
/// </summary>
/// <param name="datas"></param>
List<byte> GetSendPack(List<byte> datas)
{
datas.Insert(0, 0x80);//插入B0
//需要在后面添加CRC8
byte crc8 = Misc.CRC.CRC8(datas, 0, datas.Count);
datas.Add(crc8);
ProtocolCommon.Data2Pdu(datas, out List<byte> pack);
pack.Add(0x7E);//一定要加0x7E
return pack;
}
void AddTran(COMMREQ_Transaction tran)
{
//放入 交易队列
Transactions.Add(tran);
if (currTran == null)
{
//当前没有指令正在发送
SendMsgEvent?.Invoke(this);
}
}
#region IOComm IO指令
/// <summary>
/// 获取输入口状态
/// </summary>
/// <param name="asyncDelegate"></param>
/// <param name="asyncContext"></param>
[CallBack(typeof(UInt16))]
public void GetIn(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.IGI,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
/// <summary>
/// 获取输出口状态
/// </summary>
/// <param name="asyncDelegate"></param>
/// <param name="asyncContext"></param>
[CallBack(typeof(UInt16))]
public void GetOut(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.IGO,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
[CallBack(typeof(Int32))]
public void GetEnc1(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.IGP0,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
[CallBack(typeof(Int32))]
public void GetEnc2(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.IGP1,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
[CallBack(typeof(GetEncAll_Reponse))]
public void GetEncAll(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.IGPA,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
public void SetOutPorts(UInt16 mask, UInt16 value, CallBackHandler asyncDelegate, object asyncContext)
{
List<byte> datas = new List<byte>();
datas.AddRange(BitConverter.GetBytes(mask));
datas.AddRange(BitConverter.GetBytes(value));
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.ISO,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = datas.ToArray(),
datasObj = new { mask, value }
});
}
#endregion
#region RunComm 运行指令 SetRunParam
public void SetRunParam_V(UInt32 velocity, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RPV,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(velocity),
datasObj = velocity
});
}
public void SetRunParam_SV(UInt32 sv, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RPS,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(sv),
datasObj = sv
});
}
public void SetRunParam_AccTime(UInt32 accTime, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RPU,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(accTime),
datasObj = accTime
});
}
public void SetRunParam_DecTime(UInt32 decTime, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RPD,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(decTime),
datasObj = decTime
});
}
public void SetRunParam_HSpd1(UInt32 homespd1, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RP1,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(homespd1),
datasObj = homespd1,
});
}
public void SetRunParam_HSpd2(UInt32 homespd2, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RP2,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(homespd2),
datasObj = homespd2
});
}
#endregion
#region RunComm 运行指令 GetRunParam
public void GetRunParam_V(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RpV,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
public void GetRunParam_SV(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RpS,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
public void GetRunParam_AccTime(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RpU,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
public void GetRunParam_DecTime(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RpD,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
public void GetRunParam_HSpd1(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.Rp1,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
public void GetRunParam_HSpd2(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.Rp2,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
#endregion
#region RunComm 运行指令
public void GetRunResult(CallBackHandler asyncDelegate, object asyncContext)
{
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RN,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
}
public void Forw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RF,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(serial),
datasObj = serial
});
}
public void Backw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RB,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(serial),
datasObj = serial
});
}
public void Org(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RO,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(serial),
datasObj = serial
});
}
public void RunTo(int targetPos, Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
{
List<byte> datas = new List<byte>();
datas.Add((byte)'P');
datas.AddRange(BitConverter.GetBytes(targetPos));
datas.AddRange(BitConverter.GetBytes(serial));
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RR,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = datas.ToArray(),
datasObj = new { targetPos, serial }
});
}
public void Stop(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RS,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
public void EStop(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RT,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
#endregion
#region 同步运行指令
public void SyncRun_SetHShift(int hShift, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RD0x80,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(hShift),
datasObj = hShift
});
}
public void SyncRun_SetVShift(int vShift, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RD0x81,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(vShift),
datasObj = vShift
});
}
public void SyncRun_Start(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RD0x02,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
public void SyncRun_Stop(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RD0x03,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
public void SyncRun_Clear(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RD0x04,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
/// <summary>
/// 同步扫描至;
/// D+0xE0+开始主轴位置+结束主轴位置+结束横向脉冲位置(逻辑位置)+命令识标号(4B)
/// </summary>
/// <param name="pos2_begin"></param>
/// <param name="pos2_end"></param>
/// <param name="pos1lc"></param>
/// <param name="serial"></param>
public void SyncRun_RunAtLC(int pos2_begin, int pos2_end, int pos1lc, UInt32 serial, CallBackHandler asyncDelegate, object asyncContext)
{
List<byte> datas = new List<byte>();
datas.AddRange(BitConverter.GetBytes(pos2_begin));
datas.AddRange(BitConverter.GetBytes(pos2_end));
datas.AddRange(BitConverter.GetBytes(pos1lc));
datas.AddRange(BitConverter.GetBytes(serial));
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RD0xE0,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = datas.ToArray(),
datasObj = new { pos2_begin, pos2_end, pos1lc, serial }
});
}
/// <summary>
/// 位于队列头时运行,归零;
/// D+0xE1+命令识标号(4B)
/// </summary>
/// <param name="marker"></param>
public void SyncRun_Origin(UInt32 serial, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RD0xE1,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(serial),
datasObj = serial
});
}
/// <summary>
/// 位于队列头时运行,以速度运行至物理位置;
/// D+0xE2+横向脉冲位置(4B:int32,物理位置)+速度(4B:int32)+命令识标号(4B)
/// </summary>
/// <param name="pos1"></param>
/// <param name="velocity"></param>
/// <param name="serial"></param>
public void SyncRun_RunTo(int pos1, UInt32 velocity, UInt32 serial, CallBackHandler asyncDelegate, object asyncContext)
{
List<byte> datas = new List<byte>();
datas.AddRange(BitConverter.GetBytes(pos1));
datas.AddRange(BitConverter.GetBytes(velocity));
datas.AddRange(BitConverter.GetBytes(serial));
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RD0xE2,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = datas.ToArray(),
datasObj = new { pos1, velocity, serial }
});
}
/// <summary>
/// 位于队列头时运行,以速度运行至逻辑位置;
/// D+0xE3+横向脉冲位置(4B:int32,逻辑位置)+速度(4B:int32)+命令识标号(4B)
/// </summary>
/// <param name="pos1lc"></param>
/// <param name="velocity"></param>
/// <param name="hasDataGrid"></param>
/// <param name="marker"></param>
public void SyncRun_RunToLC(int pos1lc, UInt32 velocity, UInt32 serial, CallBackHandler asyncDelegate, object asyncContext)
{
List<byte> datas = new List<byte>();
datas.AddRange(BitConverter.GetBytes(pos1lc));
datas.AddRange(BitConverter.GetBytes(velocity));
datas.AddRange(BitConverter.GetBytes(serial));
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RD0xE3,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = datas.ToArray(),
datasObj = new { pos1lc, velocity, serial }
});
}
/// <summary>
/// 获取队列长度
/// </summary>
/// <param name="asyncDelegate"></param>
/// <param name="asyncContext"></param>
public void SyncRun_GetListCount(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.RGD,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
#endregion
#region SysParamComm 系统参数指令
/// <summary>
/// 获取系统当前Tick
/// </summary>
[CallBack(typeof(UInt32))]
public void GetSysTick(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.ST,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
/// <summary>
/// 初始化系统信息区
/// </summary>
/// <param name="asyncDelegate"></param>
/// <param name="asyncContext"></param>
[CallBack(typeof(bool))]
public void InitArea(CallBackHandler asyncDelegate, object asyncContext)
{
string password = "@flymeasure!";
List<byte> datas = new List<byte>();
datas.AddRange(password.Select(p => (byte)p));
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SI0,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = datas.ToArray(),
datasObj = new { password }
});
}
/// <summary>
/// 输入系统授权码
/// </summary>
/// <param name="access"></param>
/// <param name="asyncDelegate"></param>
/// <param name="asyncContext"></param>
[CallBack(typeof(AREA_ERR))]
public void CheckAccessCode(byte[] access, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SA,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = access,
datasObj = access
});
}
/// <summary>
/// 获取系统授权信息
/// </summary>
/// <param name="asyncDelegate"></param>
/// <param name="asyncContext"></param>
[CallBack(typeof(GetAccess_Reponse))]
public void GetAccess(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.Sa,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
/// <summary>
/// 系统运行时间+1
/// </summary>
/// <param name="asyncDelegate"></param>
/// <param name="asyncContext"></param>
public void AddAccess(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.S1,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
#endregion
#region GetSysParam 读运行参数
[CallBack(typeof(MOTORTYPE))]
public void GetSysParam_MotorType(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SpM,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
[CallBack(typeof(UInt16))]
public void GetSysParam_Ratio01(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.Spm,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
[CallBack(typeof(UInt16))]
public void GetSysParam_Ratio02(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SpE,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
[CallBack(typeof(Int16))]
public void GetSysParam_Zero(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SpZ,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
[CallBack(typeof(UInt32))]
public void GetSysParam_Jog(CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SpJ,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext
});
}
#endregion
#region SetSysParam 设置运行参数
public void SetSysParam_MotorType(MOTORTYPE motorType, CallBackHandler asyncDelegate, object asyncContext)
{
byte request = (byte)motorType;
Misc.MyBase.SIGNBIT(ref request, 5);
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SPM,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = new byte[] { request },
datasObj = motorType
});
}
public void SetSysParam_Ratio01(UInt16 ratio01, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SPm,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(ratio01),
datasObj = ratio01
});
}
public void SetSysParam_Ratio02(UInt16 ratio02, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SPE,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(ratio02),
datasObj = ratio02
});
}
public void SetSysParam_Zero(Int16 zero, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SPZ,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(zero),
datasObj = zero
});
}
public void SetSysParam_Jog(UInt32 jog, CallBackHandler asyncDelegate, object asyncContext)
{
//放入 交易队列
AddTran(
new COMMREQ_Transaction()
{
ctype = COMMREQ_Type.SPJ,
asyncDelegate = asyncDelegate,
asyncContext = asyncContext,
datas = BitConverter.GetBytes(jog),
datasObj = jog
});
}
#endregion
}
/// <summary>
/// 通知指令
/// </summary>
class COMMREQ
{
public COMMREQ_Type CType;
/// <summary>
/// 指令 前缀
/// </summary>
public byte[] Prefix;
public string PrefixString
{
get {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < Prefix.Count(); i++) {
stringBuilder.Append((char)Prefix[i]);
}
return stringBuilder.ToString();
}
}
/// <summary>
/// 回复数据 总长度,含前序
/// </summary>
public int ReponseLen;
public delegate object ParseFuncPackHandler(byte[] pack, int dataIdx);
public ParseFuncPackHandler ParseFuncPack;
public COMMREQ(COMMREQ_Type ctype)
{
CType = ctype;
string prefixString = CType.ToString();
Prefix = new byte[prefixString.Count()];
this.Prefix = prefixString.Select(p => (byte)p).ToArray();
this.ReponseLen = Prefix.Length;
}
public COMMREQ(COMMREQ_Type ctype, int reponseLen)
{
CType = ctype;
string prefixString = CType.ToString();
Prefix = new byte[prefixString.Count()];
this.Prefix = prefixString.Select(p => (byte)p).ToArray();
this.ReponseLen = reponseLen;
}
public COMMREQ(COMMREQ_Type ctype, int dataLen, ParseFuncPackHandler parseFuncPack)
{
CType = ctype;
string prefixString = CType.ToString();
Prefix = new byte[prefixString.Count()];
this.Prefix = prefixString.Select(p => (byte)p).ToArray();
this.ParseFuncPack = parseFuncPack;
this.ReponseLen = Prefix.Count() + dataLen;
}
public COMMREQ(COMMREQ_Type ctype, string prefix, int reponseLen, ParseFuncPackHandler parseFuncPack)
{
CType = ctype;
Prefix = new byte[prefix.Count()];
this.Prefix = prefix.Select(p => (byte)p).ToArray();
this.ParseFuncPack = parseFuncPack;
this.ReponseLen = reponseLen;
}
public COMMREQ(COMMREQ_Type ctype, byte[] prefixs, int reponseLen, ParseFuncPackHandler parseFuncPack)
{
CType = ctype;
this.Prefix = prefixs;
this.ParseFuncPack = parseFuncPack;
this.ReponseLen = reponseLen;
}
public COMMREQ(COMMREQ_Type ctype, byte[] prefixs)
{
CType = ctype;
this.Prefix = prefixs;
this.ReponseLen = prefixs.Length;
}
}
enum COMMREQ_Type
{
#region IOComm IO指令
IGI,
IGO,
IGP0,
IGP1,
IGPA,
ISO,
#endregion
#region RunComm 运行指令 SetRunParam
RPV,
RPS,
RPU,
RPD,
RP1,
RP2,
#endregion
#region RunComm 运行指令 GetRunParam
RpV,
RpS,
RpU,
RpD,
Rp1,
Rp2,
#endregion
#region RunComm 运行指令
RF,
RB,
RO,
RR,
RS,
RT,
RN,
#endregion
#region D,同步运行指令
RD0x80,
RD0x81,
RD0x02,
RD0x03,
RD0x04,
RD0xE0,
RD0xE1,
RD0xE2,
RD0xE3,
RGD,
#endregion
#region SysParamComm 系统参数指令
ST,
SI0,
SA,
Sa,
S1,
#endregion
#region GetSysParam 读运行参数
SpM,
Spm,
SpE,
SpZ,
SpJ,
#endregion
#region SetSysParam 设置运行参数
SPM,
SPm,
SPE,
SPZ,
SPJ,
#endregion
}
class COMMREQ_Transaction
{
/// <summary>
/// 请求的前缀
/// </summary>
public COMMREQ_Type ctype;
/// <summary>
/// 回复 callback
/// </summary>
public CallBackHandler asyncDelegate;
/// <summary>
/// 上下文
/// </summary>
public object asyncContext;
/// <summary>
/// 数据
/// </summary>
public byte[] datas;
/// <summary>
/// 数据的object 表达,只用于调试而已
/// </summary>
public object datasObj;
}
}