import static lib.mtn_lib20.*;
import static lib.mtn_lib20_com.*;
import static lib.mtn_lib20_ext.*;
import static lib.mtn_lib20_oem.*;
import static lib.mtn_lib20_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();
        HAND crdHandle = new HAND();// 坐标系句柄
        PHAND pcrdHandle = 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,"轴使能出错");
        rtn = NMC_CrdOpen(devHandle, pcrdHandle);			//打开坐标系
        checkError(rtn,"打开坐标系出错");
        crdHandle = pcrdHandle.getHandle();

        TCrdConfig crd = new TCrdConfig();
        crd.axCnts = 3;			// 设置3轴坐标系
        crd.pAxArray[0] = 0;	// 轴端口0对应X轴
        crd.pAxArray[1] = 1;	// 轴端口1对应Y轴
        crd.pAxArray[2] = 2;	// 轴端口2对应Z轴
        crd.pAxArray[3] = -1;	// 轴端口3对应A轴，-1为不使用
        crd.port[0] = 0;		// 端口设为0
        crd.port[1] = 0;		// 端口设为0
        crd.port[2] = 0;		// 端口设为0
        crd.port[3] = 0;		// 端口设为0

        rtn = NMC_CrdConfig(crdHandle, crd);		//配置坐标系
        checkError(rtn,"配置坐标系出错");
        TCrdPara crdPara = new TCrdPara();
        crdPara.orgFlag = 1;						// 默认原点(0,0,0,0)
        crdPara.offset[0] = 0;						// 坐标原点设为0
        crdPara.offset[1] = 0;						// 坐标原点设为0
        crdPara.offset[2] = 0;						// 坐标原点设为0
        crdPara.offset[3] = 0;						// 坐标原点设为0
        crdPara.synAccMax = 50;				//最大合成加速度p/ms^2,50已经比较大
        crdPara.synVelMax = 800;			//最大合成速度,允许范围内，两个参数尽可能大

        rtn = NMC_CrdSetPara(crdHandle, crdPara);	// 设置运动参数
        checkError(rtn,"设置运动参数出错");

        /******************* 以下配置可选 *******************/
        TCrdSafePara crdSafePara = new TCrdSafePara();
        crdSafePara.estpDec = 1000;					//急停加速度
        crdSafePara.maxAcc = 50;					//设置坐标系最大加速度50p/ms^2
        crdSafePara.maxVel = 800;					//设置坐标系最大速度800p/ms
        rtn = NMC_CrdSetSafePara(crdHandle, crdSafePara);//设置安全参数
        checkError(rtn,"设置安全参数出错");
        /******************* 以上配置可选 *******************/

        rtn = NMC_CrdClrError(crdHandle);			//清除坐标系错误状态
        checkError(rtn,"清除坐标系错误状态出错");
        rtn = NMC_CrdBufClr(crdHandle);				//压入指令之前需要清空缓存区
        checkError(rtn,"压入指令之前需要清空缓存区出错");
    }
}
