//Compile demo ==> javac  -cp jna-5.2.0.jar HelloWorld.java
//windows ->  jna calling msvcrt.dll ==> java -cp jna-5.2.0.jar;. HelloWorld
//Linux ->  jna calling libc.so     ==>java -cp jna-5.2.0.jar:. HelloWorld

//import com.sun.jna.Library;
import com.sun.jna.*;
import com.sun.jna.Native;
import com.sun.jna.Platform;

/** Simple example of native library declaration and usage. */
public class Getlist {
   
    public static char COM1 = 1;
    //public static int COM1 = 1;
    //public static int Data8Bit = 8;
    //public static int OneStopBit = 1;
         
    public static char    Data5Bit =5;
    public static char    Data6Bit =6;
    public static char    Data7Bit =7;
    public static char    Data8Bit =8;

    public static char    NonParity = 0;
    public static char    OddParity = 1;
    public static char    EvenParity= 2;

    public static char    OneStopBit = 0;
    public static char    One5StopBit= 1;
    public static char    TwoStopBit = 2;
    
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
            (Platform.isWindows() ? "libi8k" : "c"), CLibrary.class);
        //void printf(String format, Object... args);    
        int Open_Slot(int slot);
        void Close_Slot(int slot);
        int Open_Com(char port, long baudrate, char cData, char cParity, char cStop);	
        int Close_Com(int port);
        void ChangeToSlot(int slot);
        int DigitalOut_87K(int dwBuf[], float fBuf[], char szSend[], char szReceive[]);        
    }

    public static void main(String[] args) {
        //CLibrary.INSTANCE.printf("Hello, World\n");
                        
        char[] szSend = new char[50];    //char szSend[80];
        char[] szReceive = new char[50]; //char szReceive[80];
        float[] fBuf = new float[12];    //float fBuf[12];
        int[] i87kBuf = new int[12];   //int i87kBuf[12];
        //SzSend dwBuf = new SzSend();     //int dwBuf[12];
        //SzSend[] i87kBuf = (SzSend[]) dwBuf.toArray(12);        
        
        CLibrary.INSTANCE.Open_Slot(0);        
        CLibrary.INSTANCE.Open_Com(COM1, 115200, Data8Bit, NonParity, OneStopBit);
        CLibrary.INSTANCE.ChangeToSlot(1);
        
        
        i87kBuf[0] = 1;				   // COM Port
	      i87kBuf[1] = 00;				 // Address
	      i87kBuf[2] = 0x87054;		 // ID
	      i87kBuf[3] = 0;				   // CheckSum disable
	      i87kBuf[4] = 10;				 // TimeOut , 10 msecond
	      i87kBuf[5] = 0xf;			   // digital output
	      i87kBuf[6] = 1;				   // string debug
	    	      
        CLibrary.INSTANCE.DigitalOut_87K(i87kBuf, fBuf, szSend, szReceive);
        //System.out.println("Digital Out :" + i + "  Digital In : "+ diValue);
        System.out.println("szSend = "+ szSend +" szReceive = "+szReceive);
        CLibrary.INSTANCE.Close_Slot(1);        
        CLibrary.INSTANCE.Close_Com(1);        
    }
}
