FlyAd2021Core.cs 52.3 KB
Newer Older
1
using FlyAd2021.Inc;
潘栩锋's avatar
潘栩锋 committed
2
using FlyADBase;
3
using NLog;
4 5 6 7 8 9 10 11 12 13 14
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
{
潘栩锋's avatar
潘栩锋 committed
15
    public class FlyAd2021Core : IFlyAd2021Core
16
    {
17 18
        Logger logger = NLog.LogManager.GetCurrentClassLogger();

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
        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; }

潘栩锋's avatar
潘栩锋 committed
47 48 49 50 51
        /// <summary>
        /// 有数据需要发送
        /// </summary>
        public event SendMsgEventHander SendMsgEvent;

52 53 54 55 56
        /// <summary>
        /// 数据推送事件
        /// </summary>
        public event PushDataEventHandler PushDataEvent;

潘栩锋's avatar
潘栩锋 committed
57 58 59 60
        /// <summary>
        /// 运行状态推送事件
        /// </summary>
        public event PushRunResultEventHandler PushRunResultEvent;
61

潘栩锋's avatar
潘栩锋 committed
62
        List<byte> currPack = new List<byte>();
63 64 65 66 67 68
        int packCnt = 0;
        int recCnt = 0;
        Stopwatch stopwatch = new Stopwatch();
        CancellationTokenSource cancellationTokenSource;


潘栩锋's avatar
潘栩锋 committed
69 70 71 72 73

        /// <summary>
        /// 全部指令类别
        /// </summary>
        Dictionary<COMMREQ_Type,COMMREQ> COMMREQs;
74 75 76 77
        /// <summary>
        /// 指令队列,必须等上1条指令回复了,才能发下条指令
        /// </summary>
        List<COMMREQ_Transaction> Transactions;
潘栩锋's avatar
潘栩锋 committed
78 79


80 81 82 83
        /// <summary>
        /// 当前正在等待回复的指令
        /// </summary>
        COMMREQ_Transaction currTran;
潘栩锋's avatar
潘栩锋 committed
84 85 86
        /// <summary>
        /// currTran 发送后,开始计时
        /// </summary>
87
        Stopwatch stopwatch_timeOut;
潘栩锋's avatar
潘栩锋 committed
88 89 90 91
        /// <summary>
        /// currTran 重发次数
        /// </summary>
        int retryCnt = 0;
92

潘栩锋's avatar
潘栩锋 committed
93 94
        COMMREQ COMMREQ_RN;
        public FlyAd2021Core()
95
        {
96
            //COMMREQ_RN 作为事件,会触发一次,作为指令,也会回复一次
潘栩锋's avatar
潘栩锋 committed
97
            COMMREQ_RN = new COMMREQ(COMMREQ_Type.RN, 5, (pack, dataIdx) =>
98
            {
潘栩锋's avatar
潘栩锋 committed
99 100 101 102
                DRIVE_MAN_STATUS result = (DRIVE_MAN_STATUS)pack[dataIdx];
                dataIdx += 1;
                UInt32 serial = BitConverter.ToUInt32(pack, dataIdx);
                dataIdx += 4;
103

潘栩锋's avatar
潘栩锋 committed
104 105 106 107 108 109 110 111 112 113 114
                return new GetRunResult_Reponse()
                {
                    result = result,
                    serial = serial
                };
            });

            var _COMMREQs = new List<COMMREQ>
            {
                COMMREQ_RN,

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
                #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 运行指令
154
                new COMMREQ(COMMREQ_Type.RR),
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
                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

潘栩锋's avatar
潘栩锋 committed
174 175
                #region SysParamComm 系统参数指令
                new COMMREQ(COMMREQ_Type.ST,4,(pack,dataIdx)=>BitConverter.ToInt32(pack, dataIdx)),
176
                new COMMREQ(COMMREQ_Type.SI0,1,(pack,dataIdx)=> pack[dataIdx] == (byte)'O'),
潘栩锋's avatar
潘栩锋 committed
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
                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
225 226
            };

潘栩锋's avatar
潘栩锋 committed
227 228 229 230 231
            COMMREQs = new Dictionary<COMMREQ_Type, COMMREQ>();
            foreach (var commreq in _COMMREQs) {
                COMMREQs.Add(commreq.CType, commreq);
            }

232 233 234 235 236
            Transactions = new List<COMMREQ_Transaction>();

            stopwatch_timeOut = new Stopwatch();
        }
        #region 测量
潘栩锋's avatar
潘栩锋 committed
237
        public void StartMeasure()
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
        {
            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>
潘栩锋's avatar
潘栩锋 committed
277
        public void OnPoll_TimeOut()
278
        {
潘栩锋's avatar
潘栩锋 committed
279
            //TODO 要处理  TimeOut / ParseFuncPack / GetSendMsg 线性同步问题
280 281
            if (!IsConnected)
                return;
潘栩锋's avatar
潘栩锋 committed
282

283
            if (currTran == null)
潘栩锋's avatar
潘栩锋 committed
284 285
                return;//没有指令

286
            if (!stopwatch_timeOut.IsRunning)
潘栩锋's avatar
潘栩锋 committed
287
                return;//还没开始发送
288

潘栩锋's avatar
潘栩锋 committed
289 290
            if (stopwatch_timeOut.Elapsed < TimeSpan.FromSeconds(1))
                return;//发送到现在,还没到1秒,继续等
291 292 293 294

            //大于1秒也没回复,异常
            //重试3次
            retryCnt++;
潘栩锋's avatar
潘栩锋 committed
295
            stopwatch_timeOut.Stop();//停止,等下次发送
296

潘栩锋's avatar
潘栩锋 committed
297 298
            if (retryCnt >= 3)
            {
299 300 301 302 303 304 305 306
                //已经重试了3次,放弃
                IsConnected = false;
                currTran = null;

                //清空 指令队列
                Transactions.Clear();
                return;
            }
潘栩锋's avatar
潘栩锋 committed
307
            else {
308
                //再发一次指令
潘栩锋's avatar
潘栩锋 committed
309 310
                SendMsgEvent?.Invoke(this);
            }
311 312 313 314
        }

        public void RecMsg(byte[] recBuf)
        {
315
            IsConnected = true;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
            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]);
            }


潘栩锋's avatar
潘栩锋 committed
334
            //OnPoll_TimeOut();
335
        }
潘栩锋's avatar
潘栩锋 committed
336 337


338 339 340 341
        /// <summary>
        /// 获取 发送队列 第1条msg
        /// </summary>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
342
        public byte[] GetSendMsg()
343
        {
潘栩锋's avatar
潘栩锋 committed
344 345
            //TODO 要处理  TimeOut / ParseFuncPack / GetSendMsg 线性同步问题

346 347
            if (currTran == null)
            {
潘栩锋's avatar
潘栩锋 committed
348 349
                //当前没有指令正在发送

350 351 352 353
                if (Transactions.Count() == 0)//队列没有需要发送的指令
                    return null;


潘栩锋's avatar
潘栩锋 committed
354
                currTran = Transactions.First();
355
                retryCnt = 0;
潘栩锋's avatar
潘栩锋 committed
356
                Transactions.RemoveAt(0);
357
            }
潘栩锋's avatar
潘栩锋 committed
358
            else
359 360
            {
                //发送出去中,等待回复
361
                if (stopwatch_timeOut.IsRunning)
潘栩锋's avatar
潘栩锋 committed
362
                    return null;//已经发送了,计时器都启动了                
363 364 365
            }

            //找出 COMMREQ
潘栩锋's avatar
潘栩锋 committed
366
            var commReq = COMMREQs[currTran.ctype];
367 368 369 370
            List<byte> pack = new List<byte>();
            pack.AddRange(commReq.Prefix);
            if (currTran.datas != null)
                pack.AddRange(currTran.datas);
371 372 373 374
            if(currTran.datasObj==null)
                logger.Debug($"REQ {commReq.CType}");
            else
                logger.Debug($"REQ {commReq.CType} {Newtonsoft.Json.JsonConvert.SerializeObject(currTran.datasObj)}");
潘栩锋's avatar
潘栩锋 committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

            //获取需要发送出去的数据
            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();
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
        }

        /// 包解析
        /// </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;
            }
潘栩锋's avatar
潘栩锋 committed
425

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
            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();

潘栩锋's avatar
潘栩锋 committed
444 445 446 447 448 449 450
            int ReponseLen = 4;
            if (ReponseLen > pack.Count())
            {
                //失败,指令长度不对!!
                ErrCnt++;
                return;
            }
451 452 453 454 455 456 457 458 459 460 461 462 463

            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;
潘栩锋's avatar
潘栩锋 committed
464

465
            eventArgs.AD = BitConverter.ToUInt16(buf, index);
潘栩锋's avatar
潘栩锋 committed
466
            index += 2;
467 468 469 470


            if (hasENC1)
            {
潘栩锋's avatar
潘栩锋 committed
471 472 473 474 475 476 477 478
                ReponseLen += 2;
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

479 480 481 482 483 484
                eventArgs.ENC1 = BitConverter.ToUInt16(buf, index);
                index += 2;
            }

            if (hasENC2)
            {
潘栩锋's avatar
潘栩锋 committed
485 486 487 488 489 490 491 492
                ReponseLen += 2;
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

493 494 495 496 497 498
                eventArgs.ENC2 = BitConverter.ToUInt16(buf, index);
                index += 2;
            }

            if (hasIn)
            {
潘栩锋's avatar
潘栩锋 committed
499 500 501 502 503 504 505 506 507
                ReponseLen += 4;
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }


508 509 510 511 512 513 514 515
                eventArgs.In = BitConverter.ToUInt16(buf, index);
                index += 2;
                eventArgs.InChange = BitConverter.ToUInt16(buf, index);
                index += 2;
            }

            if (hasOut)
            {
潘栩锋's avatar
潘栩锋 committed
516 517 518 519 520 521 522 523
                ReponseLen += 2;
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

524 525 526 527 528 529
                eventArgs.Out = BitConverter.ToUInt16(buf, index);
                index += 2;
            }

            PushDataEvent?.Invoke(this, eventArgs);
        }
潘栩锋's avatar
潘栩锋 committed
530 531 532 533 534 535 536 537
        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;

538 539
            logger.Debug($"REQ RN {Newtonsoft.Json.JsonConvert.SerializeObject(retData)}");

潘栩锋's avatar
潘栩锋 committed
540 541 542 543 544
            byte systick = pack[0];
            Misc.MyBase.CLEARBIT(ref systick, 7);
            eventArgs.SysTick = systick;
            PushRunResultEvent?.Invoke(this, eventArgs);
        }
545 546 547 548 549 550
        /// <summary>
        /// 功能包解析
        /// </summary>
        /// <param name="datas"></param>
        void ParseFuncPack(byte[] pack)
        {
潘栩锋's avatar
潘栩锋 committed
551
            //TODO 要处理  TimeOut / ParseFuncPack / GetSendMsg 线性同步问题
552

潘栩锋's avatar
潘栩锋 committed
553 554
            //优先处理 PushRunResultEvent 事件
            if (IsMatch(COMMREQ_RN, pack))
555
            {
潘栩锋's avatar
潘栩锋 committed
556 557 558 559 560 561 562
                //还有B0
                if (COMMREQ_RN.ReponseLen+1 > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }
563 564 565

                

潘栩锋's avatar
潘栩锋 committed
566
                ParseFuncPack_PushRunResultEvent(pack);
567 568
            }

潘栩锋's avatar
潘栩锋 committed
569
            if (currTran == null)
570
            {
潘栩锋's avatar
潘栩锋 committed
571 572
                //没有请求。。。
                return;
573 574
            }

潘栩锋's avatar
潘栩锋 committed
575 576
            var commReq = COMMREQs[currTran.ctype];
            if (!IsMatch(commReq, pack))
577
            {
578
                logger.Error($"ACK expect:{commReq.CType} ,but reponse: {Newtonsoft.Json.JsonConvert.SerializeObject(pack)}");
潘栩锋's avatar
潘栩锋 committed
579
                //回复对不上请求
580 581 582
                return;
            }

潘栩锋's avatar
潘栩锋 committed
583 584 585 586 587
            //还有B0
            if (commReq.ReponseLen+1 > pack.Count())
            {
                //失败,指令长度不对!!
                ErrCnt++;
588 589 590
                return;
            }

591 592 593 594 595 596 597 598 599
            //处理指令
            //还有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)}");
潘栩锋's avatar
潘栩锋 committed
600

601 602 603
            //有很多指令是没有回复数据的, 回调只是通知 指令已经执行了而已
            //调用回调
            currTran.asyncDelegate?.Invoke(currTran.asyncContext, retData);
604 605 606

            stopwatch_timeOut.Stop();
            currTran = null;
潘栩锋's avatar
潘栩锋 committed
607 608 609 610 611 612
            if (Transactions.Count() > 0)
            {
                //队列还有需要发送的指令
                SendMsgEvent?.Invoke(this);
            }

613 614 615 616
        }

        bool IsMatch(COMMREQ commreq, byte[] datas)
        {
潘栩锋's avatar
潘栩锋 committed
617 618
            //datas[0] 是B0, 不应该用于判断
            if (datas.Count() < commreq.Prefix.Length+1)
619 620 621 622
                return false;

            for (int i = 0; i < commreq.Prefix.Length; i++)
            {
潘栩锋's avatar
潘栩锋 committed
623
                if (commreq.Prefix[i] != datas[1+i])
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
                {
                    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;
        }
潘栩锋's avatar
潘栩锋 committed
649 650 651 652 653 654 655 656 657 658 659
        void AddTran(COMMREQ_Transaction tran)
        {
            //放入 交易队列
            Transactions.Add(tran);
            if (currTran == null)
            {
                //当前没有指令正在发送
                SendMsgEvent?.Invoke(this);
            }
        }

660 661 662 663 664 665
        #region IOComm IO指令
        /// <summary>
        /// 获取输入口状态
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
潘栩锋's avatar
潘栩锋 committed
666 667
        [CallBack(typeof(UInt16))]
        public void GetIn(CallBackHandler asyncDelegate, object asyncContext)
668 669
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
670
            AddTran(
671 672 673 674 675 676
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.IGI,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
潘栩锋's avatar
潘栩锋 committed
677

678 679 680 681 682 683 684 685
        }

        /// <summary>
        /// 获取输出口状态
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        [CallBack(typeof(UInt16))]
潘栩锋's avatar
潘栩锋 committed
686
        public void GetOut(CallBackHandler asyncDelegate, object asyncContext)
687 688
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
689
            AddTran(
690 691 692 693 694 695 696 697 698
            new COMMREQ_Transaction()
            {
                ctype = COMMREQ_Type.IGO,
                asyncDelegate = asyncDelegate,
                asyncContext = asyncContext
            });
        }

        [CallBack(typeof(Int32))]
潘栩锋's avatar
潘栩锋 committed
699
        public void GetEnc1(CallBackHandler asyncDelegate, object asyncContext)
700 701
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
702
            AddTran(
703 704 705 706 707 708 709 710 711
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.IGP0,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(Int32))]
潘栩锋's avatar
潘栩锋 committed
712
        public void GetEnc2(CallBackHandler asyncDelegate, object asyncContext)
713 714
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
715
            AddTran(
716 717 718 719 720 721 722 723 724
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.IGP1,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(GetEncAll_Reponse))]
潘栩锋's avatar
潘栩锋 committed
725
        public void GetEncAll(CallBackHandler asyncDelegate, object asyncContext)
726 727
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
728
            AddTran(
729 730 731 732 733 734 735 736
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.IGPA,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

潘栩锋's avatar
潘栩锋 committed
737
        public void SetOutPorts(UInt16 mask, UInt16 value, CallBackHandler asyncDelegate, object asyncContext)
738 739 740 741 742 743
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(mask));
            datas.AddRange(BitConverter.GetBytes(value));

            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
744
            AddTran(
745 746
                new COMMREQ_Transaction()
                {
747
                    ctype = COMMREQ_Type.ISO,
748 749
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
750 751
                    datas = datas.ToArray(),
                    datasObj = new { mask, value }
752
                });
潘栩锋's avatar
潘栩锋 committed
753

754 755 756 757
        }
        #endregion

        #region RunComm 运行指令 SetRunParam
潘栩锋's avatar
潘栩锋 committed
758
        public void SetRunParam_V(UInt32 velocity, CallBackHandler asyncDelegate, object asyncContext)
759 760
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
761
            AddTran(
762 763 764 765 766
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RPV,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
767 768
                    datas = BitConverter.GetBytes(velocity),
                    datasObj = velocity
769 770 771
                });
        }

潘栩锋's avatar
潘栩锋 committed
772
        public void SetRunParam_SV(UInt32 sv, CallBackHandler asyncDelegate, object asyncContext)
773 774
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
775
            AddTran(
776 777 778 779 780
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RPS,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
781 782
                    datas = BitConverter.GetBytes(sv),
                    datasObj = sv
783 784 785
                });
        }

潘栩锋's avatar
潘栩锋 committed
786
        public void SetRunParam_AccTime(UInt32 accTime, CallBackHandler asyncDelegate, object asyncContext)
787 788
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
789
            AddTran(
790 791 792 793 794
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RPU,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
795 796
                    datas = BitConverter.GetBytes(accTime),
                    datasObj = accTime
797 798 799
                });
        }

潘栩锋's avatar
潘栩锋 committed
800
        public void SetRunParam_DecTime(UInt32 decTime, CallBackHandler asyncDelegate, object asyncContext)
801 802
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
803
            AddTran(
804 805 806 807 808
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RPD,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
809 810
                    datas = BitConverter.GetBytes(decTime),
                    datasObj = decTime
811 812 813
                });
        }

潘栩锋's avatar
潘栩锋 committed
814
        public void SetRunParam_HSpd1(UInt32 homespd1, CallBackHandler asyncDelegate, object asyncContext)
815 816
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
817
            AddTran(
818 819 820 821 822
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RP1,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
823 824
                    datas = BitConverter.GetBytes(homespd1),
                    datasObj = homespd1,
825 826 827
                });
        }

潘栩锋's avatar
潘栩锋 committed
828
        public void SetRunParam_HSpd2(UInt32 homespd2, CallBackHandler asyncDelegate, object asyncContext)
829 830
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
831
            AddTran(
832 833 834 835 836
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RP2,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
837 838
                    datas = BitConverter.GetBytes(homespd2),
                    datasObj = homespd2
839 840 841 842 843 844
                });
        }

        #endregion

        #region RunComm 运行指令 GetRunParam
潘栩锋's avatar
潘栩锋 committed
845
        public void GetRunParam_V(CallBackHandler asyncDelegate, object asyncContext)
846 847
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
848
            AddTran(
849 850 851 852 853 854 855 856
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RpV,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

潘栩锋's avatar
潘栩锋 committed
857
        public void GetRunParam_SV(CallBackHandler asyncDelegate, object asyncContext)
858 859
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
860
            AddTran(
861 862 863 864 865 866 867 868
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RpS,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

潘栩锋's avatar
潘栩锋 committed
869
        public void GetRunParam_AccTime(CallBackHandler asyncDelegate, object asyncContext)
870 871
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
872
            AddTran(
873 874 875 876 877 878 879 880
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RpU,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

潘栩锋's avatar
潘栩锋 committed
881
        public void GetRunParam_DecTime(CallBackHandler asyncDelegate, object asyncContext)
882 883
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
884
            AddTran(
885 886 887 888 889 890 891 892
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RpD,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

潘栩锋's avatar
潘栩锋 committed
893
        public void GetRunParam_HSpd1(CallBackHandler asyncDelegate, object asyncContext)
894 895
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
896
            AddTran(
897 898 899 900 901 902 903 904
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.Rp1,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

潘栩锋's avatar
潘栩锋 committed
905
        public void GetRunParam_HSpd2(CallBackHandler asyncDelegate, object asyncContext)
906 907
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
908
            AddTran(
909 910 911 912 913 914 915 916 917 918 919 920
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.Rp2,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        #endregion


        #region RunComm 运行指令
潘栩锋's avatar
潘栩锋 committed
921
        public void GetRunResult(CallBackHandler asyncDelegate, object asyncContext)
922 923 924
        {
            {
                //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
925
                AddTran(
926 927 928 929 930 931 932 933 934 935 936 937
                    new COMMREQ_Transaction()
                    {
                        ctype = COMMREQ_Type.RN,
                        asyncDelegate = asyncDelegate,
                        asyncContext = asyncContext
                    });
            }
        }

        public void Forw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
938
            AddTran(
939 940 941 942 943
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RF,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
944 945
                    datas = BitConverter.GetBytes(serial),
                    datasObj = serial
946 947 948 949 950 951
                });
        }

        public void Backw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
952
            AddTran(
953 954 955 956 957
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RB,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
958 959
                    datas = BitConverter.GetBytes(serial),
                    datasObj = serial
960 961 962 963 964
                });
        }
        public void Org(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
965
            AddTran(
966 967 968 969 970
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RO,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
971 972
                    datas = BitConverter.GetBytes(serial),
                    datasObj = serial
973 974 975
                });
        }

潘栩锋's avatar
潘栩锋 committed
976
        public void RunTo(int targetPos, Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
977 978
        {
            List<byte> datas = new List<byte>();
979
            datas.Add((byte)'P');
980 981 982 983
            datas.AddRange(BitConverter.GetBytes(targetPos));
            datas.AddRange(BitConverter.GetBytes(serial));

            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
984
            AddTran(
985 986
                new COMMREQ_Transaction()
                {
987
                    ctype = COMMREQ_Type.RR,
988 989
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
990 991
                    datas = datas.ToArray(),
                    datasObj = new { targetPos, serial }
992 993 994 995 996 997
                });
        }
        public void Stop(CallBackHandler asyncDelegate, object asyncContext)
        {

            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
998
            AddTran(
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RS,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        public void EStop(CallBackHandler asyncDelegate, object asyncContext)
        {

            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1010
            AddTran(
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RT,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        #endregion

        #region 同步运行指令
潘栩锋's avatar
潘栩锋 committed
1021
        public void SyncRun_SetHShift(int hShift, CallBackHandler asyncDelegate, object asyncContext)
1022 1023
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1024
            AddTran(
1025 1026 1027 1028 1029
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0x80,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1030 1031
                    datas = BitConverter.GetBytes(hShift),
                    datasObj = hShift
1032 1033 1034 1035 1036
                });
        }
        public void SyncRun_SetVShift(int vShift, CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1037
            AddTran(
1038 1039 1040 1041 1042
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0x81,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1043 1044
                    datas = BitConverter.GetBytes(vShift),
                    datasObj = vShift
1045 1046 1047
                });
        }

潘栩锋's avatar
潘栩锋 committed
1048
        public void SyncRun_Start(CallBackHandler asyncDelegate, object asyncContext)
1049 1050
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1051
            AddTran(
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0x02,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        public void SyncRun_Stop(CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1062
            AddTran(
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0x03,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        public void SyncRun_Clear(CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1073
            AddTran(
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
                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>
潘栩锋's avatar
潘栩锋 committed
1090
        public void SyncRun_RunAtLC(int pos2_begin, int pos2_end, int pos1lc, UInt32 serial, CallBackHandler asyncDelegate, object asyncContext)
1091 1092 1093 1094 1095 1096 1097 1098 1099
        {
            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));


            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1100
            AddTran(
1101 1102 1103 1104 1105
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0xE0,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1106 1107
                    datas = datas.ToArray(),
                    datasObj = new { pos2_begin, pos2_end, pos1lc, serial }
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
                });
        }

        /// <summary>
        /// 位于队列头时运行,归零;
        /// D+0xE1+命令识标号(4B)
        /// </summary>
        /// <param name="marker"></param>
        public void SyncRun_Origin(UInt32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1119
            AddTran(
1120 1121 1122 1123 1124
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0xE1,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1125 1126
                    datas = BitConverter.GetBytes(serial),
                    datasObj = serial
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
                });
        }


        /// <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));


            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1147
            AddTran(
1148 1149 1150 1151 1152
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0xE2,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1153 1154
                    datas = datas.ToArray(),
                    datasObj = new { pos1, velocity, serial }
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
                });
        }


        /// <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));


            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1176
            AddTran(
1177 1178 1179 1180 1181
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0xE3,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1182 1183
                    datas = datas.ToArray(),
                    datasObj = new { pos1lc, velocity, serial }
1184 1185 1186 1187 1188 1189 1190 1191
                });
        }

        /// <summary>
        /// 获取队列长度
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
潘栩锋's avatar
潘栩锋 committed
1192
        public void SyncRun_GetListCount(CallBackHandler asyncDelegate, object asyncContext)
1193 1194
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1195
            AddTran(
1196 1197 1198 1199 1200 1201 1202 1203 1204
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RGD,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        #endregion
潘栩锋's avatar
潘栩锋 committed
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

        #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)
        {
1231 1232 1233 1234
            string password = "@flymeasure!";
            List<byte> datas = new List<byte>();
            datas.AddRange(password.Select(p => (byte)p));

潘栩锋's avatar
潘栩锋 committed
1235 1236 1237 1238
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
1239
                    ctype = COMMREQ_Type.SI0,
潘栩锋's avatar
潘栩锋 committed
1240
                    asyncDelegate = asyncDelegate,
1241 1242 1243
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = new { password }
潘栩锋's avatar
潘栩锋 committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
                });
        }

        /// <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,
1263 1264
                    datas = access,
                    datasObj = access
潘栩锋's avatar
潘栩锋 committed
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
                });
        }

        /// <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,
1385 1386
                    datas = new byte[] { request },
                    datasObj = motorType
潘栩锋's avatar
潘栩锋 committed
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
                });
        }


        public void SetSysParam_Ratio01(UInt16 ratio01, CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.SPm,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1400 1401
                    datas = BitConverter.GetBytes(ratio01),
                    datasObj = ratio01
潘栩锋's avatar
潘栩锋 committed
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
                });
        }

        public void SetSysParam_Ratio02(UInt16 ratio02, CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.SPE,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1414 1415
                    datas = BitConverter.GetBytes(ratio02),
                    datasObj = ratio02
潘栩锋's avatar
潘栩锋 committed
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
                });
        }

        public void SetSysParam_Zero(Int16 zero, CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.SPZ,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1428 1429
                    datas = BitConverter.GetBytes(zero),
                    datasObj = zero
潘栩锋's avatar
潘栩锋 committed
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
                });
        }

        public void SetSysParam_Jog(UInt32 jog, CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.SPJ,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1442 1443
                    datas = BitConverter.GetBytes(jog),
                    datasObj = jog
潘栩锋's avatar
潘栩锋 committed
1444 1445 1446 1447
                });
        }
        #endregion

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
    }



    /// <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,
1575
        RR,
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
        RS,
        RT,
        RN,
        #endregion


        #region D,同步运行指令
        RD0x80,
        RD0x81,
        RD0x02,
        RD0x03,
        RD0x04,
        RD0xE0,
        RD0xE1,
        RD0xE2,
        RD0xE3,
        RGD,
        #endregion

        #region SysParamComm 系统参数指令
潘栩锋's avatar
潘栩锋 committed
1596
        ST,
1597
        SI0,
潘栩锋's avatar
潘栩锋 committed
1598 1599 1600
        SA,
        Sa,
        S1,
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
        #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;
1642 1643 1644 1645 1646

        /// <summary>
        /// 数据的object 表达,只用于调试而已
        /// </summary>
        public object datasObj;
1647 1648 1649
    }

}