FlyAD7.cs 67.7 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using AutoMapper;
潘栩锋's avatar
潘栩锋 committed
2
using FObjBase;
3
using Misc;
潘栩锋's avatar
潘栩锋 committed
4 5
using Newtonsoft.Json;
using System;
6
using System.Collections.Generic;
潘栩锋's avatar
潘栩锋 committed
7
using System.Collections.ObjectModel;
潘栩锋's avatar
潘栩锋 committed
8
using System.ComponentModel;
9
using System.Diagnostics;
潘栩锋's avatar
潘栩锋 committed
10 11 12
using System.IO;
using System.Linq;
using System.Net;
潘栩锋's avatar
潘栩锋 committed
13 14 15 16 17 18 19 20 21

namespace FlyADBase
{
    /// <summary>
    /// 执行顺序
    /// 1. IFlyAD flyad = new FlyAD7();//加载速度参数,IP地址
    /// 2. flyad.Connect();//flyad.Connect("192.168.251.10:20006");
    /// 3. 当连接上,会设置推送,获取flyad7控制器里面所有数据,设置速度参数到控制器
    /// </summary>
22
    public partial class FlyAD7 : FObj, IFlyADClient
潘栩锋's avatar
潘栩锋 committed
23 24
    {
        #region MARKNO
25 26
        const int MARKNO_SET_ZEROPOS = 2;
        const int MARKNO_SET_SYSPARAM = 3;
27
        const int MARKNO_SET_SAVE = 4;
潘栩锋's avatar
潘栩锋 committed
28
        #endregion
29

30

31 32 33 34 35 36 37 38 39 40
        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        #region IFlyADClient property
潘栩锋's avatar
潘栩锋 committed
41
        /// <summary>
42
        /// 连接成功
潘栩锋's avatar
潘栩锋 committed
43
        /// </summary>
44 45 46 47 48 49 50 51 52 53 54 55
        public bool IsConnected { get; private set; }

        /// <summary>
        /// 重连次数
        /// </summary>
        public int ConnectCnt { get; private set; }

        /// <summary>
        /// CRC
        /// </summary>
        public bool HasCRC { get; set; }

潘栩锋's avatar
潘栩锋 committed
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

        /// <summary>
        /// 已经从AD盒获取全部当前数据
        /// </summary>
        public bool IsReady { get; protected set; }

        /// <summary>
        ///  通过判断systick,判断AD盒是否发生重启;
        ///  BeReseted为上一次被复位时间
        /// </summary>
        public DateTime BeResetTime { get; private set; }
        /// <summary>
        /// 用于同步, 最后一次 纵向信号 0->1 时,主轴脉冲
        /// </summary>
        public bool IsSync { get; protected set; }

        /// <summary>
        /// 只要connect成功,获取systick被复位,都会从AD盒设备读取参数;
        /// 否则, 设置参数 到 AD盒
        /// </summary>
        public bool IsReadParamFromDev { get; set; }
潘栩锋's avatar
潘栩锋 committed
78 79 80 81
        /// <summary>
        /// 保存的设备参数有效
        /// </summary>
        public bool IsDevParamValid { get; set; }
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

        /// <summary>
        /// 同步列表,完成后,会删除
        /// </summary>
        public ObservableCollection<SyncOrder> SyncOrders { get; } = new ObservableCollection<SyncOrder>();

        #endregion
        #region IFlyAD property


        /// <summary>
        /// AD盒地址
        /// </summary>
        public string Addr { get; set; } = "192.168.251.10:20006";
        IPEndPoint LocalEP => Misc.StringConverter.ToIPEndPoint(Addr);

        /// <summary>
        /// 从flyad7 获取 的systick 转换的时间
        /// </summary>
        public DateTime Now { get; private set; }

        /// <summary>
        /// AD盒版本号 版本1 没有systick;版本2 有systick;
        /// </summary>
        public int Version
潘栩锋's avatar
潘栩锋 committed
107
        {
108
            get
潘栩锋's avatar
潘栩锋 committed
109
            {
110 111 112 113 114 115
                if (pos1_version == 2
                    && timegrid_version == 2
                    && grid_version == 2)
                    return 2;
                else
                    return 1;
潘栩锋's avatar
潘栩锋 committed
116
            }
117
        }
118

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        /// <summary>
        /// 硬件版本,设置的!!!
        /// </summary>
        public int HardwareVersion { get; set; } = 2;
        /// <summary>
        /// 脉冲
        /// </summary>
        public int Position { get; private set; }

        /// <summary>
        /// 速度 pps
        /// </summary>
        public int Speed { get; private set; }

        /// <summary>
        /// 纵向脉冲,也叫主轴脉冲
        /// </summary>
        public int Position2 { get; private set; }
        /// <summary>
        /// 纵向脉冲速度, pps, 1秒脉冲数
        /// </summary>
        public int Speed2 { get; private set; }

        /// <summary>
        /// 1000ms从AD卡获取(或推送)1个AD值。此AD值只用于调试,定点
        /// </summary>
        public int AD { get; private set; }


        /// <summary>
        /// AD值最大值 ,恒定返回65535
        /// </summary>
        public int ADMax => 0xFFFF;

        /// <summary>
        /// 输入口状态,只有12位有效, 大AD盒 16位
        /// </summary>
        public UInt16 IStatus { get; private set; } = 0xffff;

        /// <summary>
        /// 输出口状态,只有4位有效, 大AD盒 8位
        /// </summary>
        public UInt16 OStatus { get; private set; } = 0xffff;


        /// <summary>
        /// 电机类型
        /// </summary>
        public MOTORTYPE MotorType { get; set; } = MOTORTYPE.SERVO;

        private UInt16 posofgrid = 10;
        /// <summary>
        /// 脉冲/grid, 最大1000个grid
        /// </summary>
        public UInt16 PosOfGrid
        {
            get { return posofgrid; }
            set
潘栩锋's avatar
潘栩锋 committed
177
            {
178 179 180 181 182 183
                if (value < 1)
                    value = 1;

                if (posofgrid != value)
                    posofgrid = value;

潘栩锋's avatar
潘栩锋 committed
184 185 186
            }
        }

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

        /// <summary>
        /// 脉冲比例 分母(电机脉冲)
        /// </summary>
        public UInt16 Ratio01 { get; set; } = 4;

        /// <summary>
        /// 脉冲比例 分子(编码器脉冲)
        /// </summary>
        public UInt16 Ratio02 { get; set; } = 1;

        /// <summary>
        /// 脉冲平移(0位处脉冲值)
        /// </summary>
        public Int16 PosOffset { get; set; }

        /// <summary>
        /// 手动速度 pps
        /// </summary>
        public UInt32 JogVelocity { get; set; } = 5000;

        /// <summary>
        /// runto 速度 pps
        /// </summary>
        public UInt32 Velocity { get; private set; } = 8000;

        /// <summary>
        /// 开始速度 pps
        /// </summary>
        public UInt32 SVelocity { get; private set; } = 200;

        /// <summary>
        /// 加速时间 ms
        /// </summary>
        public UInt32 ATime { get; private set; } = 300;

        /// <summary>
        /// 减速时间 ms
        /// </summary>
        public UInt32 DTime { get; private set; } = 200;

        /// <summary>
        /// 归0 第1段速 pps
        /// </summary>
        public UInt32 HVelocity1 { get; private set; } = 4000;

        /// <summary>
        /// 归0 第2段速 pps
        /// </summary>
        public UInt32 HVelocity2 { get; private set; } = 500;


        /// <summary>
        /// 使用码区状态
        /// </summary>
        public AREA_STATUS AreaStatus { get; set; }

        /// <summary>
        /// 使用码区出错码
        /// </summary>
        public AREA_ERR AreaRet { get; set; }

        /// <summary>
        /// 序列码byte[6]
        /// </summary>
        public byte[] Code { get; } = new byte[7];


        /// <summary>
        /// //系统剩余时间
        /// </summary>
        public int Surplus { get; set; } = 0xffff;
        /// <summary>
        /// 系统授权码
        /// </summary>
        public byte[] Access { get; } = new byte[8];


        /// <summary>
        /// 当前运行指令
        /// </summary>
        public DRIVE_MAN_ORDER DriveOrder { get; set; } = DRIVE_MAN_ORDER.IDLE;

        /// <summary>
        /// 之前运行动作的状态
        /// </summary>
        public DRIVE_MAN_STATUS DriveStatus { get; set; } = DRIVE_MAN_STATUS.STOP;

        /// <summary>
        /// 同步用标示
        /// </summary>
        public Int32 Marker { get; protected set; }


        /// <summary>
        /// 逻辑横向脉冲偏移  Pos1 + Pos1LCShift = Pos1LC
        /// </summary>
        public int Pos1LCShift { get; set; }
        /// <summary>
        /// 逻辑纵向脉冲偏移  Pos2 + Pos2LCShift = Pos2LC
        /// </summary>
        public int Pos2LCShift { get; set; }

        /// <summary>
        /// timegrid 事件; 版本1: 1.28s 一次; 版本2: 0.2s一次, 
        /// ad值不会被机架修正
        /// </summary>
        public event TimeGridEventHandler TimeGridEvent;



        /// <summary>
        /// runto 中,每经过1个grid 推送数据
        /// </summary>
        public event MiniGridEventHandler MiniGridEvent;

        /// <summary>
        /// runto 的 grid事件
        /// </summary>
        public event MiniGridEventHandler GridEvent;

        /// <summary>
        /// 输入状态改变事件
        /// </summary>
        public event IStatusChangedEventHandler IStatusChangedEvent;

        /// <summary>
        /// 指令状态改变事件
        /// </summary>
        public event DriveStatusChangedEventHandler DriveStatusChangedEvent;

        /// <summary>
        /// 脉冲1改变事件
        /// </summary>
        public event PositionChangedEventHandler PositionChangedEvent;

        /// <summary>
        /// 脉冲2改变事件
        /// </summary>
        public event PositionChangedEventHandler Position2ChangedEvent;

        #endregion


        private int pos1_version { get; set; } = 2;
        private int io_version { get; set; } = 2;
        private int timegrid_version { get; set; } = 2;
        private int grid_version { get; set; } = 2;

336
        TCPCConn conn = null;
潘栩锋's avatar
潘栩锋 committed
337 338 339
        /// <summary>
        /// 不设置服务器
        /// </summary>
340
        bool bShieldSetValueEx = false;
341

潘栩锋's avatar
潘栩锋 committed
342

343 344 345 346
        /// <summary>
        /// 刚发送了运行指令,正常情况 AD盒会立刻推送状态给电脑,
        /// 但怕它不推送,等3秒,再问它获取一次。
        /// </summary>
347
        DtAndBool driveman_wait = new DtAndBool();
348 349 350 351 352
        /// <summary>
        /// 用于检测当 为IsRunning时,脉冲是否停了很久。
        /// </summary>
        int last_position = int.MinValue;

潘栩锋's avatar
潘栩锋 committed
353 354


355 356
        TimeSpan NoTGridTimeOut = TimeSpan.FromSeconds(60);
        Stopwatch stopwatch_noTGrid = new Stopwatch();
357

358

潘栩锋's avatar
潘栩锋 committed
359
        
360 361 362 363 364 365 366 367 368 369

        IsReadyContext isReadyContext = new IsReadyContext();
        SysTickContext sysTickContext = new SysTickContext();
        CalSpeed calSpeed = new CalSpeed();
        static string[] propertyName_save;

        string defaultPath = "flyad.json";
        string jsonDbPath;

        DateTime lastTimeGridTime;
370 371 372
        static FlyAD7() {
            propertyName_save = FlyAD7JsonDb.GetMemberNames();
        }
373 374 375
        /// <summary>
        /// 
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
376
        public FlyAD7()
377 378 379 380 381
        {
            constructor();
            advConstructor();
        }
        void constructor()
潘栩锋's avatar
潘栩锋 committed
382 383
        {
            Now = DateTime.Now;
384 385
            sysTickContext.BeReseted += () => BeResetTime = DateTime.Now;

潘栩锋's avatar
潘栩锋 committed
386 387
            this.PropertyChanged += new PropertyChangedEventHandler(FlyAD7_PropertyChanged);

388
            //每隔一个小时,改变剩下时间
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD, () =>
            {
                if (!IsConnected)
                    return;
                if (Surplus < 60000)
                {
                    if (Surplus > 0)
                    {
                        Surplus--;
                        CurrObjSys.CallFunctionEx(conn, FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID, ID,
                            FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.CALL_SURPLUS_SUB,
                            null);
                    }
                }
            }, TimeSpan.FromMinutes(60));
404

405
            //60秒内收不到1个timegrid ,就会重连
潘栩锋's avatar
潘栩锋 committed
406 407 408 409 410
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD,
                () =>
                {
                    if (!IsConnected)
                    {
411
                        stopwatch_noTGrid.Stop();
潘栩锋's avatar
潘栩锋 committed
412 413 414
                    }
                    else
                    {
415 416
                        if (!stopwatch_noTGrid.IsRunning)
                            stopwatch_noTGrid.Restart();
潘栩锋's avatar
潘栩锋 committed
417 418 419 420
                        else
                        {
                            if (stopwatch_noTGrid.Elapsed > NoTGridTimeOut)
                            {
421 422 423 424
                                logger.Error("60秒都无法接收到ad盒的timegrid,reconnect");
                                ReConnect();
                                stopwatch_noTGrid.Restart();
                            }
潘栩锋's avatar
潘栩锋 committed
425 426 427
                        }
                    }
                }, TimeSpan.FromSeconds(1));
428

429 430
            //每隔1秒发送一次 GetIO
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD, () =>
431
            {
432 433
                if (!IsConnected)
                    return;
434

435 436 437 438 439 440 441
                //FLYIO
                CurrObjSys.GetValueEx(
                    conn,
                    FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID,
                    ID,
                    FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.GET_IO);
            }, TimeSpan.FromSeconds(1));
442

443 444 445 446 447 448 449 450 451
            //更新线速度
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD,
                () =>
                {
                    if (calSpeed.Cal(Now, out int speed1, out int speed2))
                    {
                        Speed = speed1;
                        Speed2 = speed2;
                    }
潘栩锋's avatar
潘栩锋 committed
452

453
                }, TimeSpan.FromSeconds(1));
潘栩锋's avatar
潘栩锋 committed
454

455 456 457
            //当状态为Running时,且每秒脉冲没变,需要 查 当前的状态, 也许Stop状态少推送了
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD,
                () =>
458
                {
459 460 461 462
                    if (!IsConnected)
                        return;
                    if (DriveStatus != DRIVE_MAN_STATUS.RUNNING)
                        return;
潘栩锋's avatar
潘栩锋 committed
463

464 465 466 467 468
                    if (Position != last_position)
                    {
                        last_position = Position;
                        return;
                    }
潘栩锋's avatar
潘栩锋 committed
469

470 471 472 473 474 475 476 477 478
                    if (driveman_wait.CheckMark(TimeSpan.FromSeconds(3)))//每3秒检查一次
                    {
                        driveman_wait.Mark();
                        CurrObjSys.GetValueEx(
                            conn,
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                            ID,
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_STATE);
                    }
潘栩锋's avatar
潘栩锋 committed
479

480
                }, TimeSpan.FromSeconds(1));
481 482

            Misc.BindingOperations.SetBinding(isReadyContext, nameof(isReadyContext.IsReady), this, nameof(IsReady));
483
        }
潘栩锋's avatar
潘栩锋 committed
484 485


潘栩锋's avatar
潘栩锋 committed
486 487
        void FlyAD7_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
488
            if (e.PropertyName == nameof(HasCRC))
潘栩锋's avatar
潘栩锋 committed
489 490 491 492 493 494 495 496
            {
                if (conn != null)
                    conn.HasCRC = HasCRC;
            }

            if (bShieldSetValueEx)
                return;

497
            if ((e.PropertyName == nameof(PosOffset)) ||
498
                (e.PropertyName == nameof(JogVelocity)))
潘栩锋's avatar
潘栩锋 committed
499
            {
500
                PollModule.Current.Poll_JustOnce(
501
                    delegate ()
潘栩锋's avatar
潘栩锋 committed
502 503 504 505 506 507 508 509 510 511
                    {
                        CurrObjSys.SetValueEx(conn, FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                            ID, FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.SET_ZERO_POS,
                            new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos()
                            {
                                zero_pos = PosOffset,
                                jog_velocity = JogVelocity
                            }.ToBytes());
                    }, this, MARKNO_SET_ZEROPOS);
            }
512 513 514 515
            else if ((e.PropertyName == nameof(MotorType)) ||
                    (e.PropertyName == nameof(PosOfGrid)) ||
                    (e.PropertyName == nameof(Ratio01)) ||
                    (e.PropertyName == nameof(Ratio02)))
潘栩锋's avatar
潘栩锋 committed
516
            {
517
                PollModule.Current.Poll_JustOnce(
518
                    delegate ()
潘栩锋's avatar
潘栩锋 committed
519 520 521 522
                    {
                        SetSysParam(PosOfGrid, MotorType, Ratio01, Ratio02);
                    }, this, MARKNO_SET_SYSPARAM);
            }
523
            else if (e.PropertyName == nameof(Pos1LCShift))
潘栩锋's avatar
潘栩锋 committed
524
            {
525

潘栩锋's avatar
潘栩锋 committed
526 527 528 529 530 531 532 533 534 535
                CurrObjSys.CallFunctionEx(
                   conn,
                   FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                   ID,
                   FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                   new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos1LCShift()
                   {
                       pos1_LCShift = Pos1LCShift
                   }.ToBytes());
            }
潘栩锋's avatar
潘栩锋 committed
536 537 538 539 540 541 542 543 544 545 546 547 548
            else if (e.PropertyName == nameof(Pos2LCShift))
            {

                CurrObjSys.CallFunctionEx(
                    conn,
                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                    ID,
                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                    new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_Pos2Shift()
                    {
                        pos2_shift = Pos2LCShift
                    }.ToBytes());
            }
潘栩锋's avatar
潘栩锋 committed
549 550
        }

551 552 553 554
        /// <summary>
        /// 连接后初始化
        /// </summary>
        void AfterConnected()
潘栩锋's avatar
潘栩锋 committed
555
        {
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
            //CurrObjSys.SenseConfigEx(
            //    conn,
            //    FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.ID,
            //    ID,
            //    Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS1)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS2)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_AD1)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_IO)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_TIMEGRID)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_GRID)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_STATUS)
            //    | Misc.MyBase.BIT(FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_MINIGRID)
            //    ,
            //    SENSE_CONFIG.ADD);

            //注册全部推送事件
潘栩锋's avatar
潘栩锋 committed
572 573 574 575
            CurrObjSys.SenseConfigEx(
                conn,
                FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.ID,
                ID,
576
                0xffffffff,
潘栩锋's avatar
潘栩锋 committed
577
                SENSE_CONFIG.ADD);
578 579


580
            if (IsReadParamFromDev || !IsDevParamValid)
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
            {
                //读取全部参数!!!!!
                //OBJ_SYS_DATA
                CurrObjSys.GetValueEx(
                    conn,
                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                    ID,
                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSPARAM);


                CurrObjSys.GetValueEx(
                    conn,
                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                    ID,
                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_ZERO_POS);
            }
            else {
                //参数不保存在设备中, 保存在电脑。
                //参数写入到设备。
                NotifyPropertyChanged(nameof(MotorType));
                NotifyPropertyChanged(nameof(PosOfGrid));
                NotifyPropertyChanged(nameof(Ratio01));
                NotifyPropertyChanged(nameof(Ratio02));
                NotifyPropertyChanged(nameof(PosOffset));
                NotifyPropertyChanged(nameof(JogVelocity));
            }

608 609 610 611 612 613 614
            //DRIVE_MAN
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_DRIVEPARAM);

615 616 617 618 619 620
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSINFO);

潘栩锋's avatar
潘栩锋 committed
621 622 623 624 625 626 627 628 629


            //DRIVE_MAN
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_STATE);

630 631 632 633 634 635 636 637 638 639 640 641
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_ORDER_CNT);

            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_STATUS);

潘栩锋's avatar
潘栩锋 committed
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
            //FLYADC
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.GET_POS1AD1);

            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.GET_POS2);

            //FLYIO
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.GET_IO);

662 663
            

664
            NotifyPropertyChanged(nameof(Pos1LCShift));//写入到AD盒
665
            NotifyPropertyChanged(nameof(Pos2LCShift));//写入到AD盒
666

潘栩锋's avatar
潘栩锋 committed
667 668 669 670

            SyncClear();
            SyncEnd();

671 672
            isReadyContext.Reset();
            sysTickContext.Reset();
潘栩锋's avatar
潘栩锋 committed
673
            calSpeed.Reset();
674
            lastTimeGridTime = DateTime.MinValue;
675 676

            advAfterContected();
潘栩锋's avatar
潘栩锋 committed
677 678 679 680 681 682 683 684 685
        }

        #region FObj
        public override void ConnectNotify(IFConn from)
        {
            IsConnected = from.IsConnected;
            if (IsConnected)
            {
                ConnectCnt++;
686

687
                AfterConnected();
潘栩锋's avatar
潘栩锋 committed
688 689 690
            }
            else
            {
691
                DriveOrder = DRIVE_MAN_ORDER.IDLE;
潘栩锋's avatar
潘栩锋 committed
692 693 694
                DriveStatus = DRIVE_MAN_STATUS.STOP;
            }
        }
695 696 697 698 699 700 701
        /// <summary>
        /// 
        /// </summary>
        /// <param name="from"></param>
        /// <param name="srcid"></param>
        /// <param name="memid"></param>
        /// <param name="infodata"></param>
潘栩锋's avatar
潘栩锋 committed
702 703 704 705 706 707 708 709 710 711 712
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            bShieldSetValueEx = true;
            switch (srcid)
            {
                case FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.ID:
                    {
                        switch (memid)
                        {
                            case FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.GET_POS1AD1:
                                {
713
                                    var pack = new FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.Pack_GetPos1AD1();
潘栩锋's avatar
潘栩锋 committed
714 715 716
                                    if (!pack.TryParse(infodata))
                                        break;
                                    Position = pack.pos;
潘栩锋's avatar
潘栩锋 committed
717 718
                                    //AD = pack.ad;
                                    advGetPos1AD1(pack.ad);
719

720
                                    isReadyContext.isReadyGetPos1AD1 = true;
潘栩锋's avatar
潘栩锋 committed
721 722
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
723 724 725 726 727 728 729
                            case FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.GET_POS2:
                                {
                                    FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.Pack_GetPos2 pack = new FLYAD7_OBJ_INTERFACE.FLYADC_OBJ_INTERFACE.Pack_GetPos2();
                                    if (!pack.TryParse(infodata))
                                        break;

                                    Position2 = pack.pos2;
730

潘栩锋's avatar
潘栩锋 committed
731
                                    advGetPos2();
732

733
                                    isReadyContext.isReadyGetPos2 = true;
734 735
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
736
                        }
潘栩锋's avatar
潘栩锋 committed
737 738
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
739 740 741 742 743 744
                case FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID:
                    {
                        switch (memid)
                        {
                            case FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.GET_IO:
                                {
745
                                    var pack = new FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.Pack_GetIO();
潘栩锋's avatar
潘栩锋 committed
746 747 748 749 750
                                    if (!pack.TryParse(infodata))
                                        break;

                                    IStatus = pack.istatus;
                                    OStatus = pack.ostatus;
751

潘栩锋's avatar
潘栩锋 committed
752
                                    advGetIo();
753

754
                                    isReadyContext.isReadyGetIO = true;
潘栩锋's avatar
潘栩锋 committed
755 756
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
757
                        }
潘栩锋's avatar
潘栩锋 committed
758 759
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
                case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID:
                    {
                        switch (memid)
                        {
                            case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_DRIVEPARAM:
                                {
                                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam pack = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam();

                                    if (!pack.TryParse(infodata))
                                        break;
                                    Velocity = pack.Velocity;
                                    SVelocity = pack.SV;
                                    ATime = pack.ATime;
                                    DTime = pack.DTime;
                                    HVelocity1 = pack.hspeed1;
                                    HVelocity2 = pack.hspeed2;

潘栩锋's avatar
潘栩锋 committed
777 778
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
779 780
                            case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_STATE:
                                {
781 782 783
                                    int version;
                                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveState_2 pack = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveState_2();
                                    if (!pack.TryParse_version(infodata, out version))
潘栩锋's avatar
潘栩锋 committed
784
                                        break;
785

786 787 788 789 790 791 792 793 794 795 796 797 798
                                    if (version == 1)
                                    {
                                        DriveOrder = pack.order;
                                        DriveStatus = pack.status;
                                    }
                                    else if (version == 2)
                                    {
                                        DriveOrder = pack.order;
                                        DriveStatus = pack.status;
                                        Marker = pack.marker;

                                        advGetState();
                                    }
799

800
                                    isReadyContext.isReadyGetState = true;
潘栩锋's avatar
潘栩锋 committed
801 802
                                }
                                break;
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
                            case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_STATUS:
                                {
                                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncStatus p = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncStatus();
                                    if (!p.TryParse(infodata))
                                        break;
                                    //TODO
                                }
                                break;
                            case FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_SYNC_ORDER_CNT:
                                {
                                    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrderCnt p = new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrderCnt();
                                    if (!p.TryParse(infodata))
                                        break;
                                    //TODO
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
819
                        }
潘栩锋's avatar
潘栩锋 committed
820 821
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
                case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID:
                    {
                        switch (memid)
                        {
                            case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_IP:
                                break;
                            case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSPARAM:
                                {
                                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam();

                                    if (!pack.TryParse(infodata))
                                        break;
                                    PosOfGrid = pack.posOfGrid;//1个Grid=?脉冲数(2B)
                                    if ((pack.func_enable & 0x03) == 0x02)
                                        MotorType = MOTORTYPE.SERVO;
                                    else if ((pack.func_enable & 0x03) == 0x01)
                                        MotorType = MOTORTYPE.VF0;
                                    else
                                        MotorType = MOTORTYPE.NULL;

                                    Ratio01 = pack.ratio01;
                                    Ratio02 = pack.ratio02;
844
                                    IsDevParamValid = true;
潘栩锋's avatar
潘栩锋 committed
845 846
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
847 848 849 850 851 852 853 854 855 856 857 858
                            case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSINFO:
                                {
                                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysInfo pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysInfo();

                                    if (!pack.TryParse(infodata))
                                        break;

                                    AreaStatus = pack.status;
                                    AreaRet = pack.ret;

                                    Array.Copy(pack.code, Code, pack.code.Length);
                                    Code[6] = 0x06;
859
                                    NotifyPropertyChanged(nameof(Code));
潘栩锋's avatar
潘栩锋 committed
860 861 862 863

                                    Surplus = pack.surplus;

                                    Array.Copy(pack.access, Access, pack.access.Length);
864
                                    NotifyPropertyChanged(nameof(Access));
潘栩锋's avatar
潘栩锋 committed
865

潘栩锋's avatar
潘栩锋 committed
866 867
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
868 869 870 871 872 873 874 875 876
                            case FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_ZERO_POS:
                                {
                                    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos pack = new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_ZeroPos();

                                    if (!pack.TryParse(infodata))
                                        break;

                                    PosOffset = pack.zero_pos;
                                    JogVelocity = pack.jog_velocity;
877
                                    IsDevParamValid = true;
潘栩锋's avatar
潘栩锋 committed
878 879
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
880
                        }
潘栩锋's avatar
潘栩锋 committed
881 882
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
883 884 885 886
            }
            bShieldSetValueEx = false;
        }

887

888

潘栩锋's avatar
潘栩锋 committed
889 890 891 892 893 894 895 896 897
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            bShieldSetValueEx = true;
            switch (infoid)
            {
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS1:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos1_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos1_2();
                        int version;
898
                        if (!pack.TryParse_version(infodata, out version))
潘栩锋's avatar
潘栩锋 committed
899
                            break;
900 901 902
                        pos1_version = version;

                        if (version == 1)
潘栩锋's avatar
潘栩锋 committed
903
                        {
潘栩锋's avatar
潘栩锋 committed
904 905
                            Position = pack.pos1;
                            Now = DateTime.Now;
潘栩锋's avatar
潘栩锋 committed
906
                        }
907
                        else if (version == 2)
潘栩锋's avatar
潘栩锋 committed
908
                        {
潘栩锋's avatar
潘栩锋 committed
909
                            Position = pack.pos1;
910
                            DateTime dt = sysTickContext.ToDateTime(pack.systick);
潘栩锋's avatar
潘栩锋 committed
911
                            Now = dt;
912
                            
913
                            PositionChangedEvent?.Invoke(this, new PositionChangedEventArgs() { Time = dt, Position = Position });
潘栩锋's avatar
潘栩锋 committed
914
                        }
潘栩锋's avatar
潘栩锋 committed
915
                        calSpeed.SetPos1(Now, Position);
916 917

                        advPushPos1(version);
潘栩锋's avatar
潘栩锋 committed
918 919
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
920 921 922 923
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS2:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos2_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos2_2();
                        int version;
924
                        if (!pack.TryParse_version(infodata, out version))
潘栩锋's avatar
潘栩锋 committed
925
                            break;
926 927 928

                        if (version == 1)
                        {
潘栩锋's avatar
潘栩锋 committed
929 930
                            Position2 = pack.pos2;
                            Now = DateTime.Now;
931 932 933
                        }
                        else if (version == 2)
                        {
潘栩锋's avatar
潘栩锋 committed
934
                            Position2 = pack.pos2;
935
                            DateTime dt = sysTickContext.ToDateTime(pack.systick);
936
                            Now = dt;
937
                            
938 939
                            Position2ChangedEvent?.Invoke(this, new PositionChangedEventArgs() { Time = dt, Position = Position });
                        }
潘栩锋's avatar
潘栩锋 committed
940
                        calSpeed.SetPos2(Now, Position2);
941 942

                        advPushPos2(version);
潘栩锋's avatar
潘栩锋 committed
943 944
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
945 946 947 948 949 950
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_AD1:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushAD1 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushAD1();
                        if (!pack.TryParse(infodata))
                            break;

951 952
                        //AD = pack.AD;
                        advPushAd(pack.AD);
潘栩锋's avatar
潘栩锋 committed
953 954
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
955 956 957 958 959 960
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_IO:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushIO_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushIO_2();
                        int version;
                        if (!pack.TryParse_version(infodata, out version))
                            break;
961 962
                        io_version = version;
                        if (version == 1)
潘栩锋's avatar
潘栩锋 committed
963 964 965 966
                        {
                            UInt16 inchange = (UInt16)(IStatus ^ pack.istatus);
                            IStatus = pack.istatus;
                            OStatus = pack.ostatus;
潘栩锋's avatar
潘栩锋 committed
967
                            Now = DateTime.Now;
968

潘栩锋's avatar
潘栩锋 committed
969 970 971 972 973 974 975 976
                            IStatusChangedEvent?.Invoke(
                                this,
                                new IStatusChangedEventArgs(
                                    Now,
                                    IStatus,
                                    inchange,
                                    Position,
                                    Position2));
潘栩锋's avatar
潘栩锋 committed
977
                        }
978
                        else if (version == 2)
潘栩锋's avatar
潘栩锋 committed
979
                        {
980 981 982 983 984
                            if (pack.inChange == 0)
                            {
                                //异常包,, 这个是AD盒的bug,  systick 是错的!!!!! 不能让它通过
                                return;
                            }
潘栩锋's avatar
潘栩锋 committed
985 986
                            IStatus = pack.istatus;
                            OStatus = pack.ostatus;
987 988
                            Position = pack.pos1;
                            Position2 = pack.pos2;
989
                            DateTime dt = sysTickContext.ToDateTime(pack.systick);
潘栩锋's avatar
潘栩锋 committed
990
                            Now = dt;
991 992
                            calSpeed.SetPos1(Now, Position);
                            calSpeed.SetPos2(Now, Position2);
993

994 995 996 997 998 999
                            IStatusChangedEvent?.Invoke(
                                this,
                                new IStatusChangedEventArgs(
                                    dt,
                                    pack.istatus,
                                    pack.inChange,
潘栩锋's avatar
潘栩锋 committed
1000 1001
                                    Position,
                                    Position2));
潘栩锋's avatar
潘栩锋 committed
1002
                        }
1003
                        advPushIo(version);
潘栩锋's avatar
潘栩锋 committed
1004 1005
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1006 1007
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_TIMEGRID:
                    {
1008
                        stopwatch_noTGrid.Restart();//收到timegrid!!!!!!!!!!!
潘栩锋's avatar
潘栩锋 committed
1009 1010 1011 1012 1013 1014 1015
                        int version;
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushTGrid_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushTGrid_2();

                        if (!pack.TryParse_version(infodata, out version))
                        {
                            break;
                        }
1016
                        timegrid_version = version;
潘栩锋's avatar
潘栩锋 committed
1017 1018 1019

                        DateTime dt;
                        int[] data;
1020 1021 1022 1023 1024 1025
                        //怕数据发送出错
                        if (pack.data.Any(d => (d >> 16) != 0))
                        {
                            //异常
                            goto _end_push_info;
                        }
潘栩锋's avatar
潘栩锋 committed
1026 1027 1028 1029 1030

                        if (version == 1)
                        {
                            //怕数据发送出错
                            dt = DateTime.Now;
潘栩锋's avatar
潘栩锋 committed
1031
                            Now = dt;
潘栩锋's avatar
潘栩锋 committed
1032
                        }
1033
                        else// if (version == 2)
潘栩锋's avatar
潘栩锋 committed
1034
                        {
1035
                            dt = sysTickContext.ToDateTime(pack.systick);
潘栩锋's avatar
潘栩锋 committed
1036 1037 1038
                            Now = dt;
                        }
                        data = pack.data;
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063





                        //放入的数据应该是1ms一个,不对就扩展
                        TimeSpan curr_ad_ts = TimeSpan.FromTicks((long)(1.28 * TimeSpan.TicksPerMillisecond));
                        List<int> data_1ms = new List<int>();
                        DateTime idealTime = dt;
                        DateTime actTime = dt;
                        for (int j = 0; j < data.Count(); j++)
                        {
                            int idx2 = data.Count() - 1 - j;
                            int ad = data[idx2];
                            actTime -= curr_ad_ts;

                            while (idealTime > actTime)
                            {
                                data_1ms.Add(ad);//导出数据
                                idealTime -= TimeSpan.FromMilliseconds(1);//导出数据时间累加
                            }
                        }
                        data_1ms.Reverse();
                        data = data_1ms.ToArray();

1064 1065
                        advPushTimeGrid(dt, data);

1066
                        TimeSpan ts = TimeSpan.FromMilliseconds(1);
1067 1068 1069
                        if (lastTimeGridTime != DateTime.MinValue)
                            ts = TimeSpan.FromTicks((dt - lastTimeGridTime).Ticks / data.Length);
                        lastTimeGridTime = dt;
1070

1071 1072 1073 1074
                        TimeGridEvent?.Invoke(
                            this,
                            new TimeGridEventArgs(
                                dt,
1075
                                ts,
1076
                                data));
潘栩锋's avatar
潘栩锋 committed
1077 1078
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1079 1080 1081 1082 1083 1084
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_STATUS:
                    {
                        int version;
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushStatus_2 pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushStatus_2();
                        if (!pack.TryParse_version(infodata, out version))
                            break;
1085
                        if (version == 1)
潘栩锋's avatar
潘栩锋 committed
1086 1087 1088 1089
                        {
                            DriveOrder = pack.order;
                            DriveStatus = pack.status;
                        }
1090
                        else if (version == 2)
潘栩锋's avatar
潘栩锋 committed
1091 1092 1093
                        {
                            DriveOrder = pack.order;
                            DriveStatus = pack.status;
1094
                            Marker = pack.marker;
1095
                            DateTime dt = sysTickContext.ToDateTime(pack.systick);
1096
                            Now = dt;
1097 1098 1099 1100 1101 1102 1103 1104

                            DriveStatusChangedEvent?.Invoke(this, new DriveStatusChangedEventArgs()
                            {
                                Time = dt,
                                DriveOrder = DriveOrder,
                                DriveStatus = DriveStatus,
                                Marker = Marker,
                            });
潘栩锋's avatar
潘栩锋 committed
1105
                        }
1106
                        advPushStatus(version);
潘栩锋's avatar
潘栩锋 committed
1107 1108
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1109 1110 1111
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_GRID:
                    {
                        int version;
1112
                        var pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushGrid_2();
1113
                        if (!pack.TryParse_version(infodata, out version))
潘栩锋's avatar
潘栩锋 committed
1114
                            break;
1115
                        grid_version = version;
潘栩锋's avatar
潘栩锋 committed
1116 1117
                        DateTime dt;
                        int marker;
1118
                        if (version == 1)
潘栩锋's avatar
潘栩锋 committed
1119 1120 1121 1122
                        {
                            dt = DateTime.Now;
                            marker = Marker;
                        }
1123
                        else //if (version == 2)
潘栩锋's avatar
潘栩锋 committed
1124
                        {
1125
                            dt = sysTickContext.ToDateTime(pack.systick);
1126 1127
                            marker = pack.marker;
                        }
潘栩锋's avatar
潘栩锋 committed
1128

1129
                        advPushGrid(dt, marker, pack.direction, pack.grid_start, pack.data);
潘栩锋's avatar
潘栩锋 committed
1130

1131 1132 1133 1134 1135 1136 1137 1138
                        GridEvent?.Invoke(this, new MiniGridEventArgs()
                        {
                            direction = pack.direction,
                            grid_start = pack.grid_start,
                            posOfGrid = PosOfGrid,
                            buf = pack.data,
                            marker = marker
                        });
潘栩锋's avatar
潘栩锋 committed
1139

潘栩锋's avatar
潘栩锋 committed
1140 1141
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1142 1143 1144 1145 1146 1147
                case FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_MINIGRID:
                    {
                        FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushGrid pack = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushGrid();
                        if (!pack.TryParse(infodata))
                            break;

1148
                        advPushMiniGrid(pack.direction, pack.grid_start, pack.data);
潘栩锋's avatar
潘栩锋 committed
1149

1150
                        MiniGridEvent?.Invoke(this, new MiniGridEventArgs()
潘栩锋's avatar
潘栩锋 committed
1151
                        {
1152 1153 1154 1155 1156 1157
                            direction = pack.direction,
                            grid_start = pack.grid_start,
                            posOfGrid = PosOfGrid,
                            buf = pack.data,
                            marker = Marker
                        });
潘栩锋's avatar
潘栩锋 committed
1158

潘栩锋's avatar
潘栩锋 committed
1159 1160
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1161
            }
1162
            _end_push_info:
潘栩锋's avatar
潘栩锋 committed
1163 1164 1165 1166 1167
            bShieldSetValueEx = false;
        }

        #endregion

1168
        void Drive(DRIVE_MAN_ORDER order, object param)
潘栩锋's avatar
潘栩锋 committed
1169 1170
        {

1171
            switch (order)
潘栩锋's avatar
潘栩锋 committed
1172
            {
1173 1174 1175 1176 1177
                case DRIVE_MAN_ORDER.RUNTO:
                    {
                        int TargetPos = (int)param;
                        if (Position == TargetPos)
                            break;//已经到了位置!!!!!, FLYAD7 是没有动作,没有推送的!!!
潘栩锋's avatar
潘栩锋 committed
1178

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
                        DriveOrder = DRIVE_MAN_ORDER.RUNTO;
                        DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                        CurrObjSys.CallFunctionEx(
                            conn,
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                            ID,
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                            new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallRunto() { to = TargetPos }.ToBytes());
                        
                    }
                    break;
                case DRIVE_MAN_ORDER.ORIGIN:
                case DRIVE_MAN_ORDER.BACKWORD:
                case DRIVE_MAN_ORDER.FORWORD:
                case DRIVE_MAN_ORDER.STOP:
潘栩锋's avatar
潘栩锋 committed
1194
                    {
1195 1196 1197
                        DriveOrder = order;
                        DriveStatus = DRIVE_MAN_STATUS.RUNNING;
                        CurrObjSys.CallFunctionEx(
潘栩锋's avatar
潘栩锋 committed
1198 1199 1200
                            conn,
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                            ID,
1201 1202
                            FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                            new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_CallPXX() { order = order }.ToBytes());
潘栩锋's avatar
潘栩锋 committed
1203
                    }
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
                    break;
                case DRIVE_MAN_ORDER.SYNC:
                    {
                        if (param != null)
                        {
                            CurrObjSys.CallFunctionEx(
                                conn,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                                ID,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncBeginWithPos2()
                                {
                                    pos2 = (int)param
                                }.ToBytes());
                        }
                        else
                        {

                            CurrObjSys.CallFunctionEx(
                                conn,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                                ID,
                                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncBegin().ToBytes());
                        }
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
1231 1232
            }

1233 1234
            //3秒后,再次发送获取状态,怕AD盒没有推送
            driveman_wait.Mark();
潘栩锋's avatar
潘栩锋 committed
1235
        }
1236
        void Drive(DRIVE_MAN_ORDER order)
潘栩锋's avatar
潘栩锋 committed
1237
        {
1238
            Drive(order, null);
潘栩锋's avatar
潘栩锋 committed
1239 1240 1241 1242
        }



1243 1244 1245 1246 1247
        #region Load Save
        public bool Load()
        {
            return Load(defaultPath);
        }
潘栩锋's avatar
潘栩锋 committed
1248

1249 1250 1251
        public bool Load(string jsonDbPath)
        {
            this.jsonDbPath = jsonDbPath;
1252

1253
            this.PropertyChanged -= FlyAD7_PropertyChanged2;
1254

1255 1256 1257 1258
            //不需要设置到 AD盒
            bShieldSetValueEx = true;
            bool ret = FlyAD7JsonDb.Load(jsonDbPath, this);
            bShieldSetValueEx = false;
潘栩锋's avatar
潘栩锋 committed
1259

1260 1261 1262 1263
            
            this.PropertyChanged += FlyAD7_PropertyChanged2;
            
            return ret;
1264

潘栩锋's avatar
潘栩锋 committed
1265 1266
        }

1267
        /// <summary>
1268
        /// 属性改变,自动保存
1269
        /// </summary>
1270 1271 1272
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FlyAD7_PropertyChanged2(object sender, PropertyChangedEventArgs e)
1273
        {
1274
            if (propertyName_save.Contains(e.PropertyName))
潘栩锋's avatar
潘栩锋 committed
1275
            {
1276 1277 1278 1279
                PollModule.Current.Poll_JustOnce(() =>
                {
                    Save();
                }, this, MARKNO_SET_SAVE);
1280 1281
            }
        }
潘栩锋's avatar
潘栩锋 committed
1282

1283 1284 1285 1286 1287
        public void Save()
        {
            FlyAD7JsonDb.Save(jsonDbPath, this);
        }
        #endregion
潘栩锋's avatar
潘栩锋 committed
1288

1289
        #region IFlyADClient function
1290
        /// <summary>
1291
        /// 连接
1292
        /// </summary>
1293 1294 1295 1296
        public void Connect()
        {
            Connect(Addr);
        }
1297 1298

        /// <summary>
1299
        /// 连接 ep
1300
        /// </summary>
1301 1302 1303 1304
        /// <param name="addr"></param>
        public void Connect(string addr)
        {
            Addr = addr;
1305

1306 1307 1308 1309 1310 1311 1312 1313
            if (conn != null)
            {
                if (!conn.RemoteEP.Equals(LocalEP))
                {
                    //断开之前的连接
                    CurrObjSys.Disconnect_to_Another_OBJSys(conn.RemoteEP, ID);
                }
            }
1314

1315 1316 1317
            conn = CurrObjSys.Connect_to_Another_OBJSys(LocalEP, ID);
            conn.HasCRC = HasCRC;
        }
1318

1319
        /// <summary>
1320
        /// 重连
1321
        /// </summary>
1322 1323 1324 1325 1326 1327 1328
        public void ReConnect()
        {
            if (conn != null)
            {
                //断开之前的连接
                CurrObjSys.Disconnect_to_Another_OBJSys(conn.RemoteEP, ID);
            }
1329

1330 1331 1332
            conn = CurrObjSys.Connect_to_Another_OBJSys(LocalEP, ID);
            conn.HasCRC = HasCRC;
        }
潘栩锋's avatar
潘栩锋 committed
1333

1334 1335
        #endregion
        #region  IFlyAD function
1336 1337 1338 1339 1340 1341
        /// <summary>
        /// Out0 = 0, SetOutput(0x0001,0x0000);
        /// Out0 = 1, Out1 = 0, SetOutput(0x0003,0x0001);
        /// </summary>
        /// <param name="mask"></param>
        /// <param name="enable"></param>
潘栩锋's avatar
潘栩锋 committed
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
        public void SetOutput(ushort mask, ushort enable)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.CALL_OUTPORT,
                new FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.Pack_CallOutPort()
                {
                    mask = mask,
                    port = enable
                }.ToBytes());
1354 1355 1356 1357 1358 1359 1360 1361

            //回读
            //FLYIO
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.FLYIO_OBJ_INTERFACE.GET_IO);
潘栩锋's avatar
潘栩锋 committed
1362 1363 1364
        }

        /// <summary>
1365
        /// 设置 运动参数
潘栩锋's avatar
潘栩锋 committed
1366
        /// </summary>
1367 1368 1369 1370
        /// <param name="posOfGrid">脉冲/grid</param>
        /// <param name="motortype">电机类型</param>
        /// <param name="ratio01">脉冲比例 分母(电机脉冲)</param>
        /// <param name="ratio02">脉冲比例 分子(编码器脉冲)</param>
潘栩锋's avatar
潘栩锋 committed
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
        public void SetSysParam(UInt16 posOfGrid, MOTORTYPE motortype, UInt16 ratio01, UInt16 ratio02)
        {
            byte func_enable = 0x0e;
            if (motortype == MOTORTYPE.NULL)
                func_enable = 0x0e;
            else if (motortype == MOTORTYPE.SERVO)
                func_enable = 0x0e;
            else// if (motortype == MOTORTYPE.VF0)
                func_enable = 0x0d;

            CurrObjSys.SetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.SET_SYSPARAM,
                new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_SysParam()
                {
                    posOfGrid = posOfGrid,
                    ad1_interval = 1000,
                    ad2_interval = 1000,
                    push_interval = 100,
                    func_setting = 0,
                    func_enable = func_enable,
                    ratio01 = ratio01,
                    ratio02 = ratio02
                }.ToBytes());
            //CurrObjSys.GetValueEx(
            //    conn,
            //    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
            //    ID,
            //    0x1234,
            //    FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSPARAM);
        }
1404

1405 1406 1407 1408 1409 1410 1411 1412 1413
        /// <summary>
        /// 设置速度参数
        /// </summary>
        /// <param name="velocity">runto 速度 pps</param>
        /// <param name="sv">开始速度 pps</param>
        /// <param name="atime">加速时间 ms</param>
        /// <param name="dtime">减速时间 ms</param>
        /// <param name="hspeed1">归0 第1段速 pps</param>
        /// <param name="hspeed2">归0 第2段速 pps</par
潘栩锋's avatar
潘栩锋 committed
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
        public void SetPosParam(UInt32 velocity, UInt32 sv, UInt32 atime, UInt32 dtime, UInt32 hspeed1, UInt32 hspeed2)
        {
            bShieldSetValueEx = true;
            if (velocity != 0xffffffff)
                Velocity = velocity;
            if (sv != 0xffffffff)
                SVelocity = sv;
            if (atime != 0xffffffff)
                ATime = atime;
            if (dtime != 0xffffffff)
                DTime = dtime;
            if (hspeed1 != 0xffffffff)
                HVelocity1 = hspeed1;
            if (hspeed2 != 0xffffffff)
                HVelocity2 = hspeed2;
            bShieldSetValueEx = false;
            CurrObjSys.SetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.SET_DRIVEPARAM,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveParam()
                {
                    Velocity = velocity,
                    SV = sv,
                    ATime = atime,
                    DTime = dtime,
                    hspeed1 = hspeed1,
                    hspeed2 = hspeed2
                }.ToBytes());
            //CurrObjSys.GetValueEx(
            //    conn,
            //    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
            //    ID,
            //    0x1234,
            //    FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.GET_DRIVEPARAM);
        }
1451

1452 1453 1454 1455 1456 1457
        /// <summary>
        /// 设置速度
        /// </summary>
        /// <param name="velocity"></param>
        public void SetVelocity(UInt32 velocity)
        {
1458
            SetPosParam(velocity,UInt32.MaxValue, UInt32.MaxValue, UInt32.MaxValue, UInt32.MaxValue, UInt32.MaxValue);
1459
        }
潘栩锋's avatar
潘栩锋 committed
1460 1461 1462 1463

        /// <summary>
        /// 输入系统授权码
        /// </summary>
1464
        /// <param name="access">系统授权码access[8]</param>
潘栩锋's avatar
潘栩锋 committed
1465 1466 1467 1468 1469 1470 1471 1472
        public void SetAccess(byte[] access)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.CALL_ACCESS,
                access);
1473 1474 1475 1476 1477 1478 1479

            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSINFO);

潘栩锋's avatar
潘栩锋 committed
1480
        }
1481
        
潘栩锋's avatar
潘栩锋 committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
        /// <summary>
        /// 初始化系统信息区
        /// </summary>
        public void InitArea()
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.CALL_INIT,
                new FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.Pack_Init().ToBytes());
1493

1494 1495 1496 1497 1498 1499
            CurrObjSys.GetValueEx(
                conn,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.SYS_DATA_INTERFACE.GET_SYSINFO);
        }
1500

1501 1502 1503 1504 1505
        #region 运行动作
        /// <summary>
        /// 运行至指定位置
        /// </summary>
        /// <param name="to">脉冲</param>
潘栩锋's avatar
潘栩锋 committed
1506 1507 1508 1509
        public void Runto(int to)
        {
            Drive(DRIVE_MAN_ORDER.RUNTO, to);
        }
1510 1511 1512
        /// <summary>
        /// 归原点
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1513 1514 1515 1516
        public void Origin()
        {
            Drive(DRIVE_MAN_ORDER.ORIGIN);
        }
1517 1518 1519
        /// <summary>
        /// 停止
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1520 1521 1522 1523
        public void Stop()
        {
            Drive(DRIVE_MAN_ORDER.STOP);
        }
1524 1525 1526
        /// <summary>
        /// 反向,直到撞了限位
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1527 1528 1529 1530
        public void Backward()
        {
            Drive(DRIVE_MAN_ORDER.BACKWORD);
        }
1531 1532 1533
        /// <summary>
        /// 正行,直到撞了限位
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
1534 1535 1536 1537 1538
        public void Forward()
        {
            Drive(DRIVE_MAN_ORDER.FORWORD);
        }

1539 1540

        #endregion
潘栩锋's avatar
潘栩锋 committed
1541 1542

        #region 同步
1543 1544 1545



潘栩锋's avatar
潘栩锋 committed
1546 1547

        #region 同步控制 同步状态转换规则
潘栩锋's avatar
潘栩锋 committed
1548

潘栩锋's avatar
潘栩锋 committed
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
        /// <summary>
        /// 进入同步状态
        /// </summary>
        public void SyncBegin()
        {
            Drive(DRIVE_MAN_ORDER.SYNC, null);
            IsSync = true;
        }
        /// <summary>
        /// 退出同步状态
        /// </summary>
        public void SyncEnd()
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncEnd().ToBytes());
            IsSync = false;
        }
        /// <summary>
        /// 清空同步指令
        /// </summary>
        public void SyncClear()
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncClear().ToBytes());
1581

潘栩锋's avatar
潘栩锋 committed
1582 1583 1584
            SyncOrders.Clear();

        }
潘栩锋's avatar
潘栩锋 committed
1585

潘栩锋's avatar
潘栩锋 committed
1586 1587 1588 1589
        #endregion
        #region 同步扫描 脚本指令

        /// <summary>
1590 1591
        /// 同步扫描至;
        /// D+0xE0+开始主轴位置+结束主轴位置+结束横向脉冲位置(逻辑位置)+脉冲开关(1B)+命令识标号(4B)
潘栩锋's avatar
潘栩锋 committed
1592
        /// </summary>
1593 1594 1595 1596
        /// <param name="pos2_begin"></param>
        /// <param name="pos2_end"></param>
        /// <param name="pos1lc"></param>
        /// <param name="hasDataGrid"></param>
潘栩锋's avatar
潘栩锋 committed
1597 1598
        /// <param name="marker"></param>
        public void SyncRunAtLC(int pos2lc_begin, int pos2lc_end, int pos1lc, bool hasDataGrid, Int32 marker)
潘栩锋's avatar
潘栩锋 committed
1599 1600 1601 1602 1603 1604 1605 1606
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunAtLC()
                {
潘栩锋's avatar
潘栩锋 committed
1607 1608
                    pos2_begin = pos2lc_begin,
                    pos2_end = pos2lc_end,
潘栩锋's avatar
潘栩锋 committed
1609 1610 1611 1612 1613 1614 1615
                    pos1lc = pos1lc,
                    hasDataGrid = hasDataGrid,
                    marker = marker
                }.ToBytes());

            SyncOrders.Add(new SyncOrder_SyncRunAtLC()
            {
潘栩锋's avatar
潘栩锋 committed
1616 1617
                Pos2Begin = pos2lc_begin,
                Pos2End = pos2lc_end,
潘栩锋's avatar
潘栩锋 committed
1618 1619 1620 1621 1622 1623
                Pos1LC = pos1lc,
                HasDataGrid = hasDataGrid,
                Marker = marker
            });
        }

1624 1625 1626 1627 1628
        /// <summary>
        /// 位于队列头时运行,归零;
        /// D+0xE1+命令识标号(4B)
        /// </summary>
        /// <param name="marker"></param>
潘栩锋's avatar
潘栩锋 committed
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
        public void SyncOrigin(Int32 marker)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncOrigin()
                {
                    marker = marker
                }.ToBytes());

            SyncOrders.Add(new SyncOrder_SyncOrigin()
            {
                Marker = marker
            });
        }


1648 1649 1650 1651 1652 1653 1654 1655
        /// <summary>
        /// 位于队列头时运行,以速度运行至物理位置;
        /// D+0xE2+横向脉冲位置(4B:int32,物理位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
        /// </summary>
        /// <param name="pos1"></param>
        /// <param name="velocity"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
潘栩锋's avatar
潘栩锋 committed
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
        public void SyncRunTo(int pos1, UInt32 velocity, bool hasDataGrid, Int32 marker)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRunto()
                {
                    pos1 = pos1,
                    velocity = velocity,
                    hasDataGrid = hasDataGrid,
                    marker = marker
                }.ToBytes());

            SyncOrders.Add(new SyncOrder_SyncRunTo()
            {
                Pos1 = pos1,
                Velocity = velocity,
                HasDataGrid = hasDataGrid,
                Marker = marker
            });
        }

1680
        /// <summary>
1681 1682 1683 1684 1685 1686 1687
        /// 位于队列头时运行,以速度运行至逻辑位置;
        /// D+0xE3+横向脉冲位置(4B:int32,逻辑位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
        /// </summary>
        /// <param name="pos1lc"></param>
        /// <param name="velocity"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
潘栩锋's avatar
潘栩锋 committed
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
        public void SyncRunToLC(int pos1lc, UInt32 velocity, bool hasDataGrid, Int32 marker)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncRuntoLC()
                {
                    pos1 = pos1lc,
                    velocity = velocity,
                    hasDataGrid = hasDataGrid,
                    marker = marker
                }.ToBytes());
            SyncOrders.Add(new SyncOrder_SyncRunToLC()
            {
                Pos1 = pos1lc,
                Velocity = velocity,
                HasDataGrid = hasDataGrid,
                Marker = marker
            });
        }


1712 1713 1714 1715 1716 1717
        /// <summary>
        /// 等待,ms;
        /// D+0xE4+毫秒数(4B:int32)+命令识标号(4B)
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="marker"></param>
潘栩锋's avatar
潘栩锋 committed
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
        public void SyncWait(int ms, Int32 marker)
        {
            CurrObjSys.CallFunctionEx(
                conn,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.ID,
                ID,
                FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.CALL_PXX,
                new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_SyncWait()
                {
                    ms = ms,
                    marker = marker
                }.ToBytes());
            SyncOrders.Add(new SyncOrder_SyncWait()
            {
                MS = ms,
                Marker = marker
            });
        }
        #endregion
        #endregion
1738
        #endregion
1739 1740
    }

1741
    class FlyAD7JsonDb
1742
    {
1743 1744 1745 1746
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
        {
            c.CreateMap<FlyAD7, FlyAD7JsonDb>().ReverseMap();
        }));
潘栩锋's avatar
潘栩锋 committed
1747 1748 1749
        public static void Save(string jsonDbPath, FlyAD7 src)
        {

1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
            if (string.IsNullOrEmpty(jsonDbPath))
                return;
            var p = FlyAD7JsonDb.Mapper.Map<FlyAD7JsonDb>(src);
            try
            {
                File.WriteAllText(jsonDbPath, JsonConvert.SerializeObject(p, Formatting.Indented));
            }
            catch
            {
                //异常,没有json 编码失败
            }
        }
        public static bool Load(string jsonDbPath, FlyAD7 src)
        {
            if (string.IsNullOrEmpty(jsonDbPath))
                return false;
1766

1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
            try
            {
                if (File.Exists(jsonDbPath))
                {
                    string json = File.ReadAllText(jsonDbPath);
                    var p = JsonConvert.DeserializeObject<FlyAD7JsonDb>(json);
                    Mapper.Map(p, src);
                    return true;
                }
            }
            catch
            {
                //异常,没有json 解码失败
            }
            return false;
        }
        public string Addr = "192.168.251.10:20006";
1784 1785
        public bool HasCRC = false;
        public int GridSmooth = 0;
1786
        public int ADLag = 0;
1787 1788 1789 1790 1791
        public int PosMin = 0;
        public int PosMax = 8900;
        public bool HasPosMaxMin = false;

        //备份参数
1792
        public bool IsDevParamValid = false;
1793 1794 1795 1796 1797 1798
        public MOTORTYPE MotorType  = MOTORTYPE.SERVO;
        public UInt16 PosOfGrid = 10;
        public UInt16 Ratio01 = 4;
        public UInt16 Ratio02 = 1;
        public Int16 PosOffset = 0;
        public UInt32 JogVelocity  = 1000;
1799

1800 1801 1802 1803 1804 1805 1806 1807 1808
        /// <summary>
        /// 硬件版本
        /// version = 1,不支持 systick
        /// version = 2,支持 timeGridAdv
        /// version = 3,伟的大AD盒, io口定义不一样
        /// </summary>
        public int HardwareVersion = 2;


1809 1810 1811 1812 1813
        public static string[] GetMemberNames() {
            var  type = typeof(FlyAD7JsonDb);
            return type.GetMembers().Select(mi => mi.Name).ToArray();
        }

1814
    }
潘栩锋's avatar
潘栩锋 committed
1815

1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
    /// <summary>
    ///  systick 转 DateTime
    ///  不断把 从flyad7 获取的 systick 放入。  以每次systick 的差 加上 上一次 dt_1st, 得到时间。
    ///  如: 当前 获得 systick1, 之前是 systick0,   所以 dt = dt_1st+ (systick1-systick0);  dt_1st=dt;
    ///  
    /// 第1个 dt_1st, 是 从 DateTime.Now. 获取的。
    /// 
    /// 不会管 当前DateTime.Now 与 dt 差多少。 所以如果 flyad7 时间比较慢,可能会出现 3天后, DateTime.Now 与 dt 差了 1个小时。
    /// </summary>
    class SysTickContext
    {
        Int32 systick_1st = 0;
        DateTime dt_1st = DateTime.MinValue;
        public event Action BeReseted;
        public void Reset()
        {
            dt_1st = DateTime.MinValue;
        }
        /// <summary>
        /// 输入从 flyad7 获取的 timemark, 转换为 本地 DateTime
        /// </summary>
        /// <param name="systick"></param>
        /// <returns></returns>
        public DateTime ToDateTime(int systick)
        {
            if (dt_1st == DateTime.MinValue)
            {
                dt_1st = DateTime.Now;
                systick_1st = systick;
                return dt_1st;
            }

            int ms = systick - systick_1st;
            if ((ms < -1000) ||//时间被提前
                (ms > 1000 * 60 * 3))//记录与新的timemark 相差3分钟,太大了,重新生成 timemark 与 dt 的关系
            {
                //被复位了
                BeReseted?.Invoke();
                dt_1st = DateTime.Now;
                systick_1st = systick;
                return dt_1st;
            }

            DateTime dt = dt_1st.AddMilliseconds(ms);

            systick_1st = systick;
            dt_1st = dt;
            //if (Math.Abs((DateTime.Now-dt).TotalSeconds) > 5000)//滞后5s,异常
            //{
            //    //修正
            //    dt = DateTime.Now;
            //    dt_1st = dt;
            //}
            return dt;
        }
    }
    class DtAndBool
    {
        bool bNeedGet;
        DateTime dt;
        /// <summary>
        /// 标记 【需要获取数据】,且登记【时间】
        /// </summary>
        public void Mark()
        {
            bNeedGet = true;
            dt = DateTime.Now;
        }

        /// <summary>
        /// 检测是否 【需要获取数据】,再把 now-【时间】,时间差大于delay 返回true;
        /// 当返回true, 【需要获取数据】被清除
        /// </summary>
        /// <param name="now"></param>
        /// <param name="delay"></param>
        /// <returns></returns>
        public bool CheckMark(TimeSpan delay)
        {
            if (!bNeedGet)
                return false;

            if ((DateTime.Now - dt) < delay)
                return false;


            bNeedGet = false;
            return true;
        }
    }
    class IsReadyContext : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public bool isReadyGetState { get; set; }
        public bool isReadyGetPos1AD1 { get; set; }
        public bool isReadyGetPos2 { get; set; }
        public bool isReadyGetIO { get; set; }
潘栩锋's avatar
潘栩锋 committed
1913

1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
        public bool IsReady => isReadyGetState && isReadyGetPos2 && isReadyGetPos1AD1 && isReadyGetIO;

        public void Reset()
        {
            isReadyGetState = false;
            isReadyGetPos1AD1 = false;
            isReadyGetPos2 = false;
            isReadyGetIO = false;
        }
    }
潘栩锋's avatar
潘栩锋 committed
1924
}