import static lib.MtnLib.*;
import static lib.MtnLib_com.*;
import static lib.MtnLib_ome.*;
import static lib.MtnLib_err.*;
import com.sun.jna.*;
import com.sun.jna.ptr.*;

public class Main {
    static {
        String libPath = System.getProperty("user.dir") + "\\lib";
        System.setProperty("java.library.path", libPath);
        System.load(libPath + "\\msvcr100.dll");
        System.load(libPath + "\\msvcp100.dll");
        System.load(libPath + "\\nmc_lib20.dll");
    }
    private static void checkError(short rtn, String operation) {
        if (rtn != 0) {
            System.err.printf("错误: %s (代码: %d)\n", operation, rtn);
//            System.exit(1);
        }
    }
    public static void main(String[] args) {

        short rtn;// 指令返回值
        short axisNum = 0;// 轴号，从0开始
        HAND devHandle = new HAND();// 设备句柄
        PHAND pDevHandle = new PHAND();
        HAND axisHandle = new HAND();// 轴句柄
        PHAND pAxisHandle = new PHAND();

        short[] devCount = new short[1];
        TDevInfo[] devInfoList = new TDevInfo[10];
        rtn = NMC_DevSearch(TSearchMode.Ethernet, devCount, devInfoList);
        checkError(rtn,"搜索设备出错");
        rtn = NMC_DevOpen(devInfoList[0].address, pDevHandle);				//打开控制器
        checkError(rtn,"打开控制器出错");
        devHandle = pDevHandle.getHandle();
        rtn = NMC_MtOpen(devHandle, axisNum, pAxisHandle); // 打开轴
        checkError(rtn,"打开轴出错");
        axisHandle = pAxisHandle.getHandle();
        rtn = NMC_MtSetSvOn(axisHandle);//打开轴使能(根据实际情况使用)
        checkError(rtn,"轴使能出错");

        short source = 0;   // 0:内部脉冲计数    1:外部编码器输入
        short dir = 0;      // 0:AB相90度差    1:脉冲+方向   2:正脉冲+负脉冲
                            // 3:AB相90度差(负逻辑) 4:脉冲+方向(负逻辑) 5:正脉冲+负脉冲(负逻辑)
        short encMode = 0;      // 编码器模式
        switch (source)
        {
            case 0:
                switch (dir)
                {
                    case 0:
                        encMode = 256;  break;
                    case 1:
                        encMode = 1280; break;
                    case 2:
                        encMode = 2304; break;
                    case 3:
                        encMode = 4352; break;
                    case 4:
                        encMode = 17664; break;
                    case 5:
                        encMode = 6400; break;
                    default:break;
                } break;
            case 1:
                switch (dir)
                {
                    case 0:
                        encMode = 0;      break;
                    case 1:
                        encMode = 1024; break;
                    case 2:
                        encMode = 2048; break;
                    case 3:
                        encMode = 4096; break;
                    case 4:
                        encMode = 17408; break;
                    case 5:
                        encMode = 6144; break;
                    default: break;
                } break;
            default:break;
        }
        rtn = NMC_SetEncMode(devHandle, axisNum, encMode);  // 设置编码器模式
        checkError(rtn,"设置编码器模式出错");

    }
}
