FlyAd2021Core.cs 52.4 KB
Newer Older
1
using FlyADBase.Inc;
2
using NLog;
3 4 5 6 7 8 9 10 11
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;

12
namespace FlyADBase
13
{
潘栩锋's avatar
潘栩锋 committed
14
    public class FlyAd2021Core : IFlyAd2021Core
15
    {
16 17
        Logger logger = NLog.LogManager.GetCurrentClassLogger();

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        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
46 47 48 49 50
        /// <summary>
        /// 有数据需要发送
        /// </summary>
        public event SendMsgEventHander SendMsgEvent;

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

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

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


潘栩锋's avatar
潘栩锋 committed
68 69 70 71

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


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

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

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

            var _COMMREQs = new List<COMMREQ>
            {
                COMMREQ_RN,

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                #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 运行指令
153
                new COMMREQ(COMMREQ_Type.RR),
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                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
173 174
                #region SysParamComm 系统参数指令
                new COMMREQ(COMMREQ_Type.ST,4,(pack,dataIdx)=>BitConverter.ToInt32(pack, dataIdx)),
175
                new COMMREQ(COMMREQ_Type.SI0,1,(pack,dataIdx)=> pack[dataIdx] == (byte)'O'),
潘栩锋's avatar
潘栩锋 committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                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
224 225
            };

潘栩锋's avatar
潘栩锋 committed
226
            COMMREQs = new Dictionary<COMMREQ_Type, COMMREQ>();
227 228
            foreach (var commreq in _COMMREQs)
            {
潘栩锋's avatar
潘栩锋 committed
229 230 231
                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;
            }
307 308
            else
            {
309
                //再发一次指令
潘栩锋's avatar
潘栩锋 committed
310 311
                SendMsgEvent?.Invoke(this);
            }
312 313 314 315
        }

        public void RecMsg(byte[] recBuf)
        {
316
            IsConnected = true;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
            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
335
            //OnPoll_TimeOut();
336
        }
潘栩锋's avatar
潘栩锋 committed
337 338


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

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

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


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

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

            //获取需要发送出去的数据
            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();
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
        }

        /// 包解析
        /// </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
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
            byte B0 = pack[0];
            bool PT = Misc.MyBase.CHECKBIT(B0, 7);
            if (!PT)
            {
                ParseDataPack(pack);
            }
            else
            {

                ParseFuncPack(pack);
            }

        }


442 443
        void ParseDataPack(byte[] pack)
        {
444 445
            PushDataEventArgs eventArgs = new PushDataEventArgs();

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

            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
466

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

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

480 481 482 483

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

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

497
                eventArgs.ENC1 = BitConverter.ToUInt16(buf, index);
498 499 500
                index += 2;
            }

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

511
                eventArgs.ENC2 = BitConverter.ToUInt16(buf, index);
512 513 514 515 516
                index += 2;
            }

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

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

            PushDataEvent?.Invoke(this, eventArgs);
        }
531 532
        void ParseFuncPack_PushRunResultEvent(byte[] pack)
        {
潘栩锋's avatar
潘栩锋 committed
533 534 535 536 537 538 539

            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;

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

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

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

566

567

潘栩锋's avatar
潘栩锋 committed
568
                ParseFuncPack_PushRunResultEvent(pack);
569 570
            }

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

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

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

593 594 595 596 597 598 599 600 601
            //处理指令
            //还有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
602

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

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

615 616 617 618
        }

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

            for (int i = 0; i < commreq.Prefix.Length; i++)
            {
625
                if (commreq.Prefix[i] != datas[1 + i])
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
                {
                    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
651 652 653 654 655 656 657 658 659 660 661
        void AddTran(COMMREQ_Transaction tran)
        {
            //放入 交易队列
            Transactions.Add(tran);
            if (currTran == null)
            {
                //当前没有指令正在发送
                SendMsgEvent?.Invoke(this);
            }
        }

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

680 681 682 683 684 685 686 687
        }

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

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

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

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

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

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

756 757 758 759
        }
        #endregion

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

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

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

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

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

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

        #endregion

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

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

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

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

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

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

        #endregion


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

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

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

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

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

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

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

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

潘栩锋's avatar
潘栩锋 committed
1050
        public void SyncRun_Start(CallBackHandler asyncDelegate, object asyncContext)
1051 1052
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1053
            AddTran(
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0x02,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        public void SyncRun_Stop(CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1064
            AddTran(
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0x03,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        public void SyncRun_Clear(CallBackHandler asyncDelegate, object asyncContext)
        {
            //放入 交易队列
潘栩锋's avatar
潘栩锋 committed
1075
            AddTran(
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
                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
1092
        public void SyncRun_RunAtLC(int pos2_begin, int pos2_end, int pos1lc, UInt32 serial, CallBackHandler asyncDelegate, object asyncContext)
1093 1094 1095 1096 1097 1098 1099 1100 1101
        {
            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
1102
            AddTran(
1103 1104 1105 1106 1107
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0xE0,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1108 1109
                    datas = datas.ToArray(),
                    datasObj = new { pos2_begin, pos2_end, pos1lc, serial }
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
                });
        }

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


        /// <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
1149
            AddTran(
1150 1151 1152 1153 1154
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0xE2,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1155 1156
                    datas = datas.ToArray(),
                    datasObj = new { pos1, velocity, serial }
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
                });
        }


        /// <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
1178
            AddTran(
1179 1180 1181 1182 1183
                new COMMREQ_Transaction()
                {
                    ctype = COMMREQ_Type.RD0xE3,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
1184 1185
                    datas = datas.ToArray(),
                    datasObj = new { pos1lc, velocity, serial }
1186 1187 1188 1189 1190 1191 1192 1193
                });
        }

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

        #endregion
潘栩锋's avatar
潘栩锋 committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

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

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

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

        /// <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>
1294
        public void AddAccess(CallBackHandler asyncDelegate, object asyncContext)
潘栩锋's avatar
潘栩锋 committed
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
        {
            //放入 交易队列
            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,
1387 1388
                    datas = new byte[] { request },
                    datasObj = motorType
潘栩锋's avatar
潘栩锋 committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
                });
        }


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

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

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

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

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
    }



    /// <summary>
    /// 通知指令
    /// </summary>
    class COMMREQ
    {
        public COMMREQ_Type CType;
        /// <summary>
        /// 指令 前缀
        /// </summary>
        public byte[] Prefix;
        public string PrefixString
        {
1466 1467
            get
            {
1468
                StringBuilder stringBuilder = new StringBuilder();
1469 1470
                for (int i = 0; i < Prefix.Count(); i++)
                {
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
                    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();
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
            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;
        }
    }

1546
    enum COMMREQ_Type
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
    {
        #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,
1579
        RR,
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
        RS,
        RT,
        RN,
        #endregion


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

        #region SysParamComm 系统参数指令
潘栩锋's avatar
潘栩锋 committed
1600
        ST,
1601
        SI0,
潘栩锋's avatar
潘栩锋 committed
1602 1603 1604
        SA,
        Sa,
        S1,
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
        #endregion

        #region GetSysParam 读运行参数
        SpM,
        Spm,
        SpE,
        SpZ,
        SpJ,
        #endregion

        #region SetSysParam 设置运行参数
        SPM,
        SPm,
        SPE,
        SPZ,
        SPJ,
        #endregion


    }
1625
    class COMMREQ_Transaction
1626 1627 1628 1629 1630
    {
        /// <summary>
        /// 请求的前缀
        /// </summary>
        public COMMREQ_Type ctype;
1631

1632 1633 1634 1635
        /// <summary>
        /// 回复 callback
        /// </summary>
        public CallBackHandler asyncDelegate;
1636

1637 1638 1639 1640 1641 1642 1643 1644 1645
        /// <summary>
        /// 上下文
        /// </summary>
        public object asyncContext;

        /// <summary>
        /// 数据
        /// </summary>
        public byte[] datas;
1646 1647 1648 1649 1650

        /// <summary>
        /// 数据的object 表达,只用于调试而已
        /// </summary>
        public object datasObj;
1651 1652 1653
    }

}