1.1、文件用途
在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
编译成功!
运行成功!
2.1、先安装 libusb 相关类库
sudo apt-get install libusb*
2.2、查看系统默认类路径
vim /etc/ld.so.conf
显示:
include /etc/ld.so.conf.d/*.conf
2.3、拷贝文件cd /etc/ld.so.conf.d/
ls
显示:
i386-linux-gnu_GL.conf libc.conf x86_64-linux-gnu_GL.conf i686-linux-gnu.conf oracle.conf
vim libc.conf显示:
# libc default configuration /usr/local/lib
说明系统默认类库路径有一条 /usr/local/lib
拷贝 librf.so 到 /usr/local/lib,拷贝librf.h 到 /usr/local/include
2.4、更新系统类库
#ldconfig
2.5、在Eclipse cpp中创建一个C工程
拷贝 RfDemo_linux.c 到源代码文件夹 src;
拷贝静态链接库文件 D8U.a、libusb-1.0.a 到工程根目录下;
2.6、配置Eclipse cpp 开发环境
2.6.1、使用C99编译标准:右键点击项目->Properties->C/C++ Build -> Settings -> Tool Settings -> GCC C Complier -> Miscellaneous
在 "Other flags" 中追加 -std=c99;2.6.2、添加使用的动态类库:右键点击项目->Properties->C/C++ Build -> Settings -> Tool Settings -> GCC C linker -> Libraries
在 "Libraries" 中添加 rf、rt、usb、pthread2.6.3、添加使用的静态类库:右键点击项目->Properties->C/C++ Build -> Settings -> Tool Settings -> GCC C linker -> Miscellaneous
在 "Other objects" 中添加工程根目录下的 D8U.a、libusb-1.0.a
2.7、完成
编译成功!
运行成功!
PS1:某些读卡器的Linux驱动测试失败时,可以试试这个读卡器升级包
PS2:开发用的API帮助文档下载