package com.example;

import static com.example.mtnlib.nmc_lib20.*;
import static com.example.mtnlib.nmc_lib20_ext.*;
import static com.example.mtnlib.nmc_lib20_oem.*;
import static com.example.mtnlib.nmc_lib20_err.*;
import com.example.mtnlib.nmc_lib20_com.*;
import com.sun.jna.ptr.*;
import com.sun.jna.NativeLong;

public class Main {
    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((short)0, pDevHandle);				//打开控制器（0表示设备序号）
        checkError(rtn,"打开控制器出错");
        devHandle = pDevHandle.getHandle();
        rtn = NMC_MtOpen(devHandle, axisNum, pAxisHandle); // 打开轴
        checkError(rtn,"打开轴出错");
        axisHandle = pAxisHandle.getHandle();
        rtn = NMC_MtSetSvOn(axisHandle);//打开轴使能(根据实际情况使用)
        checkError(rtn,"轴使能出错");

        //单轴点位运动(相对运动)例程
        double acc = 0.5;								//加速度
        double dec = 0.5;								//减加速度
        double endVel = 0;								//结束速度
        double startVel = 0;							//起跳速度
        double maxVel = 10;								// 最大速度
        short smoothCoef = 0;							//平滑系数
        NativeLong tgtPos = new NativeLong(10000);							    //目标位置
        rtn = NMC_MtMovePtpRel(axisHandle, acc, dec, startVel, endVel, maxVel, smoothCoef, tgtPos);
        checkError(rtn,"运动出错");

    }
}
