KTKSMCManager.cs 52.2 KB
1 2 3 4 5 6 7 8 9 10 11 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 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 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 986 987 988 989 990 991 992 993 994 995 996 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
using log4net;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Timers;

namespace OnlineStore.DeviceLibrary
{
    /// <summary>
    /// 康泰克运动版操作类
    /// </summary>
    public class KTKSMCManager
    {
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public static string KTK_SMC_DeviceName = ConfigAppSettings.GetValue(Setting_Init.KTK_SMC_BoardName);
        private static int defaultRet = 0;
        private static Dictionary<string, KTKDeviceInfo> SMCDeviceMap = new Dictionary<string, KTKDeviceInfo>();
        private static CSmc Smc = new CSmc();

        //默认的运动参数
        public static double Move_ResolveSpeed = (double)ConfigAppSettings.GetNumValue(Setting_Init.KTK_MOVE_ResolveSpeed);
        public static double Move_StartSpeed = (double)ConfigAppSettings.GetNumValue(Setting_Init.KTK_MOVE_StartSpeed);
        public static double Move_TargetSpeed = (double)ConfigAppSettings.GetNumValue(Setting_Init.KTK_MOVE_TargetSpeed);
        public static short Move_AccelTime = (short)ConfigAppSettings.GetNumValue(Setting_Init.KTK_MOVE_AccelTime);
        public static short Move_lDecelTime = (short)ConfigAppSettings.GetNumValue(Setting_Init.KTK_MOVE_lDecelTime);
        public static double Move_SSpeed = (double)ConfigAppSettings.GetNumValue(Setting_Init.KTK_MOVE_SSpeed);
        public static int Move_Distance = ConfigAppSettings.GetIntValue(Setting_Init.KTK_MOVE_Distance);

        private static Timer timersTimer = null;

        public static void InitTimer()
        {
        }

        /// <summary>
        /// io卡初始化
        /// </summary> 
        public static string WInit(string DeviceName)
        {
            if (timersTimer == null)
            {
                InitTimer();
            }
            short DeviceId = 0;
            string msg = "";
            if (!SMCDeviceMap.ContainsKey(DeviceName))
            {
                //初始化 
                defaultRet = Smc.WInit(DeviceName, out DeviceId);
                msg = WGetErrorString(defaultRet);
                if (defaultRet != 0)
                {
                    Smc.WExit(DeviceId);
                    return msg;
                }
                KTKDeviceInfo ktk = new KTKDeviceInfo(DeviceId, DeviceName, 0, 0);
                SMCDeviceMap.Add(DeviceName, ktk); 
            }
            return msg;
        }
        
        /// <summary>
        /// 初始化轴
        /// </summary> 
        public static string InitAxis(string DeviceName,short AxisNo)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                short PulseType = 5;
                short DirTimer = 1;
                short EncType = 2;
                short CtrlOut1 = 0;
                short CtrlOut2 = 0;
                short CtrlOut3 = 0;
                short CtrlIn = 1;
                short CtrlInOutLog = 1;
                int lDistance = Move_Distance;
                short SAccelType = 1;
                short LimitTurn = 1;
                short OrgType = 0;
                short EndDir = 2;//原点返回方式,0=disable,1=Plus Dir ,2=Minus Dir
                //如果是康泰克单台料仓,上下轴和进出轴原点返回方式用0,旋转轴还使用原来的方式
                if (SAStoreManager.store != null && (!AxisNo.Equals(1)))
                {
                    LogUtil.info(DeviceName + "轴" + AxisNo + "康泰克单台料仓,原点返回方式使用0");
                    EndDir = 0;
                }
                short ZCount = 1;

                //PulseType:CW/CCW HIGH
                //Reverse Direction Timer ON
                defaultRet = Smc.WSetPulseType(DeviceId, AxisNo, PulseType, DirTimer);
                //EncType: A/B two-phase input 4 times
                defaultRet = Smc.WSetEncType(DeviceId, AxisNo, EncType);
                //out1,out3是普通IO,out1表示伺服ON,out2=普通IO(表示alm清理,需要手动写入)
                defaultRet = Smc.WSetCtrlTypeOut(DeviceId, AxisNo, CtrlOut1, CtrlOut2, CtrlOut3);
                //IN 0=ALM
                defaultRet = Smc.WSetCtrlTypeIn(DeviceId, AxisNo, CtrlIn);
                //IN logic in1=高位有效,报警高位有效
                defaultRet = Smc.WSetCtrlInOutLog(DeviceId, AxisNo, CtrlInOutLog);
                //S Accel/Decel Use=on
                defaultRet = Smc.WSetSAccelType(DeviceId, AxisNo, SAccelType);
                //原点返回方式
                defaultRet = Smc.WSetOrgMode(DeviceId, AxisNo, LimitTurn, OrgType, EndDir, ZCount);

                short InitParam;
                defaultRet = Smc.WGetInitParam(DeviceId, AxisNo, out InitParam);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                //if (InitParam == 0)
                {
                    // SmcWSetInitParam
                    defaultRet = Smc.WSetInitParam(DeviceId, AxisNo);
                    if (defaultRet != 0)
                    {
                        return WGetErrorString(defaultRet);
                    }
                }
                //OpenOrCloseAxis(DeviceName, AxisNo, 1);
                short bCoordinate = (short)CSmcConst.CSMC_INC;

                defaultRet = Smc.WSetStopPosition(DeviceId, AxisNo, bCoordinate, lDistance);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }

                //默认关闭OUT2
                short OutData = 0;
                defaultRet = Smc.WGetDigitalOut(DeviceId, AxisNo, out OutData);
                if ((OutData & (short)CSmcConst.CSMC_OUT2) == (int)CSmcConst.CSMC_OUT2)
                {
                    defaultRet = Smc.WSetDigitalOut(DeviceId, AxisNo, 0, (short)CSmcConst.CSMC_OUT2);
                    if (defaultRet != 0)
                    {
                        return WGetErrorString(defaultRet);
                    }
                } 
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
            return "";
        }
        /// <summary>
        ///打开关闭轴
        /// </summary> 
        public static string OpenOrCloseAxis(string DeviceName, short AxisNo, short value)
        {
            if (AxisNo < 0)
            {
                return "";
            }
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WSetDigitalOut(DeviceId, AxisNo, value, (short)CSmcConst.CSMC_OUT1);
                return WGetErrorString(defaultRet);
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
        }
        /// <summary>
        ///打开关闭轴
        /// </summary> 
        public static string OpenOrCloseAxis(ConfigMoveAxis moveAxis, short value)
        {
            if (moveAxis.GetAxisValue() < 0)
            {
                return "";
            }
            return OpenOrCloseAxis(moveAxis.DeviceName, (short)moveAxis.GetAxisValue(), value);
        }
        /// <summary>
        /// 判断设备是否初始化过了
        /// </summary>
        public static Boolean WDeviceIsInit(string deviceName)
        {
            if (SMCDeviceMap.ContainsKey(deviceName))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 退出运动卡
        /// </summary> 
        public static string WExit(string DeviceName)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;

                //退出
                defaultRet = Smc.WExit(DeviceId);
                SMCDeviceMap.Remove(DeviceName);

                return WGetErrorString(defaultRet);
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
        }
        private static string getNotInitStr(string DeviceName)
        {
            return "设备【" + DeviceName + "】没有初始化";
        }

        public static string ClearAlarm(string DeviceName, short AxisNo)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WSetDigitalOut(DeviceId, AxisNo, (short)CSmcConst.CSMC_OUT2, (short)CSmcConst.CSMC_OUT2);
                //LOGGER.Info(" 写入清理报警IO=1,等待2秒后重新回写");
                System.Timers.Timer mytimer = new System.Timers.Timer(1000);
                mytimer.Elapsed += (o1, e1) =>
                {
                    try
                    {
                        defaultRet = Smc.WSetDigitalOut(DeviceId, AxisNo, 0, (short)CSmcConst.CSMC_OUT2);
                        //LOGGER.Info("定时回写入清理报警IO完成");
                    }
                    catch (Exception ex)
                    {
                        LOGGER.Error("定时回写入 出错:", ex);
                    }
                };
                mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次;
                mytimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件mytask;
                return WGetErrorString(defaultRet);
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
        }

        /// <summary>
        /// 获取报警码
        /// </summary> 
        /// <returns>报警码</returns>
        public static short WGetAlarmCode(string DeviceName, short AxisNo)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short id = SMCDeviceMap[DeviceName].DeviceId;
                short AlarmCode = 0;
                Smc.WGetAlarmCode(id, AxisNo, out AlarmCode);
                return AlarmCode;
            }
            return 0;
        }
        /// <summary>
        /// 根据 错误码返回错误字符串
        /// </summary>
        /// <param name="ErrorCode">错误码</param>
        /// <returns>错误信息描述字符串</returns>
        public static string WGetErrorString(int ErrorCode)
        {
            string ErrorString = "";
            if (ErrorCode != 0)
            {
                int ret = Smc.WGetErrorString(ErrorCode, out  ErrorString);
                if (!ErrorString.Equals(""))
                {
                    LogUtil.error(LOGGER, "康泰克运动卡错误:code=" + ErrorCode + ",msg=" + ErrorString);
                }
            }
            return ErrorString;
        }

        /// <summary>
        /// 获取OutPulse的值
        /// </summary> 
        /// <returns>OutPulse</returns>
        public static int WGetOutPulse(string DeviceName, short AxisNo)
        {
            int lOutPulse = 0;
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WGetOutPulse(DeviceId, AxisNo, out lOutPulse);
            }
            return lOutPulse;
        }
        /// <summary>
        /// 获取lCountPulse的值
        /// </summary> 
        /// <returns>CountPulse</returns>
        public static int WGetCountPulse(string DeviceName, short AxisNo)
        {
            int lCountPulse = 0;
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WGetCountPulse(DeviceId, AxisNo, out lCountPulse);
            }
            return lCountPulse;
        }
        /// <summary>
        /// 获取报警状态
        /// </summary> 
        public static IO_VALUE GetAlarmIO(string DeviceName, short AxisNo)
        {   // Get Limit Status from Driver      
            short LimitSts = 0;
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WGetLimitStatus(DeviceId, AxisNo, out LimitSts);
            }
            return ((LimitSts & (short)CSmcConst.CSMC_ALM) == (int)CSmcConst.CSMC_ALM) ? IO_VALUE.HIGH : IO_VALUE.LOW;
        }
        /// <summary>
        /// 获取设备的基本状态
        /// </summary> 
        public static KTKDeviceStatus getDeviceStatus(string DeviceName, short AxisNo)
        {
            KTKDeviceStatus status = new KTKDeviceStatus();
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                int lOutPulse, lCountPulse;
                short PulseSts, MoveSts, StopSts, LimitSts;

                // Get OutPulse from Driver
                //defaultRet = Smc.WGetOutPulse(DeviceId, AxisNo, out lOutPulse);              
                lOutPulse = WGetOutPulse(DeviceName, AxisNo);
                status.OutPulse = lOutPulse;
                status.SetResult(defaultRet);
                // Get CountPulse from Driver
                defaultRet = Smc.WGetCountPulse(DeviceId, AxisNo, out lCountPulse);
                status.CountPulse = lCountPulse;
                status.SetResult(defaultRet);
                // Get Pulse Status from Driver
                defaultRet = Smc.WGetPulseStatus(DeviceId, AxisNo, out PulseSts);
                status.OutPulseStatus = PulseSts;
                status.SetResult(defaultRet);
                status.PulseStatusStr = GetPulseStatusStr(PulseSts);

                // Get Move Status from Driver
                defaultRet = Smc.WGetMoveStatus(DeviceId, AxisNo, out MoveSts);
                status.MoveStatus = MoveSts;
                status.SetResult(defaultRet);
                status.MoveStatusStr = GetMoveStatusStr(MoveSts);
                // Get Stop Status from Driver
                defaultRet = Smc.WGetStopStatus(DeviceId, AxisNo, out StopSts);
                status.StopStatus = StopSts;
                status.SetResult(defaultRet);
                status.StopStatusStr = GetStopStatusStr(StopSts);

                // Get Limit Status from Driver      
                defaultRet = Smc.WGetLimitStatus(DeviceId, AxisNo, out LimitSts);
                status.LimitStatus = LimitSts;
                status.SetResult(defaultRet);

                short AlarmCode = 0;
                Smc.WGetAlarmCode(DeviceId, AxisNo, out AlarmCode);
                status.AlarmCode = AlarmCode;
            }
            return status;
        }
        /// <summary>
        /// Smc.WGetStopStatus返回的状态列表
        /// </summary>
        public static string GetStopStatusStr(short StopSts)
        {
            string wStr = "";
            switch (StopSts)
            {
                case (short)CSmcConst.CSMC_MOVE:
                    wStr = "Moving"; break;
                case (short)CSmcConst.CSMC_STOP_COMMAND:
                    wStr = "Stop Command"; break;
                case (short)CSmcConst.CSMC_SD_COMMAND:
                    wStr = "Slowdown Stop Command"; break;
                case (short)CSmcConst.CSMC_STOP_OTHER:
                    wStr = "Stop by other axis"; break;
                case (short)CSmcConst.CSMC_STOP_ALARM:
                    wStr = "Stop by alarm"; break;
                case (short)CSmcConst.CSMC_STOP_PLIM:
                    wStr = "Stop by +LIM"; break;
                case (short)CSmcConst.CSMC_STOP_MLIM:
                    wStr = "Stop by -LIM"; break;
                case (short)CSmcConst.CSMC_STOP_SD:
                    wStr = "Stop by SD"; break;
                case (short)CSmcConst.CSMC_ERROR_ORG:
                    wStr = "Stop by ORG ERROR"; break;
                case (short)CSmcConst.CSMC_ERROR_PULSER:
                    wStr = "Stop by PULSER ERROR"; break;
                case (short)CSmcConst.CSMC_STOP_NORMAL:
                    wStr = "Normal Stop"; break;
                default:
                    wStr = "Unknown Status"; break;
            }
            return wStr;
        }
        /// <summary>
        /// WGetMoveStatus方法返回的状态列表
        /// </summary>
        public static string GetMoveStatusStr(short MoveSts)
        {
            string wStr = "";
            switch (MoveSts)
            {
                case (short)CSmcConst.CSMC_STOP:
                    wStr = "Stop Motion"; break;
                case (short)CSmcConst.CSMC_PTP:
                    wStr = "PTP Motion"; break;
                case (short)CSmcConst.CSMC_JOG:
                    wStr = "JOG Motion"; break;
                case (short)CSmcConst.CSMC_ORG:
                    wStr = "ORG Motion"; break;
                case (short)CSmcConst.CSMC_SINGLE:
                    wStr = "Single (Bank) Motion"; break;
                case (short)CSmcConst.CSMC_LOOP:
                    wStr = "Loop (Bank) Motion"; break;
                case (short)CSmcConst.CSMC_ZMOVE:
                    wStr = "Z-phase count Motion"; break;
                case (short)CSmcConst.CSMC_PLSER:
                    wStr = "Pulser Motion"; break;
                default:
                    wStr = "Unknown Status"; break;
            } return wStr;
        }
        /// <summary>
        /// WGetPulseStatus方法返回的状态值
        /// </summary>
        public static string GetPulseStatusStr(short PulseSts)
        {
            string wStr = "";
            switch (PulseSts)
            {
                case (short)CSmcConst.CSMC_PLS_STOP:
                    wStr = "Pulse Stop"; break;
                case (short)CSmcConst.CSMC_PLS_FLCONST:
                    wStr = "Pulse FL Constant"; break;
                case (short)CSmcConst.CSMC_PLS_FHCONST:
                    wStr = "Pulse FH Constant"; break;
                case (short)CSmcConst.CSMC_PLS_READY:
                    wStr = "Pulse Ready"; break;
                case (short)CSmcConst.CSMC_PLS_ERCW:
                    wStr = "Pulse ERC Timer"; break;
                case (short)CSmcConst.CSMC_PLS_DTMW:
                    wStr = "Pulse Dir Timer"; break;
                case (short)CSmcConst.CSMC_PLS_ACCEL:
                    wStr = "Pulse Accel"; break;
                case (short)CSmcConst.CSMC_PLS_DECEL:
                    wStr = "Pulse Decel"; break;
                case (short)CSmcConst.CSMC_PLS_INPW:
                    wStr = "Pulse INP Wait"; break;
                case (short)CSmcConst.CSMC_PLS_PAPBW:
                    wStr = "Pulser Input Wait"; break;
                default:
                    wStr = "Unknown Status"; break;
            }
            return wStr;
        }

        /// <summary>
        /// 轴运动,需要传入完整的 参数
        /// </summary> 
        public static string Move(KTKMoveParam moveParam)
        {
            string DeviceName = moveParam.DeviceName;
            short AxisNo = moveParam.AxisNo;

            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                int TargetPosition = moveParam.TargetPosition;
                short StartDir = moveParam.StartDir;
                double targetSpeed = moveParam.TargetSpeed;
                if (moveParam.MotionType == (short)CSmcConst.CSMC_PTP && moveParam.BCoordinate == (short)CSmcConst.CSMC_INC)
                {
                    TargetPosition = Math.Abs(TargetPosition);
                    if (StartDir == (short)CSmcConst.CSMC_CCW)
                    {
                        TargetPosition = -1 * (TargetPosition);
                    }
                    moveParam.TargetPosition = (short)TargetPosition;
                }
                if (moveParam.TargetSpeed < 0)
                {
                    moveParam.TargetSpeed = Math.Abs(moveParam.TargetSpeed);
                }
                moveParam.StartDir = StartDir;
                string msg = SetMoveParam(moveParam);
                if (!msg.Equals(""))
                {
                    return msg;
                }
                defaultRet = Smc.WSetStopPosition(DeviceId, AxisNo, moveParam.BCoordinate, TargetPosition);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Ready motion 
                defaultRet = Smc.WSetReady(DeviceId, AxisNo, moveParam.MotionType, moveParam.StartDir);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Start Motion 
                defaultRet = Smc.WMotionStart(DeviceId, AxisNo);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                return "";
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
        }

        private static string SetMoveParam(KTKMoveParam param)
        {
            return SetMoveParam(param.DeviceName, param.AxisNo, param.ResolveSpeed, param.StartSpeed, param.TargetSpeed, param.AccelTime, param.DecelTime, param.SSpeed);
        }
        /// <summary>
        /// 设置运动参数
        /// </summary> 
        private static string SetMoveParam(String DeviceName, short AxisNo, double ResolveSpeed, double StartSpeed, double TargetSpeed, short AccelTime, short DecelTime, double SSpeed)
        {
            //double dblResolveSpeed = 1000;
            //double dblStartSpeed = 1000;
            //double dblTargetSpeed = 1000;
            //short dblAccelTime = 10;
            //short dblDecelTime = 10;
            //double dblSSpeed = 1000;

            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                // Set      SmcWSetOrgMode( Id, AxisNo, LimitTurn, OrgType, EndDir, ZCount )

                short LimitTurn = 1;
                short OrgType = 0;
                short EndDir = 2; //CCW
                short ZCount = 1;

                defaultRet = Smc.WSetOrgMode(DeviceId, AxisNo, LimitTurn, OrgType, EndDir, ZCount);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Set parameters to Driver                
                defaultRet = Smc.WSetResolveSpeed(DeviceId, AxisNo, ResolveSpeed);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Set StartSpeed to Driver                 
                defaultRet = Smc.WSetStartSpeed(DeviceId, AxisNo, StartSpeed);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Set TargetSpeed to Driver               
                if (TargetSpeed < 0)
                {
                    //目标速度不能设置负数
                    TargetSpeed = TargetSpeed * (-1);
                }
                defaultRet = Smc.WSetTargetSpeed(DeviceId, AxisNo, TargetSpeed);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Set AccelTime to Driver                 
                defaultRet = Smc.WSetAccelTime(DeviceId, AxisNo, AccelTime);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Set DecelTime to Driver                 
                defaultRet = Smc.WSetDecelTime(DeviceId, AxisNo, DecelTime);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Set SSpeed to Driver
                defaultRet = Smc.WSetSSpeed(DeviceId, AxisNo, SSpeed);
                return WGetErrorString(defaultRet);
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
        }
        public static string ABSMove(String DeviceName, short AxisNo, int targetPosition)
        {
            return ABSMove(DeviceName, AxisNo, targetPosition, (int)Move_TargetSpeed, Move_AccelTime, Move_lDecelTime, (int)Move_ResolveSpeed);
        }
        public static string ABSMove(String DeviceName, short AxisNo, int targetPosition, int startSpeed, int targetSpeed, int resolveSpeed)
        {
            return ABSMove(DeviceName, AxisNo, targetPosition, targetSpeed, Move_AccelTime, Move_lDecelTime, resolveSpeed);
        }
        /// <summary>
        /// 绝对路径运动,会触发事件
        /// </summary> 
        public static string ABSMove(String DeviceName, short AxisNo, int targetPosition, int targetSpeed, short accelTime, short decelTime, int resolveSpeed)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                int currPosition = WGetOutPulse(DeviceName, AxisNo);
                //已经在指定位置,如果继续运行会出错
                if (currPosition == targetPosition)
                {
                    //if (AbsMoveEnd != null)
                    //{
                    //    AbsMoveEnd.Invoke(DeviceName, 0, AxisNo, targetPosition);
                    //}
                    return "";
                }
                //旋转轴的开始速度=目标速度/20,其他轴的开始速度=目标速度/10
                int startSpeed = targetSpeed / 10;
                if (AxisNo % 4 == 1)
                {
                    startSpeed = targetSpeed / 20;
                }
                SetMoveParam(DeviceName, AxisNo, resolveSpeed, startSpeed, targetSpeed, accelTime, decelTime, Move_SSpeed);
                short StartDir = (short)CSmcConst.CSMC_CW;
                short bCoordinate = (short)CSmcConst.CSMC_ABS;
                defaultRet = Smc.WSetStopPosition(DeviceId, AxisNo, bCoordinate, targetPosition);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }

                defaultRet = Smc.WSetReady(DeviceId, AxisNo, (short)CSmcConst.CSMC_PTP, StartDir);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                //开始运动
                defaultRet = Smc.WMotionStart(DeviceId, AxisNo);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }

            }
            else
            {
                return getNotInitStr(DeviceName);
            }
            return "";
        }
        /// <summary>
        /// 相对路径运动
        /// </summary>
        public static string RelMove(String DeviceName, short AxisNo, int targetPosition)
        {
            return RelMove(DeviceName, AxisNo, targetPosition, (int)Move_TargetSpeed, (int)Move_ResolveSpeed);
        }
        /// <summary>
        /// 相对路径运动
        /// </summary> 
        public static string RelMove(String DeviceName, short AxisNo, int targetPosition, int speed, int resolveSpeed)
        {
            return RelMove(DeviceName, AxisNo, targetPosition, speed, Move_AccelTime, Move_lDecelTime, resolveSpeed);
            
        }
        /// <summary>
        /// 相对路径运动
        /// </summary> 
        public static string RelMove(String DeviceName, short AxisNo, int targetPosition, int speed, short addTime, short delTime, int resolveSpeed)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;

                SetMoveParam(DeviceName, AxisNo, resolveSpeed, speed / 10, speed, addTime, delTime, Move_SSpeed);
                short StartDir = (short)CSmcConst.CSMC_CW;
                //负数需要反方向运动
                if (targetPosition < 0)
                {
                    StartDir = (short)CSmcConst.CSMC_CCW;
                    //   targetPosition = -targetPosition;
                }
                short bCoordinate = (short)CSmcConst.CSMC_INC;

                defaultRet = Smc.WSetStopPosition(DeviceId, AxisNo, bCoordinate, targetPosition);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Ready motion               
                defaultRet = Smc.WSetReady(DeviceId, AxisNo, (short)CSmcConst.CSMC_PTP, StartDir);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Start Motion               
                defaultRet = Smc.WMotionStart(DeviceId, AxisNo);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
            return "";
        }
        public static string VolMove(String DeviceName, short AxisNo, int Speed, int resolveSpeed)
        {
            return VolMove(DeviceName, AxisNo, Speed, Move_AccelTime, Move_lDecelTime, resolveSpeed);
        }
        /// <summary>
        /// 匀速运动
        /// </summary> 
        public static string VolMove(String DeviceName, short AxisNo, int Speed, short accelTime, short decelTime, int resolveSpeed)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                short StartDir = (short)CSmcConst.CSMC_CW;
                if (Speed < 0)
                {
                    Speed = (0 - Speed);
                    StartDir = (short)CSmcConst.CSMC_CCW;
                }
                SetMoveParam(DeviceName, AxisNo, resolveSpeed, Speed / 10, Speed, accelTime, decelTime, Move_SSpeed);

                short bCoordinate = (short)CSmcConst.CSMC_INC;
                defaultRet = Smc.WSetStopPosition(DeviceId, AxisNo, bCoordinate, Speed);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Ready motion               
                defaultRet = Smc.WSetReady(DeviceId, AxisNo, (short)CSmcConst.CSMC_JOG, StartDir);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
                // Start Motion               
                defaultRet = Smc.WMotionStart(DeviceId, AxisNo);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet);
                }
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
            return "";
        }

        private static bool isLimClose(string DeviceName, short DeviceId, short AxisNo, int speed, int count, int resolveSpeed)
        {
            if (count < 0)
            {
                LOGGER.Info("deviceName:" + DeviceName + ",axis:" + AxisNo + "HOMEMOVE前倒数第" + count + "次验证,直接返回ture,不再进行验证");
                return true;
            }

            //判断此轴的LIM-  LIM+灯是否亮着,如果亮着,需要先反方向走一段距离,当灯灭后,在开始原点返回
            short LimitSts = 0;
            defaultRet = Smc.WGetLimitStatus(DeviceId, AxisNo, out LimitSts);

            bool limjian = ((LimitSts & (short)CSmcConst.CSMC_MLIM) == (int)CSmcConst.CSMC_MLIM);
            bool limjia = ((LimitSts & (short)CSmcConst.CSMC_PLIM) == (int)CSmcConst.CSMC_PLIM);
            int limMoveSpeed = 0;
            if (limjia)
            {
                limMoveSpeed = -1 * Math.Abs(speed);
            }
            else if (limjian)
            {
                limMoveSpeed = Math.Abs(speed);
            }
            else
            {
                return true;
            }
            //如果LIM+亮,需要负方向行走直到LIM+灭
            //如果LIM-亮,需要正方向行走直到LIM-灭

            LOGGER.Info("deviceName:" + DeviceName + ",axis:" + AxisNo + "HOMEMOVE前倒数第" + count + "次验证,发现LIM灯亮,以" + speed + "的速度走300毫秒,停止运动后重新验证");
            VolMove(DeviceName, AxisNo, limMoveSpeed, resolveSpeed);
            System.Threading.Thread.Sleep(300);
            StopMove(DeviceName, AxisNo);

            count--;
            return isLimClose(DeviceName, DeviceId, AxisNo, speed, count, resolveSpeed);

        }
        /// <summary>
        /// 原点返回运动
        /// </summary> 
        public static string HomeMove(string DeviceName, short AxisNo)
        {
            return HomeMove(DeviceName, AxisNo, (int)Move_StartSpeed, (int)Move_TargetSpeed, Move_AccelTime, Move_lDecelTime, (int)Move_ResolveSpeed);
        }
        public static string HomeMove(string DeviceName, short AxisNo, int startSpeed, int targetSpeed, int resolveSpeed)
        {
            return HomeMove(DeviceName, AxisNo, startSpeed, targetSpeed, Move_AccelTime, Move_lDecelTime, resolveSpeed);
        }
        public static string HomeMove(string DeviceName, short AxisNo, int startSpeed, int targetSpeed, short accelTime, short decelTime, int resolveSpeed)
        {
            return HomeMove(DeviceName, AxisNo, startSpeed, targetSpeed, accelTime, decelTime, resolveSpeed, false);
        }

        public static string HomeMove(string DeviceName, short AxisNo, int startSpeed, int targetSpeed, short accelTime, short decelTime, int resolveSpeed,bool middleAxisIsReset)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                //调用此方法验证LIM灯是否都灭了(当灯亮时,会返回失败!)
                isLimClose(DeviceName, DeviceId, AxisNo, targetSpeed, 10, resolveSpeed);
                SetMoveParam(DeviceName, AxisNo, resolveSpeed, startSpeed, targetSpeed, accelTime, decelTime, Move_SSpeed);
                short StartDir = (short)CSmcConst.CSMC_CW;
                if (AxisNo % 4 == 2 || DBStoreManager.configMap.Count > 0)
                {
                    StopMove(DeviceName, AxisNo);
                    //上升轴向上走两万个脉冲,防止过载,负方向走20000
                    RelMove(DeviceName, AxisNo, -20000, targetSpeed, resolveSpeed);
                    LOGGER.Info("DeviceName=" + DeviceName + ",axisNo=" + AxisNo + "上升轴向上走两万个脉冲,防止过载,负方向走20000");
                    for (int i = 0; i < 10; i++)
                    {
                        short MoveSts, StopSts;
                        defaultRet = Smc.WGetMoveStatus(DeviceId, AxisNo, out MoveSts);
                        defaultRet = Smc.WGetStopStatus(DeviceId, AxisNo, out StopSts);
                        short InData = KTKSMCManager.WGetDigitalIn(DeviceName, AxisNo);
                        if (StopSts != (short)CSmcConst.CSMC_MOVE && MoveSts == (short)CSmcConst.CSMC_STOP)
                        {
                            //IO_VALUE in2Value = IO_VALUE.LOW;
                            if ((InData & (short)CSmcConst.CSMC_IN2) == (int)CSmcConst.CSMC_IN2)
                            {
                                LOGGER.Info("DeviceName=" + DeviceName + ",axisNo=" + AxisNo + "原点返回前负方向走20000停止");
                                break;
                            }
                        } 
                        System.Threading.Thread.Sleep(500);
                    }
                }
                else if (AxisNo % 4 == 1 && middleAxisIsReset)
                {
                    StopMove(DeviceName, AxisNo);
                    //旋转轴负方向走到-50000,因为反向回原点太慢了
                    LOGGER.Info("DeviceName=" + DeviceName + ",axisNo=" + AxisNo + "旋转轴负方向走到-50000,因为反向回原点太慢了");
                    ABSMove(DeviceName, AxisNo, -50000, startSpeed, targetSpeed, resolveSpeed);
                    for (int i = 0; i < 20; i++)
                    {
                        short MoveSts, StopSts;
                        defaultRet = Smc.WGetMoveStatus(DeviceId, AxisNo, out MoveSts);
                        defaultRet = Smc.WGetStopStatus(DeviceId, AxisNo, out StopSts);
                        short InData = KTKSMCManager.WGetDigitalIn(DeviceName, AxisNo);
                        if (StopSts != (short)CSmcConst.CSMC_MOVE && MoveSts == (short)CSmcConst.CSMC_STOP)
                        {
                            //IO_VALUE in2Value = IO_VALUE.LOW;
                            if ((InData & (short)CSmcConst.CSMC_IN2) == (int)CSmcConst.CSMC_IN2)
                            {
                                LOGGER.Info("DeviceName=" + DeviceName + ",axisNo=" + AxisNo + "旋转轴负方向走到-50000停止");
                                break;
                            }
                        }
                        System.Threading.Thread.Sleep(500);
                    } 
                }
                StopMove(DeviceName, AxisNo); 
                // Ready motion               
                defaultRet = Smc.WSetReady(DeviceId, AxisNo, (short)CSmcConst.CSMC_ORG, StartDir);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet); ;
                }
                // Start Motion               
                defaultRet = Smc.WMotionStart(DeviceId, AxisNo);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet); ;
                } 
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
            return "";
        }

        public static string HomeMove(string DeviceName, short AxisNo, int startSpeed, int targetSpeed, int resolveSpeed, short StartDir)
        {
            return HomeMove(DeviceName, AxisNo, startSpeed, targetSpeed, Move_AccelTime, Move_lDecelTime, resolveSpeed,StartDir);
        }
        public static string HomeMove(string DeviceName, short AxisNo, int startSpeed, int targetSpeed, short accelTime, short decelTime, int resolveSpeed, short StartDir)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                //调用此方法验证LIM灯是否都灭了(当灯亮时,会返回失败!)
                isLimClose(DeviceName, DeviceId, AxisNo, targetSpeed, 10, resolveSpeed);
                SetMoveParam(DeviceName, AxisNo, resolveSpeed, startSpeed, targetSpeed, accelTime, decelTime, Move_SSpeed);
                //StartDir = (short)CSmcConst.CSMC_CW;
                if (SAStoreManager.store != null)
                {
                    //先判断原点是否亮,如果原点亮了,先反方向走一点再原点放回
                    short LimitSts = 0;
                    defaultRet = Smc.WGetLimitStatus(DeviceId, AxisNo, out LimitSts);
                    bool org = ((LimitSts & (short)CSmcConst.CSMC_ORGLIM) == (int)CSmcConst.CSMC_ORGLIM);
                    if (org)
                    {
                        int positionValue = Math.Abs(targetSpeed * 3);
                        if (StartDir.Equals((short)CSmcConst.CSMC_CW))
                        {
                            positionValue = 0 - positionValue * 2;
                        }
                      
                        //康泰克单台料仓,先反方向走一点
                        StopMove(DeviceName, AxisNo);
                        RelMove(DeviceName, AxisNo, positionValue, targetSpeed, resolveSpeed);
                        LOGGER.Info("DeviceName=" + DeviceName + ",axisNo=" + AxisNo + "原点返回,原点已经亮,先正方向走" + positionValue);
                        for (int i = 0; i < 10; i++)
                        {
                            short MoveSts, StopSts;
                            defaultRet = Smc.WGetMoveStatus(DeviceId, AxisNo, out MoveSts);
                            defaultRet = Smc.WGetStopStatus(DeviceId, AxisNo, out StopSts);
                            short InData = KTKSMCManager.WGetDigitalIn(DeviceName, AxisNo);
                            if (StopSts != (short)CSmcConst.CSMC_MOVE && MoveSts == (short)CSmcConst.CSMC_STOP)
                            {
                                if ((InData & (short)CSmcConst.CSMC_IN2) == (int)CSmcConst.CSMC_IN2)
                                {
                                    LOGGER.Info("DeviceName=" + DeviceName + ",axisNo=" + AxisNo + "原点返回,原点已经亮,正方向走" + positionValue + "停止");
                                    break;
                                }
                            }
                            System.Threading.Thread.Sleep(500);
                        }
                    }
                }

                StopMove(DeviceName, AxisNo);
                // Ready motion               
                defaultRet = Smc.WSetReady(DeviceId, AxisNo, (short)CSmcConst.CSMC_ORG, StartDir);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet); ;
                }
                // Start Motion               
                defaultRet = Smc.WMotionStart(DeviceId, AxisNo);
                if (defaultRet != 0)
                {
                    return WGetErrorString(defaultRet); ;
                }
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
            return "";
        }
       
        /// <summary>
        /// 停止运动
        /// </summary> 
        public static string StopMove(String DeviceName, short AxisNo)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WMotionStop(DeviceId, AxisNo);
                return WGetErrorString(defaultRet);
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
        }
        /// <summary>
        /// 减速停止
        /// </summary> 
        public static string SDStopMove(String DeviceName, short AxisNo)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WMotionDecStop(DeviceId, AxisNo);
                return WGetErrorString(defaultRet);
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
        }
        /// <summary>
        /// 获取移动参数
        /// </summary> 
        public static KTKMoveParam GetMoveParam(string DeviceName, short AxisNo, short bCoordinate)
        {
            KTKMoveParam param = new KTKMoveParam();
            string wStr = "";
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;

                double dblResolveSpeed = 0;
                double dblStartSpeed = 0;
                double dblTargetSpeed = 0;
                double dblAccelTime = 0;
                double dblDecelTime = 0;
                double dblSSpeed = 0;
                int targetPosition = 0;

                // Set default value of Resolution                
                defaultRet = Smc.WGetResolveSpeed(DeviceId, AxisNo, out dblResolveSpeed);
                if (defaultRet != 0)
                {
                    wStr = WGetErrorString(defaultRet);
                }

                // Set default value of StartSpeed                
                defaultRet = Smc.WGetStartSpeed(DeviceId, AxisNo, out dblStartSpeed);
                if (defaultRet != 0)
                {
                    wStr = WGetErrorString(defaultRet);
                }

                // Set default value of TargetSpeed                
                defaultRet = Smc.WGetTargetSpeed(DeviceId, AxisNo, out dblTargetSpeed);
                if (defaultRet != 0)
                {
                    wStr = WGetErrorString(defaultRet);
                }

                // Set default value of AccelTime                
                defaultRet = Smc.WGetAccelTime(DeviceId, AxisNo, out dblAccelTime);
                if (defaultRet != 0)
                {
                    wStr = WGetErrorString(defaultRet);
                }

                // Set default value of DecelTime                
                defaultRet = Smc.WGetDecelTime(DeviceId, AxisNo, out dblDecelTime);
                if (defaultRet != 0)
                {
                    wStr = WGetErrorString(defaultRet);
                }

                // Set default value of SSpeed     
                defaultRet = Smc.WGetSSpeed(DeviceId, AxisNo, out dblSSpeed);
                if (defaultRet != 0)
                {
                    wStr = WGetErrorString(defaultRet);
                }

                // Set default value of lDistance                
                defaultRet = Smc.WGetStopPosition(DeviceId, AxisNo, bCoordinate, out targetPosition);
                if (defaultRet != 0)
                {
                    wStr = WGetErrorString(defaultRet);
                }
                param.DeviceName = DeviceName;
                param.AxisNo = AxisNo;
                param.BCoordinate = bCoordinate;
                param.DecelTime = (short)dblDecelTime;
                param.AccelTime = (short)dblAccelTime;
                param.SSpeed = dblSSpeed;
                param.ResolveSpeed = dblResolveSpeed;
                param.StartSpeed = dblStartSpeed;
                param.TargetSpeed = dblTargetSpeed;
                param.TargetPosition = (short)targetPosition;
            }
            return param;
        }

        public static string WSetOutPulse(string DeviceName, short AxisNo, int OutPulse)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WSetOutPulse(DeviceId, AxisNo, OutPulse);
                return WGetErrorString(defaultRet);
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
        }

        public static string WSetCountPulse(string DeviceName, short AxisNo, int CountPulse)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WSetCountPulse(DeviceId, AxisNo, CountPulse);
                return WGetErrorString(defaultRet);
            }
            else
            {
                return getNotInitStr(DeviceName);
            }
        }
        public static short WGetDigitalOut(string DeviceName, short AxisNo)
        {
            short OutData = 0;
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WGetDigitalOut(DeviceId, AxisNo, out OutData);
            }
            return OutData;
        }

        public static short WGetDigitalIn(string DeviceName, short AxisNo)
        {
            short InData = 0;
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WGetDigitalIn(DeviceId, AxisNo, out InData);
            }
            return InData;
        }
        public static IO_VALUE WGetDigitalIn(string DeviceName, short AxisNo, int index)
        {
            short InData = WGetDigitalIn(DeviceName, AxisNo);
            switch (index)
            {
                case 1:
                    return ((InData & (short)CSmcConst.CSMC_IN1) == (int)CSmcConst.CSMC_IN1) ? IO_VALUE.HIGH : IO_VALUE.LOW;
                case 2:
                    return ((InData & (short)CSmcConst.CSMC_IN2) == (int)CSmcConst.CSMC_IN2) ? IO_VALUE.HIGH : IO_VALUE.LOW;
                case 3:
                    return ((InData & (short)CSmcConst.CSMC_IN3) == (int)CSmcConst.CSMC_IN3) ? IO_VALUE.HIGH : IO_VALUE.LOW;
                case 4:
                    return ((InData & (short)CSmcConst.CSMC_IN4) == (int)CSmcConst.CSMC_IN4) ? IO_VALUE.HIGH : IO_VALUE.LOW;
                case 5:
                    return ((InData & (short)CSmcConst.CSMC_IN5) == (int)CSmcConst.CSMC_IN5) ? IO_VALUE.HIGH : IO_VALUE.LOW;
                case 6:
                    return ((InData & (short)CSmcConst.CSMC_IN6) == (int)CSmcConst.CSMC_IN6) ? IO_VALUE.HIGH : IO_VALUE.LOW;
                case 7:
                    return ((InData & (short)CSmcConst.CSMC_IN7) == (int)CSmcConst.CSMC_IN7) ? IO_VALUE.HIGH : IO_VALUE.LOW;
                default:
                    return IO_VALUE.LOW;
            }

        }
        public static short WGetHoldOff(string DeviceName, short AxisNo)
        {
            short HoldOff = 0;
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                defaultRet = Smc.WGetHoldOff(DeviceId, AxisNo, out HoldOff);
            }
            return HoldOff;
        }

        /// <summary>
        /// 判断轴是否运动完成
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static bool IsMoveEnd(string DeviceName, short AxisNo)
        {
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                short MoveSts, StopSts;
                defaultRet = Smc.WGetMoveStatus(DeviceId, AxisNo, out MoveSts);
                defaultRet = Smc.WGetStopStatus(DeviceId, AxisNo, out StopSts);
                short InData = KTKSMCManager.WGetDigitalIn(DeviceName, AxisNo);
                if (StopSts != (short)CSmcConst.CSMC_MOVE && MoveSts == (short)CSmcConst.CSMC_STOP)
                {
                    //IO_VALUE in2Value = IO_VALUE.LOW;
                    if ((InData & (short)CSmcConst.CSMC_IN2) == (int)CSmcConst.CSMC_IN2)
                    {
                        //LOGGER.Info("DeviceName=" + DeviceName + ",axisNo=" + AxisNo + "运动已停止");
                        return true;
                    }
                }
            }
            return false;
        }
        public static IO_VALUE GetOrgValue(string DeviceName, short AxisNo)
        {
            //需要停止运动并且org=true
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                short LimitSts;
                defaultRet = Smc.WGetLimitStatus(DeviceId, AxisNo, out LimitSts);
                //原点信号也亮
                bool org = ((LimitSts & (short)CSmcConst.CSMC_ORGLIM) == (int)CSmcConst.CSMC_ORGLIM);
                if (org)
                {
                    return IO_VALUE.HIGH;
                }
            }
            return IO_VALUE.LOW;
        }
        public static bool IsHomeMoveEnd(string DeviceName, short AxisNo)
        {
            //需要停止运动并且org=true
            if (SMCDeviceMap.ContainsKey(DeviceName))
            {
                short DeviceId = SMCDeviceMap[DeviceName].DeviceId;
                short MoveSts, StopSts, LimitSts;
                defaultRet = Smc.WGetMoveStatus(DeviceId, AxisNo, out MoveSts);
                defaultRet = Smc.WGetStopStatus(DeviceId, AxisNo, out StopSts);
                short InData = KTKSMCManager.WGetDigitalIn(DeviceName, AxisNo);
                if (StopSts != (short)CSmcConst.CSMC_MOVE && MoveSts == (short)CSmcConst.CSMC_STOP)
                {
                    //IN2亮
                    if ((InData & (short)CSmcConst.CSMC_IN2) == (int)CSmcConst.CSMC_IN2)
                    {
                        defaultRet = Smc.WGetLimitStatus(DeviceId, AxisNo, out LimitSts);
                        //原点信号也亮
                        bool org = ((LimitSts & (short)CSmcConst.CSMC_ORGLIM) == (int)CSmcConst.CSMC_ORGLIM);
                        if (org)
                        {
                            return true;
                        }

                    }
                }
            }
            return false;
        }

    }
}