FLYAD7_OBJ_INTERFACE.cs 60.8 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using System.Net;
using FlyADBase;

9 10 11
/// <summary>
/// AD盒 通信的 OBJ 包定义
/// </summary>
潘栩锋's avatar
潘栩锋 committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
namespace FLYAD7_OBJ_INTERFACE
{
    public class FLYADC_OBJ_INTERFACE
    {
        #region OBJ_ID
        public const UInt32 ID = 0x10020001;
        #endregion

        #region GET
        public const UInt16 GET_POS1AD1 = 0x101;
        public class Pack_GetPos1AD1 : IPack
        {
            public int pos;
            public int ad;
            public int speed;



            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(pos));
                buf.AddRange(BitConverter.GetBytes(ad));
                buf.AddRange(BitConverter.GetBytes(speed));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 12)
                    return false;
                pos = BitConverter.ToInt32(value, 0);
                ad = BitConverter.ToInt32(value, 4);
                speed = BitConverter.ToInt32(value, 8);
                return true;
            }

            #endregion
        }
        public const UInt16 GET_POS2 = 0x102;//long POS2
        //long segment
        //long distance
        //long speed
        public class Pack_GetPos2 : PUSH_DATA_INTERFACE.Pack_PushPos2
        {

        }
        public const UInt16 GET_AD2 = 0x103;//long AD2
        public class Pack_GetAD2 : PUSH_DATA_INTERFACE.Pack_PushAD1
        {

        }
        #endregion

        #region CALL
        public const UInt16 CALL_RESETPOS = 0x101;//ResetPos()
        #endregion

    }
    public class FLYIO_OBJ_INTERFACE
    {
        #region OBJ_ID
        public const UInt32 ID = 0x10030002;
        #endregion

        #region GetValue
        public const UInt16 GET_IO = 0x101;//unsigned char inport
        //unsigned char outport
        public class Pack_GetIO : PUSH_DATA_INTERFACE.Pack_PushIO
        {

        }
        #endregion

        #region CallFunction
        public const UInt16 CALL_OUTPORT = 0x101;//OutPort(uint8_t mask,uint8_t port) 
        public class Pack_CallOutPort : IPack
        {
            public UInt16 mask;
            public UInt16 port;
            #region IPack 成员
            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(mask));
                buf.AddRange(BitConverter.GetBytes(port));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 4)
                    return false;
                mask = BitConverter.ToUInt16(value, 0);
                port = BitConverter.ToUInt16(value, 2);
                return true;
            }

            #endregion
        }
        #endregion
    }
    public class DRIVE_MAN_INTERFACE
    {
        #region OBJ_ID
        public const UInt32 ID = 0x10060003;
        #endregion

        #region Pack

        public class Pack_DriveParam : IPack
        {
            public UInt32 Velocity;
            public UInt32 SV;
            public UInt32 ATime;			// in ms
            public UInt32 DTime;			// in ms
            public UInt32 hspeed1;			// Home Speed1
            public UInt32 hspeed2;			// Home Speed2


            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes(Velocity));
                buf.AddRange(BitConverter.GetBytes(SV));
                buf.AddRange(BitConverter.GetBytes(ATime));
                buf.AddRange(BitConverter.GetBytes(DTime));
                buf.AddRange(BitConverter.GetBytes(hspeed1));
                buf.AddRange(BitConverter.GetBytes(hspeed2));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Count() < 24)
                    return false;
                Velocity = BitConverter.ToUInt32(value, 0);
                SV = BitConverter.ToUInt32(value, 4);
                ATime = BitConverter.ToUInt32(value, 8);
                DTime = BitConverter.ToUInt32(value, 12);
                hspeed1 = BitConverter.ToUInt32(value, 16);
                hspeed2 = BitConverter.ToUInt32(value, 20);
                return true;
            }

            #endregion
        }
        public class Pack_DriveState : IPack
        {
            //当前运行指令(1B)+当前运行状态(1B)+运行模式(1B)+  systick(4B) + 命令序列码(4B)
            public DRIVE_MAN_ORDER order;
            public DRIVE_MAN_STATUS status;
            public byte mode;
            public int systick;
            public Int32 marker;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.Add((byte)order);
                buf.Add((byte)status);
                buf.Add((byte)mode);
                buf.AddRange(BitConverter.GetBytes(systick));
                buf.AddRange(BitConverter.GetBytes(marker));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Count() < (1 + 1 + 1 + 4 + 4))
                    return false;
                int idx = 0;
                order = (DRIVE_MAN_ORDER)value[idx];
                idx++;
                status = (DRIVE_MAN_STATUS)value[idx];
                idx++;
                mode = value[idx];
                idx++;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;
                marker = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }

            #endregion
        }
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
        public class Pack_SyncStatus : IPack
        {
            public SYNC_STATUS status;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.Add((byte)status);
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Count() < (1))
                    return false;
                int idx = 0;
  
                status = (SYNC_STATUS)value[idx];
                idx++;
                return true;
            }

            #endregion
        }

        public class Pack_SyncOrderCnt : IPack
        {
            public UInt16 cnt;
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes(cnt));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Count() < (2))
                    return false;
                int idx = 0;

                cnt = BitConverter.ToUInt16(value, idx);
                idx += 2;
                return true;
            }

            #endregion
        }
潘栩锋's avatar
潘栩锋 committed
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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 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 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
        public class Pack_MotorType : IPack
        {
            public MOTORTYPE motortype;

            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.Add((byte)motortype);
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Count() < 1)
                    return false;
                motortype = (MOTORTYPE)value[0];
                return true;
            }

            #endregion
        }

        public abstract class Pack_OrderBase : IPack
        {
            public DRIVE_MAN_ORDER order;
            #region IPack 成员

            public virtual byte[] ToBytes()
            {
                byte[] buf = new byte[1];
                buf[0] = (byte)order;
                return buf;
            }

            public virtual bool TryParse(byte[] value)
            {
                if (value.Count() < 1)
                    return false;
                order = (DRIVE_MAN_ORDER)value[0];
                return true;
            }

            #endregion
            public static bool TryParse(byte[] value, out Pack_OrderBase outpack)
            {
                outpack = null;
                bool ret;
                if (value.Count() < 1)
                    return false;

                Pack_OrderBase pack;
                DRIVE_MAN_ORDER order = (DRIVE_MAN_ORDER)value[0];
                switch (order)
                {
                    case DRIVE_MAN_ORDER.ORIGIN:
                    case DRIVE_MAN_ORDER.FORWORD:
                    case DRIVE_MAN_ORDER.BACKWORD:
                    case DRIVE_MAN_ORDER.STOP:
                        pack = new Pack_CallPXX();
                        break;
                    case DRIVE_MAN_ORDER.RUNTO:
                        pack = new Pack_CallRunto();
                        break;
                    case DRIVE_MAN_ORDER.SYNC:
                        return Pack_SyncBase.TryParse(value, out outpack);
                        break;
                    default:
                        return false;
                }
                ret = pack.TryParse(value);
                outpack = pack;
                return ret;
            }
        }
        public class Pack_CallPXX : Pack_OrderBase
        {

        }
        public class Pack_CallRunto : Pack_OrderBase
        {
            public int to;
            public Pack_CallRunto()
            {
                order = DRIVE_MAN_ORDER.RUNTO;
            }
            #region IPack 成员

            public override byte[] ToBytes()
            {

                byte[] buf = new byte[6];
                buf[0] = (byte)DRIVE_MAN_ORDER.RUNTO;
                BitConverter.GetBytes(to).CopyTo(buf, 1);
                buf[5] = 1;
                return buf;
            }

            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 5)
                    return false;
                if (value[0] != (byte)DRIVE_MAN_ORDER.RUNTO)
                    return false;

                order = DRIVE_MAN_ORDER.RUNTO;
                to = BitConverter.ToInt32(value, 1);
                return true;
            }

            #endregion
        }
        #region 同步参数设置
        public abstract class Pack_SyncBase : Pack_OrderBase
        {
            public byte syncnnum;
            static Dictionary<byte, Type> packtype;
            static Pack_SyncBase()
            {
                packtype = new Dictionary<byte, Type>();
                packtype.Add(Pack_Pos1LCShift.GetNum(), typeof(Pack_Pos1LCShift));
                packtype.Add(Pack_Pos2Shift.GetNum(), typeof(Pack_Pos2Shift));
                packtype.Add(Pack_Pos2.GetNum(), typeof(Pack_Pos2));
                packtype.Add(Pack_Pos2Comp.GetNum(), typeof(Pack_Pos2Comp));
                packtype.Add(Pack_Pos2_01.GetNum(), typeof(Pack_Pos2_01));

                packtype.Add(Pack_SyncBeginWithPos2.GetNum(), typeof(Pack_SyncBeginWithPos2));
                packtype.Add(Pack_SyncBegin.GetNum(), typeof(Pack_SyncBegin));
                packtype.Add(Pack_SyncEnd.GetNum(), typeof(Pack_SyncEnd));
                packtype.Add(Pack_SyncPos2Clear.GetNum(), typeof(Pack_SyncPos2Clear));


                packtype.Add(Pack_SyncRunAtLC.GetNum(), typeof(Pack_SyncRunAtLC));
                packtype.Add(Pack_SyncOrigin.GetNum(), typeof(Pack_SyncOrigin));
                packtype.Add(Pack_SyncRunto.GetNum(), typeof(Pack_SyncRunto));
                packtype.Add(Pack_SyncRuntoLC.GetNum(), typeof(Pack_SyncRuntoLC));
                packtype.Add(Pack_SyncWait.GetNum(), typeof(Pack_SyncWait));
            }
            public Pack_SyncBase()
            {
                order = DRIVE_MAN_ORDER.SYNC;
            }
            public override byte[] ToBytes()
            {
                byte[] buf = new byte[2];
                buf[0] = (byte)order;
                buf[1] = syncnnum;
                return buf;
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2)
                    return false;
                order = (DRIVE_MAN_ORDER)value[0];
                syncnnum = value[1];
                return true;
            }
            public static bool TryParse(byte[] value, out Pack_OrderBase outpack)
            {
                outpack = null;
                if (value.Count() < 2)
                    return false;
                char order = (char)value[0];
                byte syncnnum = value[1];

                if (!packtype.ContainsKey(syncnnum))
                    return false;
                outpack = Activator.CreateInstance(packtype[syncnnum]) as Pack_OrderBase;
                return outpack.TryParse(value);
            }
        }
        /// <summary>
        /// 
        /// D+0x80+m_pos1_LCShift(4B:int32)
        /// </summary>
        public class Pack_Pos1LCShift : Pack_SyncBase
        {
            public int pos1_LCShift;
            public static byte GetNum()
            {
                return 0x80;
            }
            public Pack_Pos1LCShift()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(pos1_LCShift));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 4)
                    return false;
                int idx = 0;
                idx += 2;
                pos1_LCShift = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }
        /// <summary>
        /// 设定纵向偏移
        /// D+0x81+m_pos2_shift(4B:int32)
        /// </summary>
        public class Pack_Pos2Shift : Pack_SyncBase
        {
            public int pos2_shift;
            public static byte GetNum()
            {
                return 0x81;
            }
            public Pack_Pos2Shift()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(pos2_shift));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 4)
                    return false;
                int idx = 0;
                idx += 2;
                pos2_shift = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }
        /// <summary>
        /// 设定纵向值
        /// D+0x82+m_pos2(4B:int32)
        /// </summary>
        public class Pack_Pos2 : Pack_SyncBase
        {
            public int pos2;
            public static byte GetNum()
            {
                return 0x82;
            }
            public Pack_Pos2()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(pos2));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 4)
                    return false;
                int idx = 0;
                idx += 2;
                pos2 = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }
        /// <summary>
        /// 设定纵向值补偿系数,补偿时,先乘除后加减
        /// D+0x83+m_pos2_comp(4B:float)
        /// </summary>
        public class Pack_Pos2Comp : Pack_SyncBase
        {
            public float comp;
            public static byte GetNum()
            {
                return 0x83;
            }
            public Pack_Pos2Comp()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(comp));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 4)
                    return false;
                int idx = 0;
                idx += 2;
                comp = BitConverter.ToSingle(value, idx);
                idx += 4;
                return true;
            }
        }

        /// <summary>
        /// 纵向同步事件,0-1事件
        /// D+0x84+主轴脉冲(4B:int32)+ 'I' 或 'N'
        /// </summary>
        public class Pack_Pos2_01 : Pack_SyncBase
        {
            public int pos2;
            public bool immediately;
            public static byte GetNum()
            {
                return 0x84;
            }
            public Pack_Pos2_01()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(pos2));
                if (immediately)
                {
                    buf.Add((byte)'I');
                }
                else 
                {
                    buf.Add((byte)'N');
                }

                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < (2+4+1))
                    return false;
                int idx = 0;
                idx += 2;
                pos2 = BitConverter.ToInt32(value, idx);
                idx += 4;
                char c = (char)value[idx];
                if (c == 'I')
                    immediately = true;
                else
                    immediately = false;
                idx += 1;
                return true;
            }
        }

        #endregion
        #region  同步控制
        /// <summary>
        /// 进入同步状态
        /// D+0x01+m_pos2(4B:int32)
        /// </summary>
        public class Pack_SyncBeginWithPos2 : Pack_SyncBase
        {
            public int pos2;
            public static byte GetNum()
            {
                return 0x01;
            }
            public Pack_SyncBeginWithPos2()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(pos2));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 4)
                    return false;
                int idx = 0;
                idx += 2;
                pos2 = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }
        /// <summary>
        /// 进入同步状态
        /// D+0x02(4B:int32)
        /// </summary>
        public class Pack_SyncBegin : Pack_SyncBase
        {
            public static byte GetNum()
            {
                return 0x02;
            }
            public Pack_SyncBegin()
            {
                syncnnum = GetNum();
            }
        }
        /// <summary>
        /// 退出同步状态
        /// D+0x03
        /// </summary>
        public class Pack_SyncEnd : Pack_SyncBase
        {
            public static byte GetNum()
            {
                return 0x03;
            }
            public Pack_SyncEnd()
            {
                syncnnum = GetNum();
            }
        }
        /// <summary>
        /// 清空同步列表
        /// </summary>
        public class Pack_SyncClear : Pack_SyncBase
        {
            public static byte GetNum()
            {
                return 0x04;
            }
            public Pack_SyncClear()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2)
                    return false;
                int idx = 0;
                idx += 2;
                return true;
            }
        }

        /// <summary>
        /// 清空主轴脉冲同步列表
        /// </summary>
        public class Pack_SyncPos2Clear : Pack_SyncBase
        {
            public static byte GetNum()
            {
                return 0x05;
            }
            public Pack_SyncPos2Clear()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2)
                    return false;
                int idx = 0;
                idx += 2;
                return true;
            }
        }
        #endregion
        #region 同步扫描

        /// <summary>
        ///         7.1	同步扫描至:
        ///         D+0xE0+开始主轴位置+结束主轴位置+结束横向脉冲位置(逻辑位置)+脉冲开关(1B)+命令识标号(4B)
        /// </summary>
        public class Pack_SyncRunAtLC : Pack_SyncBase
        {
            public int pos2_begin;
            public int pos2_end;
            public int pos1lc;
            public bool hasDataGrid;
            public Int32 marker;

            public static byte GetNum()
            {
                return 0xE0;
            }
            public Pack_SyncRunAtLC()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(pos2_begin));
                buf.AddRange(BitConverter.GetBytes(pos2_end));
                buf.AddRange(BitConverter.GetBytes(pos1lc));
                buf.AddRange(BitConverter.GetBytes(hasDataGrid));
                buf.AddRange(BitConverter.GetBytes(marker));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 17)
                    return false;
                int idx = 0;
                idx += 2;
                pos2_begin = BitConverter.ToInt32(value, idx);
                idx += 4;
                pos2_end = BitConverter.ToInt32(value, idx);
                idx += 4;
                pos1lc = BitConverter.ToInt32(value, idx);
                idx += 4;
                hasDataGrid = BitConverter.ToBoolean(value, idx);
                idx++;
                marker = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }

        /// <summary>
        /// 7.2	位于队列头时运行,归零
        /// D+0xE1+命令识标号(4B)
        /// </summary>
        public class Pack_SyncOrigin : Pack_SyncBase
        {
            public Int32 marker;
            public static byte GetNum()
            {
                return 0xE1;
            }
            public Pack_SyncOrigin()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(marker));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 4)
                    return false;
                int idx = 0;
                idx += 2;
                marker = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }

        /// <summary>
        /// 7.3	位于队列头时运行,以速度运行至物理位置
        /// D+0xE2+横向脉冲位置(4B:int32,物理位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
        /// </summary>
        public class Pack_SyncRunto : Pack_SyncBase
        {
            public int pos1;
            public UInt32 velocity;
            public bool hasDataGrid;
            public Int32 marker;
            public static byte GetNum()
            {
                return 0xE2;
            }
            public Pack_SyncRunto()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(pos1));
                buf.AddRange(BitConverter.GetBytes(velocity));
                buf.AddRange(BitConverter.GetBytes(hasDataGrid));
                buf.AddRange(BitConverter.GetBytes(marker));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 13)
                    return false;
                int idx = 0;
                idx += 2;
                pos1 = BitConverter.ToInt32(value, idx);
                idx += 4;
                velocity = BitConverter.ToUInt32(value, idx);
                idx += 4;
                hasDataGrid = BitConverter.ToBoolean(value, idx);
                idx++;
                marker = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }

        /// <summary>
        ///         7.3	位于队列头时运行,以速度运行至逻辑位置
        ///         D+0xE3+横向脉冲位置(4B:int32,逻辑位置)+速度(4B:int32)+脉冲开关(1B)+命令识标号(4B)
        /// </summary>
        public class Pack_SyncRuntoLC : Pack_SyncBase
        {
            public int pos1;
            public UInt32 velocity;
            public bool hasDataGrid;
            public Int32 marker;
            public static byte GetNum()
            {
                return 0xE3;
            }
            public Pack_SyncRuntoLC()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(pos1));
                buf.AddRange(BitConverter.GetBytes(velocity));
                buf.AddRange(BitConverter.GetBytes(hasDataGrid));
                buf.AddRange(BitConverter.GetBytes(marker));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 13)
                    return false;
                int idx = 0;
                idx += 2;
                pos1 = BitConverter.ToInt32(value, idx);
                idx += 4;
                velocity = BitConverter.ToUInt32(value, idx);
                idx += 4;
                hasDataGrid = BitConverter.ToBoolean(value, idx);
                idx++;
                marker = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }


        /// <summary>
        /// 7.4	等待,ms
        /// D+0xE4+毫秒数(4B:int32)+命令识标号(4B)
        /// </summary>
        public class Pack_SyncWait : Pack_SyncBase
        {
            public int ms;
            public Int32 marker;
            public static byte GetNum()
            {
                return 0xE4;
            }
            public Pack_SyncWait()
            {
                syncnnum = GetNum();
            }
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add(syncnnum);
                buf.AddRange(BitConverter.GetBytes(ms));
                buf.AddRange(BitConverter.GetBytes(marker));
                return buf.ToArray();
            }
            public override bool TryParse(byte[] value)
            {
                if (value.Count() < 2 + 8)
                    return false;
                int idx = 0;
                idx += 2;
                ms = BitConverter.ToInt32(value, idx);
                idx += 4;
                marker = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }

        #endregion
        #endregion

        #region GetValue
        /// <summary>
        /// 读最近运行状态及结果和运行模式,当前运行指令(1B)+当前运行状态(1B)+运行模式(1B)
        /// Pack_DriveState
        /// </summary>
        public const UInt16 GET_STATE = 0x101;


        /// <summary>
        /// 读脉冲输出参数;
        /// 脉冲速率(4B)+初始速率(4B)+加速时间(4B)+减速时间(4B)+回零速度1(4B)+回零速度2(4B);
        /// Pack_DriveParam
        /// </summary>
        public const UInt16 GET_DRIVEPARAM = 0x102;
986 987 988 989 990 991 992 993 994 995 996

        /// <summary>
        /// 读同步状态 (1Byte)
        /// </summary>
        public const UInt16 GET_SYNC_STATUS = 0x103;

        /// <summary>
        /// 读同步指令数 (2Byte)
        /// </summary>
        public const UInt16 GET_SYNC_ORDER_CNT = 0x104;

潘栩锋's avatar
潘栩锋 committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 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 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 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 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 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 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 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 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
        #endregion

        #region SetValue
        /// <summary>
        /// 写脉冲输出参数。脉冲速率(4B)+初始速率(4B)+加速时间(4B)+减速时间(4B)+回零速度1(4B)+回零速度2(4B);
        /// Pack_DriveParam
        /// </summary>
        public const UInt16 SET_DRIVEPARAM = 0x102;//写脉冲输出参数。

        #endregion

        #region CallFunction
        /// <summary>
        /// Pack_CallPXX,Pack_CallRunto
        /// </summary>
        public const UInt16 CALL_PXX = 0x101;//调用运行函数,发各种运行指令。

        #endregion


    }
    public class SYS_DATA_INTERFACE
    {
        #region OBJ_ID
        public const UInt32 ID = 0x10070004;
        #endregion

        #region Pack
        public class Pack_IP : IPack
        {
            public IPEndPoint ep;
            #region IPack 成员

            public byte[] ToBytes()
            {
                byte[] _addr = ep.Address.GetAddressBytes();
                byte[] ipaddr = new byte[4];
                for (int i = 0; i < ipaddr.Count() && i < _addr.Count(); i++)
                {
                    ipaddr[i] = _addr[i];
                }
                UInt16 port = (UInt16)ep.Port;

                List<byte> buf = new List<byte>();
                buf.AddRange(ipaddr);
                buf.AddRange(BitConverter.GetBytes(port));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Count() < 6)
                    return false;
                UInt32 ipaddr = BitConverter.ToUInt32(value, 0);
                UInt16 port = BitConverter.ToUInt16(value, 4);
                ep = new IPEndPoint(ipaddr, port);
                return true;
            }

            #endregion
        }
        public class Pack_SysParam : IPack
        {
            public UInt16 posOfGrid;//1个Grid=?脉冲数(2B)
            public UInt16 ad1_interval;//定点AD1采样频率(2B)
            public UInt16 ad2_interval;//定点AD2采样频率(2B)
            public UInt16 push_interval;//信息包推送间隔(2B)
            public byte func_setting;//开关量功能设置(1B)
            public byte func_enable;//功能使能(1B)
            public UInt16 ratio01;//电机比例(2B)
            public UInt16 ratio02;//脉冲比例(2B)
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(posOfGrid));
                buf.AddRange(BitConverter.GetBytes(ad1_interval));
                buf.AddRange(BitConverter.GetBytes(ad2_interval));
                buf.AddRange(BitConverter.GetBytes(push_interval));
                buf.Add(func_setting);
                buf.Add(func_enable);
                buf.AddRange(BitConverter.GetBytes(ratio01));
                buf.AddRange(BitConverter.GetBytes(ratio02));

                return buf.ToArray();

            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 14)
                    return false;
                int idx = 0;
                posOfGrid = BitConverter.ToUInt16(value, idx);
                idx += 2;
                ad1_interval = BitConverter.ToUInt16(value, idx);
                idx += 2;
                ad2_interval = BitConverter.ToUInt16(value, idx);
                idx += 2;
                push_interval = BitConverter.ToUInt16(value, idx);
                idx += 2;
                func_setting = value[idx];
                idx += 1;
                func_enable = value[idx];
                idx += 1;
                ratio01 = BitConverter.ToUInt16(value, idx);
                idx += 2;
                ratio02 = BitConverter.ToUInt16(value, idx);
                idx += 2;
                return true;
            }

            #endregion
        }

        /// <summary>
        /// char status;
        /// char ret;
        /// unsigned char code[6];
        /// unsigned short surplus;
        /// unsigned char access[8];
        /// </summary>
        public class Pack_SysInfo : IPack
        {


            public AREA_STATUS status;
            public AREA_ERR ret;
            public byte[] code = new byte[6];
            public UInt16 surplus;
            public byte[] access = new byte[8];


            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)status);
                buf.Add((byte)ret);
                buf.AddRange(code);
                buf.AddRange(BitConverter.GetBytes(surplus));
                buf.AddRange(access);
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Count() < 18)
                    return false;
                int idx = 0;
                status = (AREA_STATUS)value[idx];
                idx++;
                ret = (AREA_ERR)value[idx];
                idx++;

                Array.Copy(value, idx, code, 0, 6);
                idx += 6;
                surplus = BitConverter.ToUInt16(value, idx);
                idx += 2;
                Array.Copy(value, idx, access, 0, 8);
                idx += 8;
                return true;
            }

            #endregion
        }
        public class Pack_ZeroPos : IPack
        {
            public Int16 zero_pos;//零位脉冲
            public UInt32 jog_velocity;//点动速度
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(zero_pos));
                buf.AddRange(BitConverter.GetBytes(jog_velocity));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Count() < 6)
                    return false;

                zero_pos = BitConverter.ToInt16(value, 0);
                jog_velocity = BitConverter.ToUInt32(value, 2);
                return true;
            }

            #endregion
        }
        public class Pack_Init : IPack
        {
            public byte area = 1;
            #region IPack 成员
            public byte[] ToBytes()
            {
                string password = "flymeasure@@";

                List<byte> buf = new List<byte>();
                for (int i = 0; i < password.Length; i++)
                {
                    buf.Add((byte)password[i]);
                }
                buf.Add(area);
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                return true;
            }

            #endregion
        }
        #endregion

        #region GetValue
        /// <summary>
        /// Pack_IP;
        /// unsigned char IPaddr[4]
        /// unsigned short port
        /// </summary>
        public const UInt16 GET_IP = 0x101;

        /// <summary>
        /// 运行参数读。1个Grid=?脉冲数(2B)+定点AD1采样频率(2B)+定点AD2采样频率(2B)+信息包推送间隔(2B)+开关量功能设置(1B)+功能使能(1B)+电机一圈脉冲(2B)+编码器一圈脉冲(2B)
        /// Pack_SysParam
        /// </summary>
        public const UInt16 GET_SYSPARAM = 0x102;

        /// <summary>
        /// 返回系统授权信息,状态码(1B)+返回码(1B)+序列码(6B)+系统剩余时间(2B)+授权码(8B)
        /// Pack_SysInfo
        /// </summary>
        public const UInt16 GET_SYSINFO = 0x103;

        /// <summary>
        /// Pack_ZeroPos
        /// </summary>
        public const UInt16 GET_ZERO_POS = 0x104;

        #endregion

        #region SetValue
        /// <summary>
        /// Pack_IP, 这个不支持!!!!!
        /// </summary>
        public const UInt16 SET_IP = 0x101;
        /// <summary>
        /// 运行参数写。value=1个Grid=?脉冲数(2B)+定点AD1采样频率(2B)+定点AD2采样频率(2B)+信息包推送间隔(2B)+开关量功能设置(1B)+功能使能(1B)+电机一圈脉冲(2B)+编码器一圈脉冲(2B)
        /// Pack_SysParam
        /// </summary>
        public const UInt16 SET_SYSPARAM = 0x102;

        /// <summary>
        /// Pack_ZeroPos
        /// </summary>
        public const UInt16 SET_ZERO_POS = 0x103;
        #endregion

        #region CallFunction
        public const UInt16 CALL_INIT = 0x101;//初始化系统信息区,InitArea(uint_t *pass,uint8_t area)pass是初始化需要的密码,area=01授权区,02参数区
        //密码(12byte)+初始化区域(1byte)

        public const UInt16 CALL_ACCESS = 0x102;//输入系统授权码,CheckAccessCode(uint8_t *acc)
        //授权码(8byte)


        public const UInt16 CALL_SURPLUS_SUB = 0x104;//没参数 系统使用时间加-1	
        #endregion


    }
    public class PUSH_DATA_INTERFACE
    {
        #region OBJ_ID
        public const UInt32 ID = 0x10080005;
        #endregion

        #region Pack
        public class Pack_PushPos1 
        {
            public int pos1;
            public int speed1;

            #region IPack 成员
            /// <summary>
            /// 8 bytes
            /// </summary>
            /// <returns></returns>
            public virtual byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(pos1));
                buf.AddRange(BitConverter.GetBytes(speed1));
                return buf.ToArray();
            }
            /// <summary>
            /// 8 bytes
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public virtual bool TryParse(byte[] value)
            {
                if (value.Length < 8)
                    return false;
                pos1 = BitConverter.ToInt32(value, 0);
                speed1 = BitConverter.ToInt32(value, 4);
                return true;
            }

            #endregion
        }

        public class Pack_PushAD1 : IPack
        {
            public int AD;

            #region IPack 成员
            public virtual void CopyTo(Pack_PushAD1 t)
            {
                t.AD = AD;
            }
            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(AD));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 4)
                    return false;
                AD = BitConverter.ToInt32(value, 0);
                return true;
            }

            #endregion
        }

        public class Pack_PushIO : IPack
        {
            public UInt16 istatus;
            public UInt16 ostatus;

            #region IPack 成员
            /// <summary>
            /// 4 bytes
            /// </summary>
            /// <returns></returns>
            public virtual byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes(istatus));
                buf.AddRange(BitConverter.GetBytes(ostatus));
                return buf.ToArray();
            }
            /// <summary>
            /// 4 bytes
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public virtual bool TryParse(byte[] value)
            {
                if (value.Length < 4)
                    return false;
                int idx = 0;
                istatus = BitConverter.ToUInt16(value, idx);
                idx += 2;
                ostatus = BitConverter.ToUInt16(value, idx);
                idx += 2;

                return true;
            }

            #endregion
        }

        public class Pack_PushPos2 : IPack
        {
            public int pos2;
            public int segment;
            public int distance;
            public int speed2;
            #region IPack 成员
            /// <summary>
            /// 16 bytes
            /// </summary>
            /// <returns></returns>
            public virtual byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(pos2));
                buf.AddRange(BitConverter.GetBytes(segment));
                buf.AddRange(BitConverter.GetBytes(distance));
                buf.AddRange(BitConverter.GetBytes(speed2));
                return buf.ToArray();
            }
            /// <summary>
            /// 16 bytes
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public virtual bool TryParse(byte[] value)
            {
                if (value.Length < 16)
                    return false;
                pos2 = BitConverter.ToInt32(value, 0);
                segment = BitConverter.ToInt32(value, 4);
                distance = BitConverter.ToInt32(value, 8);
                speed2 = BitConverter.ToInt32(value, 12);
                return true;
            }

            #endregion
        }
        public class Pack_PushTGrid
        {
            public int[] data;


            /// <summary>
            /// 2+4*n
            /// </summary>
            /// <returns></returns>
            public virtual byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes((UInt16)data.Length));
                for (int i = 0; i < data.Length; i++)
                {
                    buf.AddRange(BitConverter.GetBytes(data[i]));
                }

                return buf.ToArray();
            }
            /// <summary>
            /// 2+4*n
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public virtual bool TryParse(byte[] value)
            {
                int cnt = 2;
                int idx = 0;
                if (value.Length < cnt)
                    return false;
                idx = 0;
                int len = BitConverter.ToUInt16(value, idx);
                idx += 2;
                cnt += len * 4;
                if (value.Length < cnt)
                    return false;

                data = new int[len];

                for (int i = 0; i < len; i++)
                {
                    data[i] = BitConverter.ToInt32(value, idx);
                    idx += 4;
                }

                return true;
            }

            /// <summary>
            /// 2+4*n
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public virtual bool TryParse(byte[] value,out int cnt)
            {
                cnt = 2;
                int idx = 0;
                if (value.Length < cnt)
                    return false;
                idx = 0;
                int len = BitConverter.ToUInt16(value, idx);
                idx += 2;
                cnt += len * 4;
                if (value.Length < cnt)
                    return false;

                data = new int[len];

                for (int i = 0; i < len; i++)
                {
                    data[i] = BitConverter.ToInt32(value, idx);
                    idx += 4;
                }

                return true;
            }          
        }

        public class Pack_PushStatus
        {
            public DRIVE_MAN_ORDER order;
            public DRIVE_MAN_STATUS status;

            #region IPack 成员
            /// <summary>
            /// 2 byte
            /// </summary>
            /// <returns></returns>
            public virtual byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.Add((byte)order);
                buf.Add((byte)status);
                
                return buf.ToArray();
            }
            /// <summary>
            ///  2 byte
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public virtual bool TryParse(byte[] value)
            {
                if (value.Length < 2)
                    return false;
                order = (DRIVE_MAN_ORDER)value[0];
                status = (DRIVE_MAN_STATUS)value[1];

                return true;
            }

            #endregion
        }
        public class Pack_PushGrid 
        {
            public Misc.DIRECTION direction;

            public int grid_start;

            public int[] data;
            #region IPack 成员

            public virtual byte[] ToBytes()
            {
                int grid_end = data.Length + grid_start - 1;
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes((UInt16)grid_start));

                buf.AddRange(BitConverter.GetBytes((UInt16)grid_end));

                if (direction == Misc.DIRECTION.FORWARD)
                    buf.Add(0);
                else
                    buf.Add(1);

                for (int i = 0; i < data.Length; i++)
                {
                    buf.AddRange(BitConverter.GetBytes(data[i]));
                }
                return buf.ToArray();
            }

            public virtual bool TryParse(byte[] value)
            {
                int cnt;
                return TryParse(value, out cnt);
            }

            public virtual bool TryParse(byte[] value, out int cnt)
            {
                cnt = 5;
                
                if (value.Length < cnt)
                    return false;
                int idx = 0;
                grid_start = BitConverter.ToUInt16(value, idx);
                idx += 2;
                int grid_end = BitConverter.ToUInt16(value, idx);
                idx += 2;
                if (value[idx] == 0)
                    direction = Misc.DIRECTION.FORWARD;
                else
                    direction = Misc.DIRECTION.BACKWARD;
                idx += 1;

                int len;
                len = grid_end - grid_start + 1;

                if (len < 0)
                    return false;
                cnt += len * 4;
                if (value.Length < cnt)
                    return false;

                data = new int[len];

                for (int i = 0; i < len; i++)
                {
                    data[i] = BitConverter.ToInt32(value, idx);
                    idx += 4;
                }
                return true;
            }

            #endregion
        }


        #region systick 的版本
        public class Pack_PushPos1_2 : Pack_PushPos1
        {
            public Int32 systick;
            #region IPack 成员

            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(base.ToBytes());
                buf.AddRange(BitConverter.GetBytes(systick));
                return buf.ToArray();
            }

            public override bool TryParse(byte[] value)
            {
                if (!base.TryParse(value))
                    return false;
                int idx = 8;
                int cnt = 8 + 4;
                if (value.Length < cnt)
                    return false;

                systick = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }

            public bool TryParse_version(byte[] value, out int version)
            {
                version = 1;
                 if (!base.TryParse(value))
                    return false;
                int idx = 8;
                int cnt = 8 + 4;
                if (value.Length < cnt)
                    return true;
                version = 2;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;               
            }
            #endregion
        }

        public class Pack_PushIO_2 : Pack_PushIO
        {
            public UInt16 inChange;
            public Int32 pos1;
            public Int32 pos2;
            public Int32 systick;

            #region IPack 成员
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(base.ToBytes());
                buf.AddRange(BitConverter.GetBytes(inChange));
                buf.AddRange(BitConverter.GetBytes(pos1));
                buf.AddRange(BitConverter.GetBytes(pos2));
                buf.AddRange(BitConverter.GetBytes(systick));
                return buf.ToArray();
            }

            public override bool TryParse(byte[] value)
            {
                if (!base.TryParse(value))
                    return false;
                int idx = 4;
                int cnt = 4 + 2 + 4 * 3;

                if (value.Length < cnt)
                    return false;

                inChange = BitConverter.ToUInt16(value, idx);
                idx += 2;
                pos1 = BitConverter.ToInt32(value, idx);
                idx += 4;
                pos2 = BitConverter.ToInt32(value, idx);
                idx += 4;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
            public bool TryParse_version(byte[] value, out int version) 
            {
                version = 1;
                if (!base.TryParse(value))
                    return false;
                int idx = 4;
                int cnt = 4 + 2 + 4 * 3;

                if (value.Length < cnt)
                    return true;
                version = 2;

                inChange = BitConverter.ToUInt16(value, idx);
                idx += 2;
                pos1 = BitConverter.ToInt32(value, idx);
                idx += 4;
                pos2 = BitConverter.ToInt32(value, idx);
                idx += 4;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;               
            }
            #endregion
        }
        public class Pack_PushPos2_2 : Pack_PushPos2
        {
            public Int32 systick;
            /// <summary>
            /// 20 byte
            /// </summary>
            /// <returns></returns>
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(base.ToBytes());
                buf.AddRange(BitConverter.GetBytes(systick));
                return buf.ToArray();
            }
            /// <summary>
            /// 20 byte
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public override bool TryParse(byte[] value)
            {
                if (!base.TryParse(value))
                    return false;
                int idx = 16;
                int cnt = 16 + 4;
                if (value.Length < cnt)
                    return false;

                systick = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
            public bool TryParse_version(byte[] value, out int version)
            {
                version = 1;
                if (!base.TryParse(value))
                    return false;
                int idx = 16;
                int cnt = 16 + 4;
                if (value.Length < cnt)
                    return true;
                version = 2;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }

        public class Pack_PushTGrid_2 : Pack_PushTGrid
        {
            public Int32 systick = 0;
            #region IPack 成员

            /// <summary>
            /// 2+4*n+4
            /// </summary>
            /// <returns></returns>
            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(base.ToBytes());
                buf.AddRange(BitConverter.GetBytes(systick));

                return buf.ToArray();
            }
            /// <summary>
            /// 2+4*n+4
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public override bool TryParse(byte[] value)
            {

                int cnt;
                return TryParse(value, out cnt);
            }

            /// <summary>
            /// 2+4*n+4
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public override bool TryParse(byte[] value, out int cnt)
            {
                if (!base.TryParse(value,out cnt))
                    return false;

                int idx = cnt;

                cnt += 4;

                if (value.Length < cnt)
                    return false;

                systick = BitConverter.ToInt32(value, idx);
                idx += 4;

                return true;
            }
            public virtual bool TryParse_version(byte[] value, out int version)
            {
                version = 1;

                int cnt;

                if (!base.TryParse(value, out cnt))
                    return false;

                int idx = cnt;

                cnt += 4;

                if (value.Length < cnt)
                    return true;
                version = 2;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;

                return true;
            }
            #endregion
        }

        public class Pack_PushStatus_2 : Pack_PushStatus
        {
            //当前运行指令(1B)+当前运行状态(1B)+运行模式(1B)+  systick(4B) + 命令序列码(4B)

            public byte mode;
            public int systick;
            public Int32 marker;

            public override byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();

                buf.AddRange(base.ToBytes());
                buf.Add((byte)mode);
                buf.AddRange(BitConverter.GetBytes(systick));
                buf.AddRange(BitConverter.GetBytes(marker));
                return buf.ToArray();
            }

            public override bool TryParse(byte[] value)
            {
                if (!base.TryParse(value))
                    return false;
                int idx = 2;
                int cnt = 2 + 1 + 4 + 4;
                if (value.Length < cnt)
                    return false;
   
                mode = value[idx];
                idx++;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;
                marker = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }

            public virtual bool TryParse_version(byte[] value, out int version)
            {
                version = 1;

                if (!base.TryParse(value))
                    return false;
                int idx = 2;
                int cnt = 2 + 1 + 4 + 4;
                if (value.Length < cnt)
                    return true;

                version = 2;
                mode = value[idx];
                idx++;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;
                marker = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }
        }

        public class Pack_PushGrid_2 : Pack_PushGrid
        {
            public Int32 marker;

            public int systick;

            #region IPack 成员

            public byte[] ToBytes()
            {
               
                List<byte> buf = new List<byte>();

                buf.AddRange(base.ToBytes());

                buf.AddRange(BitConverter.GetBytes(marker));

                buf.AddRange(BitConverter.GetBytes(systick));

                return buf.ToArray();
            }
            public override bool TryParse(byte[] value) 
            {
                int cnt;
                return TryParse(value, out cnt);

            }
            public override bool TryParse(byte[] value, out int cnt)
            {
                if (!base.TryParse(value,out cnt))
                    return false;
                int idx = cnt;
                cnt += (4+4);

                if (value.Length < cnt)
                    return false;

                marker = BitConverter.ToInt32(value, idx);
                idx+=4;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;

                return true;
            }

            public virtual bool TryParse_version(byte[] value, out int version) 
            {
                version = 1;

                int cnt;

                if (!base.TryParse(value, out cnt))
                    return false;
                int idx = cnt;
                cnt += (4 + 4);

                if (value.Length < cnt)
                    return true;
                
                version = 2;

                marker = BitConverter.ToInt32(value, idx);
                idx += 4;
                systick = BitConverter.ToInt32(value, idx);
                idx += 4;

                return true;               
            }

            #endregion
        }
        #endregion

        #endregion

        #region PUSH
        // Add Sense Config: 000+00+sense_mask(4B)
        //sense_mask
        /// <summary>
        /// Pack_PushPos1
        /// </summary>
        public const UInt16 PUSH_POS1 = 0;//pos1(4B)+speed1(4B)

        /// <summary>
        /// Pack_PushAD1
        /// </summary>
        public const UInt16 PUSH_AD1 = 1;//AD1(4B)
        /// <summary>
        /// Pack_PushIO
        /// </summary>
        public const UInt16 PUSH_IO = 2;//Inport(2B)+outport(2B)

        /// <summary>
        /// Pack_PushPos2
        /// </summary>
        public const UInt16 PUSH_POS2 = 3;//pos2(4B)+段长1(4B)+段长2(4B)+speed2(4B)

        /// <summary>
        /// Pack_PushAD1
        /// </summary>
        public const UInt16 PUSH_AD2 = 4;//AD2(4B)

        /// <summary>
        /// Timegrid数据量N(2byte)+数据(N*4bytes)
        /// Pack_PushTGrid
        /// </summary>
        public const UInt16 PUSH_TIMEGRID = 5;

        public const UInt16 PUSH_GRID = 6;//开始grid(2byte)+结束grid(2byte)+grid方向(1byte)+gridAD*N(N*4byte)


        /// <summary>
        /// Pack_PushStatus
        /// </summary>
        public const UInt16 PUSH_STATUS = 7;//当前运行指令(1B)+当前运行状态(1B)
        /// <summary>
        /// Pack_PushGrid
        /// </summary>
        public const UInt16 PUSH_MINIGRID = 8;

        #endregion
    }
}