package com.example;

import static com.example.mtnlib.mtn_lib20.*;
import static com.example.mtnlib.mtn_lib20_ext.*;
import static com.example.mtnlib.mtn_lib20_oem.*;
import static com.example.mtnlib.mtn_lib20_err.*;
import com.example.mtnlib.mtn_lib20_com.*;
import com.sun.jna.ptr.*;
import com.sun.jna.*;


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 devNum = 0;                       // 设备序号，从0开始
            short axisNum = 0;                      // 轴号从0开始
            PHAND pDevHandle = new PHAND();         // 设备句柄
            PHAND pAxisHandle = new PHAND();        // 轴句柄
            
            // 打开控制器
            short rtn = NMC_DevOpen(devNum, pDevHandle);
            checkError(rtn,"打开控制器出错");
            System.out.println("打开设备 - 成功");
            HAND devHandle = pDevHandle.getHandle();

            //打开轴,并获得轴句柄
            rtn = NMC_MtOpen(devHandle, axisNum, pAxisHandle);
            checkError(rtn,"打开轴出错");
            System.out.printf("打开轴%d - 成功\n", axisNum);

            //打开轴使能(根据实际情况使用)
            HAND axisHandle = pAxisHandle.getHandle();
            rtn = NMC_MtSetSvOn(axisHandle);
            checkError(rtn,"轴使能出错");
            System.out.printf("轴%d伺服使能 - 成功\n", axisNum);

        NativeLongByReference diVal = new NativeLongByReference();
        NativeLongByReference diValEx = new NativeLongByReference();
        NativeLongByReference doVal = new NativeLongByReference();
        NativeLongByReference doValEx = new NativeLongByReference();
        ShortByReference di0 = new ShortByReference((short) 0);
        ShortByReference di64 = new ShortByReference((short) 0);
        IntByReference axisIO = new IntByReference();
        boolean IsExmoNeed = false;
        //如果需要读取扩展模块
        if (IsExmoNeed == true) {
            //设备ID需要从2开始，多个扩展模块，依次增加，拨码开关也要拨码成对应ID
            rtn = NMC_IOModuleSetEn(devHandle, (byte) 2, (short) 1);
            checkError(rtn,"读取扩展模块出错");
        }
        rtn = NMC_GetDIGroup(devHandle, diVal, (short) 0);	 //读取全部本地通用数字量输入
        checkError(rtn,"读取全部本地通用数字量输入出错");
        rtn = NMC_GetDIBit(devHandle, (short) 0, di0);       //按位读取(单个读取),这里示范读取第0个输入
        checkError(rtn,"读取DI0位出错");
        rtn = NMC_GetDIGroup(devHandle, diValEx, (short) 2);        //全部读取扩展模，这里读取一个模块的
        checkError(rtn,"全部读取扩展模出错");
        rtn = NMC_GetDIBit(devHandle, (short) 64, di64);	//按位读取(单个读取)，读取扩展模块第0个输入
        checkError(rtn,"按位读取(单个读取)出错");
        rtn = NMC_GetDOGroup(devHandle, doVal, (short) 0);	        //读取本地全部通用数字量输出
        checkError(rtn,"读取本地全部通用数字量输出出错");
        rtn = NMC_GetDOGroup(devHandle, doValEx, (short) 2);//读取扩展模块
        checkError(rtn,"读取扩展模块出错");
        rtn = NMC_SetDOGroup(devHandle, new NativeLong(0), (short) 0);		//设置通用数字量全部输出
        checkError(rtn,"设置通用数字量全部输出出错");
        rtn = NMC_SetDOGroup(devHandle, new NativeLong(0xFFFD), (short) 0);	//设置通用数字量第1个输出
        checkError(rtn,"设置通用数字量第1个输出出错");
        rtn = NMC_SetDOBit(devHandle, (short) 0, (short) 0);		//设置通用数字量第0个输出,按位输出
        checkError(rtn,"设置通用数字量第0个输出出错");
        rtn = NMC_MtGetMotionIO(axisHandle, axisIO);//专用IO，如限位原点的电平读取
        if ((axisIO.getValue() & BIT_AXMTIO_LMTN) != 0) {}			//负限位为高电平
        if ((axisIO.getValue() & BIT_AXMTIO_LMTP) != 0) {}			//正限位为高电平
        if ((axisIO.getValue() & BIT_AXMTIO_HOME) != 0) {}			//原点为高电平
        rtn = NMC_SetDacMode(devHandle, (short) 0, (short) 4);		//模拟量设置，默认，+-10V，此函数可省略
        checkError(rtn,"模拟量设置出错");
        rtn = NMC_SetDac(devHandle, (short) 0, (short) 32767);		//模拟量设置输出满值
        checkError(rtn,"模拟量设置输出满值出错");
        ShortByReference dac = new ShortByReference();
        rtn = NMC_GetDac(devHandle, (short) 0, dac);		//读取模拟输出
        checkError(rtn,"读取模拟输出出错");
        ShortByReference adc = new ShortByReference();
        rtn = NMC_SetAdcMode(devHandle, (short) 0, (short) 4, (short) 0);	//设置ADC，默认，+-10V，此函数可省略
        checkError(rtn,"设置ADC出错");
        rtn = NMC_GetAdc(devHandle, (short) 0, adc);		// 读取ADC
        checkError(rtn,"读取ADC出错");
    }
}

