FlyAd2021B2Core.cs 34.2 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using FlyADBase.Inc;
2
using FObjBase;
3
using GeneralGommunication;
潘栩锋's avatar
潘栩锋 committed
4 5 6 7 8 9 10 11 12 13 14 15
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 FlyADBase
{
16
    public class FlyAd2021B2Core : Dev7E, IFlyAd2021B2Core
潘栩锋's avatar
潘栩锋 committed
17
    {
潘栩锋's avatar
潘栩锋 committed
18 19 20 21 22 23 24
        const UInt16 P_Saved_Index_Op_Mode = 0;
        const UInt16 P_Saved_Index_Op_Motor = 1;
        const UInt16 P_Saved_Index_Op_Encoder = 2;
        const UInt16 P_Saved_Index_Op_Shift = 3;
        const UInt16 P_Saved_Index_Op_Speed = 4;


潘栩锋's avatar
潘栩锋 committed
25 26 27 28 29 30 31 32 33 34 35
        /// <summary>
        /// 数据推送事件
        /// </summary>
        public event PushDataEventHandler PushDataEvent;

        /// <summary>
        /// 运行状态推送事件
        /// </summary>
        public event PushRunResultEventHandler PushRunResultEvent;

        COMMREQ COMMREQ_RN;
36
        public FlyAd2021B2Core()
潘栩锋's avatar
潘栩锋 committed
37
        {
38 39
            logger = NLog.LogManager.GetCurrentClassLogger();

潘栩锋's avatar
潘栩锋 committed
40
            //COMMREQ_RN 作为事件,会触发一次,作为指令,也会回复一次
41
            COMMREQ_RN = new COMMREQ()
潘栩锋's avatar
潘栩锋 committed
42
            {
43
                Prefix = COMMREQ.ToPrefix("RN"),
44
                ResponseLen = 5,
45
                ParseFuncPack = (pack, dataIdx) =>
潘栩锋's avatar
潘栩锋 committed
46
                {
47 48 49 50
                    DRIVE_MAN_STATUS result = (DRIVE_MAN_STATUS)pack[dataIdx];
                    dataIdx += 1;
                    UInt32 serial = BitConverter.ToUInt32(pack, dataIdx);
                    dataIdx += 4;
潘栩锋's avatar
潘栩锋 committed
51

52
                    return new GetRunResult_Response()
潘栩锋's avatar
潘栩锋 committed
53
                    {
54 55 56
                        result = result,
                        serial = serial
                    };
潘栩锋's avatar
潘栩锋 committed
57
                }
58
            };
潘栩锋's avatar
潘栩锋 committed
59 60 61
        }


62
        protected override void ParsePackAfterCheckCRC8(byte[] pack) 
潘栩锋's avatar
潘栩锋 committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        {
            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();

81
            int ReponseLen = 4;  //B0(1)+B1(1)+AD(2)
潘栩锋's avatar
潘栩锋 committed
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
            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);
            bool hasAd2 = Misc.MyBase.CHECKBIT(B1, 3);

            var buf = pack;
            int index = 2;

            eventArgs.AD = BitConverter.ToUInt16(buf, index);
            index += 2;

            if (hasIn)
            {
108
                ReponseLen += 4;//IN(2)+ICHANGE(2)
潘栩锋's avatar
潘栩锋 committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }


                eventArgs.In = BitConverter.ToUInt16(buf, index);
                index += 2;
                eventArgs.InChange = BitConverter.ToUInt16(buf, index);
                index += 2;
            }

            if (hasENC1)
            {
125
                ReponseLen += 2;//ENC1(2)
潘栩锋's avatar
潘栩锋 committed
126 127 128 129 130 131 132 133 134 135 136 137 138
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

                eventArgs.ENC1 = BitConverter.ToUInt16(buf, index);
                index += 2;
            }

            if (hasENC2)
            {
139
                ReponseLen += 2;//ENC2(2)
潘栩锋's avatar
潘栩锋 committed
140 141 142 143 144 145 146 147 148 149 150 151 152
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

                eventArgs.ENC2 = BitConverter.ToUInt16(buf, index);
                index += 2;
            }

            if (hasOut)
            {
153
                ReponseLen += 2;//OUT(2)
潘栩锋's avatar
潘栩锋 committed
154 155 156 157 158 159 160 161 162 163 164 165 166
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

                eventArgs.Out = BitConverter.ToUInt16(buf, index);
                index += 2;
            }

            if (hasAd2)
            {
167 168 169 170 171 172 173 174
                ReponseLen += 2;//AD2(2)
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

潘栩锋's avatar
潘栩锋 committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
                eventArgs.AD2 = 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_Response;
            eventArgs.Status = retData.result;
            eventArgs.Serial = retData.serial;

189
            //logger.Debug($"PushRunResultEvent {Newtonsoft.Json.JsonConvert.SerializeObject(retData)}");
潘栩锋's avatar
潘栩锋 committed
190 191 192 193 194 195

            byte systick = pack[0];
            Misc.MyBase.CLEARBIT(ref systick, 7);
            eventArgs.SysTick = systick;
            PushRunResultEvent?.Invoke(this, eventArgs);
        }
196 197 198
        
        
        
潘栩锋's avatar
潘栩锋 committed
199 200 201 202
        /// <summary>
        /// 功能包解析
        /// </summary>
        /// <param name="datas"></param>
203
        protected override void ParseFuncPack(byte[] pack)
潘栩锋's avatar
潘栩锋 committed
204 205 206 207
        {
            //TODO 要处理  TimeOut / ParseFuncPack / GetSendMsg 线性同步问题

            //优先处理 PushRunResultEvent 事件
208
            if (Is_RN_Match(pack))
潘栩锋's avatar
潘栩锋 committed
209 210 211 212
            {
                ParseFuncPack_PushRunResultEvent(pack);
            }

213
            base.ParseFuncPack(pack);
潘栩锋's avatar
潘栩锋 committed
214 215 216
        }

        /// <summary>
217
        /// 数据发送,输入不需要包含开头的7E; 输出会添加CRC8 和 7E
潘栩锋's avatar
潘栩锋 committed
218 219
        /// </summary>
        /// <param name="datas"></param>
220
        protected override List<byte> GetSendPack(List<byte> datas)
潘栩锋's avatar
潘栩锋 committed
221 222
        {
            datas.Insert(0, 0x80);//插入B0
223 224
            return base.GetSendPack(datas);
        }
潘栩锋's avatar
潘栩锋 committed
225 226


227
        bool Is_RN_Match(byte[] pack)
潘栩锋's avatar
潘栩锋 committed
228
        {
229 230 231 232
            //datas[0] 是B0, 不应该用于判断
            if (pack.Count() < COMMREQ_RN.Prefix.Length + PrefixIndex)
                return false;
            return base.IsMatch(COMMREQ_RN, pack);
潘栩锋's avatar
潘栩锋 committed
233 234 235 236 237 238 239 240 241 242 243
        }

        #region IOComm IO指令
        /// <summary>
        /// 获取输入口状态
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [CallBack(typeof(UInt16))]
        public void GetIn(CallBackHandler asyncDelegate, object asyncContext)
        {
244 245 246
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("IGI"),
247
                ResponseLen = 2,
248 249 250
                ParseFuncPack = (pack, dataIdx) => { return BitConverter.ToUInt16(pack, dataIdx); }
            };

潘栩锋's avatar
潘栩锋 committed
251 252 253 254
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
255
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
256 257 258 259 260 261 262 263 264 265 266 267 268
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        /// <summary>
        /// 获取输出口状态
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [CallBack(typeof(UInt16))]
        public void GetOut(CallBackHandler asyncDelegate, object asyncContext)
        {
269 270 271
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("IGO"),
272
                ResponseLen = 2,
273 274 275
                ParseFuncPack = (pack, dataIdx) => { return BitConverter.ToUInt16(pack, dataIdx); }
            };

潘栩锋's avatar
潘栩锋 committed
276 277
            //放入 交易队列
            AddTran(
278 279 280 281 282 283
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
潘栩锋's avatar
潘栩锋 committed
284 285 286 287 288
        }

        [CallBack(typeof(Int32))]
        public void GetEnc1(CallBackHandler asyncDelegate, object asyncContext)
        {
289 290 291
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("IGP0"),
292
                ResponseLen = 4,
293 294 295
                ParseFuncPack = (pack,dataIdx)=>BitConverter.ToInt32(pack, dataIdx) 
            };

潘栩锋's avatar
潘栩锋 committed
296 297 298 299
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
300
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
301 302 303 304 305 306 307 308
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(Int32))]
        public void GetEnc2(CallBackHandler asyncDelegate, object asyncContext)
        {
309 310 311
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("IGP1"),
312
                ResponseLen = 4,
313 314 315
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToInt32(pack, dataIdx)
            };

潘栩锋's avatar
潘栩锋 committed
316 317 318 319
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
320
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
321 322 323 324 325 326 327 328
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(GetEncAll_Response))]
        public void GetEncAll(CallBackHandler asyncDelegate, object asyncContext)
        {
329 330 331
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("IGPA"),
332
                ResponseLen = 8,
333 334 335 336 337 338 339 340 341 342 343 344 345 346
                ParseFuncPack = (pack, dataIdx) =>
                {
                    int pos1 = BitConverter.ToInt32(pack, dataIdx);
                    dataIdx += 4;
                    int pos2 = BitConverter.ToInt32(pack, dataIdx);
                    dataIdx += 4;
                    return new GetEncAll_Response()
                    {
                        pos1 = pos1,
                        pos2 = pos2
                    };
                }
            };

潘栩锋's avatar
潘栩锋 committed
347 348 349 350
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
351
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
352 353 354 355 356 357 358 359 360 361
                    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));
362 363 364 365 366 367
            var datasObj = new { mask, value };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("ISO")
            };
潘栩锋's avatar
潘栩锋 committed
368 369 370 371 372

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
373
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
374 375 376
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
377
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
378 379 380 381 382 383 384
                });
        }
        #endregion

        #region RunComm 运行指令 SetRunParam
        public void SetRunParam_V(UInt32 velocity, CallBackHandler asyncDelegate, object asyncContext)
        {
385 386 387 388 389 390 391 392 393
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(velocity));
            var datasObj = new { velocity };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RPV")
            };

潘栩锋's avatar
潘栩锋 committed
394 395 396 397
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
398
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
399 400
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
401 402
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
403 404 405 406 407
                });
        }

        public void SetRunParam_SV(UInt32 sv, CallBackHandler asyncDelegate, object asyncContext)
        {
408 409 410 411 412 413 414 415 416
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(sv));
            var datasObj = new { sv };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RPS")
            };

潘栩锋's avatar
潘栩锋 committed
417 418 419 420
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
421
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
422 423
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
424 425
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
426 427 428 429 430
                });
        }

        public void SetRunParam_AccTime(UInt32 accTime, CallBackHandler asyncDelegate, object asyncContext)
        {
431 432 433 434 435 436 437 438 439
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(accTime));
            var datasObj = new { accTime };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RPU")
            };

潘栩锋's avatar
潘栩锋 committed
440 441 442 443
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
444
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
445 446
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
447 448
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
449 450 451 452 453
                });
        }

        public void SetRunParam_DecTime(UInt32 decTime, CallBackHandler asyncDelegate, object asyncContext)
        {
454 455 456 457 458 459 460 461 462
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(decTime));
            var datasObj = new { decTime };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RPD")
            };

潘栩锋's avatar
潘栩锋 committed
463 464 465 466
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
467
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
468 469
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
470 471
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
472 473 474 475 476
                });
        }

        public void SetRunParam_HSpd1(UInt32 homespd1, CallBackHandler asyncDelegate, object asyncContext)
        {
477 478 479 480 481 482 483 484 485
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(homespd1));
            var datasObj = new { homespd1 };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RP1")
            };

潘栩锋's avatar
潘栩锋 committed
486 487 488 489
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
490
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
491 492
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
493 494
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
495 496 497 498 499
                });
        }

        public void SetRunParam_HSpd2(UInt32 homespd2, CallBackHandler asyncDelegate, object asyncContext)
        {
500 501 502 503 504 505 506 507 508
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(homespd2));
            var datasObj = new { homespd2 };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RP2")
            };

潘栩锋's avatar
潘栩锋 committed
509 510 511 512
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
513
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
514 515
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
516 517
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
518 519 520 521 522 523 524 525
                });
        }

        #endregion

        #region RunComm 运行指令 GetRunParam
        public void GetRunParam_V(CallBackHandler asyncDelegate, object asyncContext)
        {
526 527 528
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RpV"),
529
                ResponseLen = 4,
530 531 532 533 534 535
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

潘栩锋's avatar
潘栩锋 committed
536 537 538 539
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
540
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
541 542 543 544 545 546 547
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_SV(CallBackHandler asyncDelegate, object asyncContext)
        {
548 549 550
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RpS"),
551
                ResponseLen = 4,
552 553 554 555 556 557
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

潘栩锋's avatar
潘栩锋 committed
558 559 560 561
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
562
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
563 564 565 566 567 568 569
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_AccTime(CallBackHandler asyncDelegate, object asyncContext)
        {
570 571 572
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RpU"),
573
                ResponseLen = 4,
574 575 576 577 578 579
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

潘栩锋's avatar
潘栩锋 committed
580 581 582 583
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
584
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
585 586 587 588 589 590 591
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_DecTime(CallBackHandler asyncDelegate, object asyncContext)
        {
592 593 594
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RpD"),
595
                ResponseLen = 4,
596 597 598 599 600 601
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

潘栩锋's avatar
潘栩锋 committed
602 603 604 605
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
606
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
607 608 609 610 611 612 613
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_HSpd1(CallBackHandler asyncDelegate, object asyncContext)
        {
614 615 616
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("Rp1"),
617
                ResponseLen = 4,
618 619 620 621 622 623
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

潘栩锋's avatar
潘栩锋 committed
624 625 626 627
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
628
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
629 630 631 632 633 634 635
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_HSpd2(CallBackHandler asyncDelegate, object asyncContext)
        {
636 637 638
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("Rp2"),
639
                ResponseLen = 4,
640 641 642 643 644 645
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

潘栩锋's avatar
潘栩锋 committed
646 647 648 649
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
650
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
651 652 653 654 655 656 657 658 659 660
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        #endregion

        #region RunComm 运行指令
        public void GetRunResult(CallBackHandler asyncDelegate, object asyncContext)
        {
661 662
            //COMMREQ_RN 作为事件,会触发一次,作为指令,也会回复一次
            COMMREQ commReq = new COMMREQ()
潘栩锋's avatar
潘栩锋 committed
663
            {
664
                Prefix = COMMREQ.ToPrefix("RN"),
665
                ResponseLen = 5,
666 667 668 669 670 671 672 673
                ParseFuncPack = (pack, dataIdx) =>
                {
                    DRIVE_MAN_STATUS result = (DRIVE_MAN_STATUS)pack[dataIdx];
                    dataIdx += 1;
                    UInt32 serial = BitConverter.ToUInt32(pack, dataIdx);
                    dataIdx += 4;

                    return new GetRunResult_Response()
潘栩锋's avatar
潘栩锋 committed
674
                    {
675 676 677 678 679 680 681 682 683 684 685 686 687 688
                        result = result,
                        serial = serial
                    };
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
潘栩锋's avatar
潘栩锋 committed
689 690 691 692
        }

        public void Forw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
693 694 695 696 697 698 699 700 701
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(serial));
            var datasObj = new { serial };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RF")
            };

潘栩锋's avatar
潘栩锋 committed
702 703 704 705
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
706
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
707 708
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
709 710
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
711 712 713 714 715
                });
        }

        public void Backw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
716 717 718 719 720 721 722 723 724
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(serial));
            var datasObj = new { serial };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RB")
            };

潘栩锋's avatar
潘栩锋 committed
725 726 727 728
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
729
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
730 731
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
732 733
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
734 735 736 737
                });
        }
        public void Org(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
738 739 740 741 742 743 744 745 746
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(serial));
            var datasObj = new { serial };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RO")
            };

潘栩锋's avatar
潘栩锋 committed
747 748 749 750
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
751
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
752 753
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
754 755
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
756 757 758 759 760 761 762 763 764
                });
        }

        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));
765 766 767 768 769 770
            var datasObj = new { targetPos, serial };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RR")
            };
潘栩锋's avatar
潘栩锋 committed
771 772 773 774 775

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
776
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
777 778 779
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
780
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
781 782 783 784
                });
        }
        public void Stop(CallBackHandler asyncDelegate, object asyncContext)
        {
785 786 787 788
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RS")
            };
潘栩锋's avatar
潘栩锋 committed
789 790 791 792 793

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
794
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
795 796 797 798 799 800
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        public void EStop(CallBackHandler asyncDelegate, object asyncContext)
        {
801 802 803 804
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RT")
            };
潘栩锋's avatar
潘栩锋 committed
805 806 807 808 809

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
810
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
811 812 813 814 815 816 817 818 819 820 821 822 823
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        #endregion

        #region SysParamComm 系统参数指令
        /// <summary>
        /// 获取系统当前Tick
        /// </summary>
        [CallBack(typeof(UInt32))]
        public void GetSysTick(CallBackHandler asyncDelegate, object asyncContext)
        {
824 825 826
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("ST"),
827
                ResponseLen = 4,
828 829 830
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToInt32(pack, dataIdx)
            };

潘栩锋's avatar
潘栩锋 committed
831 832 833 834
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
835
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
836 837 838 839 840 841 842 843 844 845 846
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        #endregion

        #region GetSysParam 读运行参数
        [CallBack(typeof(MOTORTYPE))]
        public void GetSysParam_MotorType(CallBackHandler asyncDelegate, object asyncContext)
        {
847 848
            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
849 850
                Prefix = COMMREQ.ToPrefix("PG",P_Saved_Index_Op_Mode),
                ResponseLen = 4,
851 852 853 854 855 856 857 858
                ParseFuncPack = (pack, dataIdx) => 
                {
                    byte motorType = pack[dataIdx];
                    motorType &= 3;
                    return (MOTORTYPE)motorType;
                }
            };

潘栩锋's avatar
潘栩锋 committed
859 860 861 862
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
863
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
864 865 866 867 868 869 870 871
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(UInt16))]
        public void GetSysParam_Ratio01(CallBackHandler asyncDelegate, object asyncContext)
        {
872 873
            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
874 875
                Prefix = COMMREQ.ToPrefix("PG", P_Saved_Index_Op_Motor),
                ResponseLen = 4,
876 877 878
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToUInt16(pack, dataIdx)
            };

潘栩锋's avatar
潘栩锋 committed
879 880 881 882
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
883
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
884 885 886 887 888 889 890 891
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(UInt16))]
        public void GetSysParam_Ratio02(CallBackHandler asyncDelegate, object asyncContext)
        {
892 893
            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
894 895
                Prefix = COMMREQ.ToPrefix("PG", P_Saved_Index_Op_Encoder),
                ResponseLen = 4,
896 897 898
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToUInt16(pack, dataIdx)
            };

潘栩锋's avatar
潘栩锋 committed
899 900 901 902
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
903
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
904 905 906 907 908 909 910 911
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(Int16))]
        public void GetSysParam_Zero(CallBackHandler asyncDelegate, object asyncContext)
        {
912 913
            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
914 915 916
                Prefix = COMMREQ.ToPrefix("PG", P_Saved_Index_Op_Shift),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToInt16(pack, dataIdx)
917 918
            };

潘栩锋's avatar
潘栩锋 committed
919 920 921 922
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
923
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
924 925 926 927 928 929 930 931
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(UInt32))]
        public void GetSysParam_Jog(CallBackHandler asyncDelegate, object asyncContext)
        {
932 933
            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
934
                Prefix = COMMREQ.ToPrefix("PG", P_Saved_Index_Op_Speed),
935
                ResponseLen = 4,
936 937 938
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToUInt32(pack, dataIdx)
            };

潘栩锋's avatar
潘栩锋 committed
939 940 941 942
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
943
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
944 945 946 947 948 949 950
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        #endregion

        #region SetSysParam 设置运行参数
潘栩锋's avatar
潘栩锋 committed
951 952


潘栩锋's avatar
潘栩锋 committed
953 954
        public void SetSysParam_MotorType(MOTORTYPE motorType, CallBackHandler asyncDelegate, object asyncContext)
        {
潘栩锋's avatar
潘栩锋 committed
955 956
            UInt32 request = (UInt32)motorType;
            request |= (0x3 << 2);
957
            List<byte> datas = new List<byte>();
潘栩锋's avatar
潘栩锋 committed
958 959
            datas.AddRange(BitConverter.GetBytes(request));

960 961 962 963
            var datasObj = new { motorType };

            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
964
                Prefix = COMMREQ.ToPrefix("PS", P_Saved_Index_Op_Mode)
965 966
            };

潘栩锋's avatar
潘栩锋 committed
967 968 969 970
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
971
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
972 973
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
974 975
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
976 977 978 979 980 981
                });
        }


        public void SetSysParam_Ratio01(UInt16 ratio01, CallBackHandler asyncDelegate, object asyncContext)
        {
982
            List<byte> datas = new List<byte>();
潘栩锋's avatar
潘栩锋 committed
983
            datas.AddRange(BitConverter.GetBytes((UInt32)ratio01));
984 985 986 987
            var datasObj = new { ratio01 };

            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
988
                Prefix = COMMREQ.ToPrefix("PS", P_Saved_Index_Op_Motor)
989 990
            };

潘栩锋's avatar
潘栩锋 committed
991 992 993 994
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
995
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
996 997
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
998 999
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
1000 1001 1002 1003 1004
                });
        }

        public void SetSysParam_Ratio02(UInt16 ratio02, CallBackHandler asyncDelegate, object asyncContext)
        {
1005
            List<byte> datas = new List<byte>();
潘栩锋's avatar
潘栩锋 committed
1006
            datas.AddRange(BitConverter.GetBytes((UInt32)ratio02));
1007 1008 1009 1010
            var datasObj = new { ratio02 };

            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
1011
                Prefix = COMMREQ.ToPrefix("PS",P_Saved_Index_Op_Encoder)
1012 1013
            };

潘栩锋's avatar
潘栩锋 committed
1014 1015 1016 1017
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
1018
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
1019 1020
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1021 1022
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
1023 1024 1025 1026 1027
                });
        }

        public void SetSysParam_Zero(Int16 zero, CallBackHandler asyncDelegate, object asyncContext)
        {
1028
            List<byte> datas = new List<byte>();
潘栩锋's avatar
潘栩锋 committed
1029
            datas.AddRange(BitConverter.GetBytes((Int32)zero));
1030 1031 1032 1033
            var datasObj = new { zero };

            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
1034
                Prefix = COMMREQ.ToPrefix("PS",P_Saved_Index_Op_Shift)
1035 1036
            };

潘栩锋's avatar
潘栩锋 committed
1037 1038 1039 1040
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
1041
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
1042 1043
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1044 1045
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
1046 1047 1048 1049 1050
                });
        }

        public void SetSysParam_Jog(UInt32 jog, CallBackHandler asyncDelegate, object asyncContext)
        {
1051 1052 1053 1054 1055 1056
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(jog));
            var datasObj = new { jog };

            COMMREQ commReq = new COMMREQ()
            {
潘栩锋's avatar
潘栩锋 committed
1057
                Prefix = COMMREQ.ToPrefix("PS", P_Saved_Index_Op_Speed)
1058 1059
            };

潘栩锋's avatar
潘栩锋 committed
1060 1061 1062 1063
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
1064
                    commReq = commReq,
潘栩锋's avatar
潘栩锋 committed
1065 1066
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1067 1068
                    datas = datas.ToArray(),
                    datasObj = datasObj
潘栩锋's avatar
潘栩锋 committed
1069 1070
                });
        }
潘栩锋's avatar
潘栩锋 committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

        public void SetSysParam_Apply(CallBackHandler asyncDelegate, object asyncContext) 
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PA")
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
潘栩锋's avatar
潘栩锋 committed
1088 1089 1090 1091
        #endregion

    }
}