Java之关于使用com.sun.jna的简单实例


import com.sun.jna.Library;

public interface TemperatureLibrary  extends Library{

	int GetTempCount();
	int GetTemp1();
	int GetTemp2();
	int GetTemp3();
	int GetTemp4();
	int GetTemp5();
	int GetTemp6();
	int GetTemp7();
	
}

import com.sun.jna.Native;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TemperatureUtility {

    public static final TemperatureLibrary INSTANCE;

    static {
//	    System.setProperty("jna.encoding", "GBK");
//	    System.setProperty("jna.library.path", System.getProperty("user.dir") + "/dll");
//	    System.setProperty("jna.debug_load", "true");
        INSTANCE = Native.load("lib\\Temperature.dll", TemperatureLibrary.class);
        refreshIfCanGetTemperature();
        log.info("加载温度动态库完成。");
    }

    private TemperatureUtility() {
        //
    }

    private static void refreshIfCanGetTemperature() {
        long time = System.currentTimeMillis();
        while (true) {
            float i = INSTANCE.GetTemp1() / 10f;
            if (i != 3276.7f) {
                log.info("可以开始获取温度。加载时间ms:{}", System.currentTimeMillis() - time);
                return;
            } else {
                try {
                    INSTANCE.GetTempCount();
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    log.info("error at init getTemp1:{}", e);
                    Thread.currentThread().interrupt();
                }
            }
        }
    }

    public static int getTempCount() {
        return INSTANCE.GetTempCount();
    }

    public static float getTemp1() {
        return INSTANCE.GetTemp1() / 10f;
    }


}

你可能感兴趣的:(java,开发语言)