using FlyADBase.Inc;
using FObjBase;
using GeneralGommunication;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FlyADBase
{
    public class FlyAd2021B2Core : Dev7E, IFlyAd2021B2Core
    {
        const UInt16 P_Saved_Index_Op_Mode = 0;
        const UInt16 P_Saved_Index_Op_Motor = 1;
        const UInt16 P_Saved_Index_Op_Encoder = 2;
        const UInt16 P_Saved_Index_Op_Shift = 3;
        const UInt16 P_Saved_Index_Op_Speed = 4;


        /// <summary>
        /// 数据推送事件
        /// </summary>
        public event PushDataEventHandler PushDataEvent;

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

        COMMREQ COMMREQ_RN;
        public FlyAd2021B2Core()
        {
            logger = NLog.LogManager.GetCurrentClassLogger();

            //COMMREQ_RN 作为事件,会触发一次,作为指令,也会回复一次
            COMMREQ_RN = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RN"),
                ResponseLen = 5,
                ParseFuncPack = (pack, dataIdx) =>
                {
                    DRIVE_MAN_STATUS result = (DRIVE_MAN_STATUS)pack[dataIdx];
                    dataIdx += 1;
                    UInt32 serial = BitConverter.ToUInt32(pack, dataIdx);
                    dataIdx += 4;

                    return new GetRunResult_Response()
                    {
                        result = result,
                        serial = serial
                    };
                }
            };
        }


        protected override void ParsePackAfterCheckCRC8(byte[] pack) 
        {
            byte B0 = pack[0];
            bool PT = Misc.MyBase.CHECKBIT(B0, 7);
            if (!PT)
            {
                ParseDataPack(pack);
            }
            else
            {

                ParseFuncPack(pack);
            }
        }

        void ParseDataPack(byte[] pack)
        {
            PushDataEventArgs eventArgs = new PushDataEventArgs();

            int ReponseLen = 4;  //B0(1)+B1(1)+AD(2)
            if (ReponseLen > pack.Count())
            {
                //失败,指令长度不对!!
                ErrCnt++;
                return;
            }

            byte systick = pack[0];
            Misc.MyBase.CLEARBIT(ref systick, 7);
            eventArgs.SysTick = systick;

            byte B1 = pack[1];
            bool hasIn = Misc.MyBase.CHECKBIT(B1, 7);
            bool hasENC1 = Misc.MyBase.CHECKBIT(B1, 6);
            bool hasENC2 = Misc.MyBase.CHECKBIT(B1, 5);
            bool hasOut = Misc.MyBase.CHECKBIT(B1, 4);
            bool hasAd2 = Misc.MyBase.CHECKBIT(B1, 3);

            var buf = pack;
            int index = 2;

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

            if (hasIn)
            {
                ReponseLen += 4;//IN(2)+ICHANGE(2)
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }


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

            if (hasENC1)
            {
                ReponseLen += 2;//ENC1(2)
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

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

            if (hasENC2)
            {
                ReponseLen += 2;//ENC2(2)
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

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

            if (hasOut)
            {
                ReponseLen += 2;//OUT(2)
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

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

            if (hasAd2)
            {
                ReponseLen += 2;//AD2(2)
                if (ReponseLen > pack.Count())
                {
                    //失败,指令长度不对!!
                    ErrCnt++;
                    return;
                }

                eventArgs.AD2 = BitConverter.ToUInt16(buf, index);
                index += 2;
            }
            PushDataEvent?.Invoke(this, eventArgs);
        }
        void ParseFuncPack_PushRunResultEvent(byte[] pack)
        {

            PushRunResultEventArgs eventArgs = new PushRunResultEventArgs();

            var retData = COMMREQ_RN.ParseFuncPack(pack, 1 + COMMREQ_RN.Prefix.Count()) as GetRunResult_Response;
            eventArgs.Status = retData.result;
            eventArgs.Serial = retData.serial;

            //logger.Debug($"PushRunResultEvent {Newtonsoft.Json.JsonConvert.SerializeObject(retData)}");

            byte systick = pack[0];
            Misc.MyBase.CLEARBIT(ref systick, 7);
            eventArgs.SysTick = systick;
            PushRunResultEvent?.Invoke(this, eventArgs);
        }
        
        
        
        /// <summary>
        /// 功能包解析
        /// </summary>
        /// <param name="datas"></param>
        protected override void ParseFuncPack(byte[] pack)
        {
            //TODO 要处理  TimeOut / ParseFuncPack / GetSendMsg 线性同步问题

            //优先处理 PushRunResultEvent 事件
            if (Is_RN_Match(pack))
            {
                ParseFuncPack_PushRunResultEvent(pack);
            }

            base.ParseFuncPack(pack);
        }

        /// <summary>
        /// 数据发送,输入不需要包含开头的7E; 输出会添加CRC8 和 7E
        /// </summary>
        /// <param name="datas"></param>
        protected override List<byte> GetSendPack(List<byte> datas)
        {
            datas.Insert(0, 0x80);//插入B0
            return base.GetSendPack(datas);
        }


        bool Is_RN_Match(byte[] pack)
        {
            //datas[0] 是B0, 不应该用于判断
            if (pack.Count() < COMMREQ_RN.Prefix.Length + PrefixIndex)
                return false;
            return base.IsMatch(COMMREQ_RN, pack);
        }

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(GetEncAll_Response))]
        public void GetEncAll(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("IGPA"),
                ResponseLen = 8,
                ParseFuncPack = (pack, dataIdx) =>
                {
                    int pos1 = BitConverter.ToInt32(pack, dataIdx);
                    dataIdx += 4;
                    int pos2 = BitConverter.ToInt32(pack, dataIdx);
                    dataIdx += 4;
                    return new GetEncAll_Response()
                    {
                        pos1 = pos1,
                        pos2 = pos2
                    };
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void SetOutPorts(UInt16 mask, UInt16 value, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(mask));
            datas.AddRange(BitConverter.GetBytes(value));
            var datasObj = new { mask, value };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }
        #endregion

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

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        public void SetRunParam_SV(UInt32 sv, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(sv));
            var datasObj = new { sv };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        public void SetRunParam_AccTime(UInt32 accTime, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(accTime));
            var datasObj = new { accTime };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        public void SetRunParam_DecTime(UInt32 decTime, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(decTime));
            var datasObj = new { decTime };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        public void SetRunParam_HSpd1(UInt32 homespd1, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(homespd1));
            var datasObj = new { homespd1 };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        public void SetRunParam_HSpd2(UInt32 homespd2, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(homespd2));
            var datasObj = new { homespd2 };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        #endregion

        #region RunComm 运行指令 GetRunParam
        public void GetRunParam_V(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RpV"),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_SV(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RpS"),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_AccTime(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RpU"),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_DecTime(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RpD"),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_HSpd1(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("Rp1"),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void GetRunParam_HSpd2(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("Rp2"),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) =>
                {
                    return BitConverter.ToUInt32(pack, dataIdx);
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        #endregion

        #region RunComm 运行指令
        public void GetRunResult(CallBackHandler asyncDelegate, object asyncContext)
        {
            //COMMREQ_RN 作为事件,会触发一次,作为指令,也会回复一次
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RN"),
                ResponseLen = 5,
                ParseFuncPack = (pack, dataIdx) =>
                {
                    DRIVE_MAN_STATUS result = (DRIVE_MAN_STATUS)pack[dataIdx];
                    dataIdx += 1;
                    UInt32 serial = BitConverter.ToUInt32(pack, dataIdx);
                    dataIdx += 4;

                    return new GetRunResult_Response()
                    {
                        result = result,
                        serial = serial
                    };
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        public void Forw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(serial));
            var datasObj = new { serial };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        public void Backw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(serial));
            var datasObj = new { serial };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }
        public void Org(Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(serial));
            var datasObj = new { serial };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        public void RunTo(int targetPos, Int32 serial, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.Add((byte)'P');
            datas.AddRange(BitConverter.GetBytes(targetPos));
            datas.AddRange(BitConverter.GetBytes(serial));
            var datasObj = new { targetPos, serial };

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }
        public void Stop(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RS")
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        public void EStop(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("RT")
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        #endregion

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        #endregion

        #region GetSysParam 读运行参数
        [CallBack(typeof(MOTORTYPE))]
        public void GetSysParam_MotorType(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PG",P_Saved_Index_Op_Mode),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) => 
                {
                    byte motorType = pack[dataIdx];
                    motorType &= 3;
                    return (MOTORTYPE)motorType;
                }
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(UInt16))]
        public void GetSysParam_Ratio01(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PG", P_Saved_Index_Op_Motor),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToUInt16(pack, dataIdx)
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(UInt16))]
        public void GetSysParam_Ratio02(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PG", P_Saved_Index_Op_Encoder),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToUInt16(pack, dataIdx)
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(Int16))]
        public void GetSysParam_Zero(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PG", P_Saved_Index_Op_Shift),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToInt16(pack, dataIdx)
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }

        [CallBack(typeof(UInt32))]
        public void GetSysParam_Jog(CallBackHandler asyncDelegate, object asyncContext)
        {
            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PG", P_Saved_Index_Op_Speed),
                ResponseLen = 4,
                ParseFuncPack = (pack, dataIdx) => BitConverter.ToUInt32(pack, dataIdx)
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        #endregion

        #region SetSysParam 设置运行参数


        public void SetSysParam_MotorType(MOTORTYPE motorType, CallBackHandler asyncDelegate, object asyncContext)
        {
            UInt32 request = (UInt32)motorType;
            request |= (0x3 << 2);
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes(request));

            var datasObj = new { motorType };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PS", P_Saved_Index_Op_Mode)
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }


        public void SetSysParam_Ratio01(UInt16 ratio01, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes((UInt32)ratio01));
            var datasObj = new { ratio01 };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PS", P_Saved_Index_Op_Motor)
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        public void SetSysParam_Ratio02(UInt16 ratio02, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes((UInt32)ratio02));
            var datasObj = new { ratio02 };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PS",P_Saved_Index_Op_Encoder)
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

        public void SetSysParam_Zero(Int16 zero, CallBackHandler asyncDelegate, object asyncContext)
        {
            List<byte> datas = new List<byte>();
            datas.AddRange(BitConverter.GetBytes((Int32)zero));
            var datasObj = new { zero };

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PS",P_Saved_Index_Op_Shift)
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

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

            COMMREQ commReq = new COMMREQ()
            {
                Prefix = COMMREQ.ToPrefix("PS", P_Saved_Index_Op_Speed)
            };

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext,
                    datas = datas.ToArray(),
                    datasObj = datasObj
                });
        }

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

            //放入 交易队列
            AddTran(
                new COMMREQ_Transaction()
                {
                    commReq = commReq,
                    asyncDelegate = asyncDelegate,
                    asyncContext = asyncContext
                });
        }
        #endregion

    }
}