在D3的Java檔案夾下有檔案夾兩個:classpackage、example,檔案一個:readme.txt。
classpackage中的 javaRD800.dll 是一個面向Java JNI 的windows動態連結程式庫。
classpackage中的 javaRD800.java 是一個面向Java的 JNI 介面。
example中的javaRD800Demo.java 是一個使用Java編寫的 讀寫卡器測試程式。
調用的順序如下:
javaRD800Demo.java---> javaRD800.java---> javaRD800.dll
javaRD800.DLL 檔案下載
/* * @(#)JavaRD800.java 1.0 03/11/27 * * You can modify the template of this file in the * directory ..\JCreator\Templates\Template_1\Project_Name.java * * You can also create your own project template by making a new * folder in the directory ..\JCreator\Template\. Use the other * templates as examples. * */public class JavaRD800{public JavaRD800(){}static{System.loadLibrary("javaRD800");}public native int dc_init(int lPort,int lBaud);public native short dc_exit(int lDevice);public native short dc_card(int lDevice,short iMode,int[] pSnr);public native short dc_authentication(int lDevice,short iMode,short iSecNr);public native short dc_halt(int lDevice);public native short dc_read(int lDevice,short iAdr,char[] pData);public native short dc_write(int lDevice,short iAdr,char[] pData);public native short dc_increment(int lDevice,short iAdr,int lValue);public native short dc_decrement(int lDevice,short iAdr,int lValue);public native short dc_initval(int lDevice,short iAdr,int lValue);public native short dc_readval(int lDevice,short iAdr,int[] pValue);public native short dc_transfer(int lDevice,short iAdr);public native short dc_restore(int lDevice,short iAdr);public native short dc_load_key(int lDevice,short iMode,short iSecNr,char[] pKey);public native short dc_beep(int lDevice,short iMsec);public native short dc_high_disp(int lDevice,short iOffset,short iDispLen,char[] pDispStr);public native short dc_request(int lDevice,short iMode,int[] pTagType);public native short dc_anticoll(int lDevice,short iBcnt,int[] pSnr);public native short dc_select(int lDevice,int lSnr,short[] pSize);public native short dc_gettime(int lDevice,char[] pTimeStr);public native short dc_gettimehex(int lDevice,char[] pTimeStr);public native short dc_settime(int lDevice,char[] pTimeStr);public native short dc_settimehex(int lDevice,char[] pTimeStr);public native short dc_setbright(int lDevice,short iBright);public native short dc_ctl_mode(int lDevice,short iMode);public native short dc_disp_mode(int lDevice,short iMode);public native short dc_cpureset(int lDevice,short[] pLen,char[] pData);public native short dc_cpuapdu(int lDevice,short iLen,char[] pSData,short[] pLen,char[] pRData);public native short dc_cpuapdusource(int lDevice,short iLen,char[] pSData,short[] pLen,char[] pRData);public native short dc_cpudown(int lDevice);public native short dc_swr_eeprom(int lDevice,int lOffset,int lLen,char[] pBuffer);public native short dc_srd_eeprom(int lDevice,int lOffset,int lLen,char[] pBuffer);public native short dc_disp_str(int lDevice,char[] pDispStr);}
class JavaRD800Demo{public static void main(String[] args){short status;int lDevice = 0;int[] pSnr = new int[1];char[] pSBuffer = new char[16];char[] pRBuffer = new char[16];JavaRD800 rd = new JavaRD800();lDevice = rd.dc_init(100,115200);if(lDevice <= 0){System.out.print("dc_init error!\n");return;}System.out.print("dc_init ok!\n");rd.dc_beep(lDevice,(short)10); status = rd.dc_card(lDevice,(short)0,pSnr);if(status != 0){System.out.print("dc_card error!\n");rd.dc_exit(lDevice);return;}System.out.print("dc_card ok!\n");System.out.println(pSnr[0]);pSBuffer[0] = 0xFF;pSBuffer[1] = 0xFF;pSBuffer[2] = 0xFF;pSBuffer[3] = 0xFF;pSBuffer[4] = 0xFF;pSBuffer[5] = 0xFF;pSBuffer[6] = 0x00;status = rd.dc_load_key(lDevice,(short)0,(short)0,pSBuffer);if(status != 0){System.out.print("dc_load_key error!\n");rd.dc_exit(lDevice);return;}System.out.print("dc_load_key ok!\n");status = rd.dc_authentication(lDevice,(short)0,(short)0);if(status != 0){System.out.print("dc_authentication error!\n");rd.dc_exit(lDevice);return;}System.out.print("dc_authentication ok!\n");for(int i=0;i<16;i++){pSBuffer[i] = 'M';}status = rd.dc_write(lDevice,(short)2,pSBuffer);if(status != 0){System.out.print("dc_write error!\n");rd.dc_exit(lDevice);return;}System.out.print("dc_write ok!\n");status = rd.dc_read(lDevice,(short)2,pRBuffer);if(status != 0){System.out.print("dc_read error!\n");rd.dc_exit(lDevice);return;}System.out.print("dc_read ok!\n");System.out.println(pRBuffer);status = rd.dc_exit(lDevice);if(status != 0){System.out.print("dc_exit error!\n");rd.dc_exit(lDevice);return;}System.out.print("dc_exit ok!\n");}}
1.2、動手測試
在 Eclipse 中建立一個 Java 工程,javaRD800Demo.java、javaRD800.java 放在 src 檔案夾內,javaRD800.dll 放在工程根目錄下。
編譯成功!
運行出錯!提示找不到依賴的類庫。
1.3、分析、解決問題
估計是缺少dll檔案,下載 dll檔案依賴查看器 發現缺少 dcrf32.DLL 檔案。
這個檔案 Java 檔案夾裡面沒有這個檔案,可以在 D3--->win32-dll目錄下找到此檔案,拷貝此檔案到工程根目錄下。
調用的順序如下:
javaRD800Demo.java---> javaRD800.java---> javaRD800.dll---> dcrf32.dll