#
zhou zhou
昨天 6bbfef82d3019f01bd88e84da5bb83311ffdf844
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.rfid.uhf288;
 
import java.io.File;
 
/**
 * RFID UHF288设备JNI接口
 * 对应DLL: com_rfid_uhf288_Device.dll
 * 
 * 参考demo项目的加载方式:在static代码块中加载DLL
 * 注意:DLL可能不支持多线程并发访问,需要同步保护
 */
public class Device {
 
    // DLL加载状态标志
    private static volatile boolean libraryLoaded = false;
    // DLL加载锁,确保只加载一次
    private static final Object loadLock = new Object();
 
    /**
     * 静态代码块:加载DLL库
     * 参考demo项目的方式,优先使用绝对路径加载,如果失败则尝试从系统PATH加载
     */
    static {
        loadLibrary();
    }
 
    /**
     * 加载DLL库
     * 优先使用绝对路径直接加载,如果失败则尝试从系统PATH加载(参考demo项目的方式)
     * 使用同步锁确保线程安全,避免多线程同时加载导致冲突
     */
    private static void loadLibrary() {
        // 如果已经加载,直接返回
        if (libraryLoaded) {
            return;
        }
        
        // 同步加载,确保只加载一次
        synchronized (loadLock) {
            // 双重检查,避免重复加载
            if (libraryLoaded) {
                return;
            }
            
            String mainDllName = "com_rfid_uhf288_Device.dll";
            String depDllName = "UHFReader288.dll"; // 依赖的DLL
 
            // 方法1: 使用绝对路径直接加载DLL
            try {
            // 使用真实路径加载DLL
            String projectBaseDir = "D:\\work\\work-zy\\zy-wcs";
            String mainDllPath = projectBaseDir + "\\src\\main\\resources\\lib\\" + mainDllName;
            String depDllPath = projectBaseDir + "\\src\\main\\resources\\lib\\" + depDllName;
            
            File mainDllFile = new File(mainDllPath);
            File depDllFile = new File(depDllPath);
            
            if (mainDllFile.exists() && mainDllFile.canRead()) {
                // 先尝试加载依赖DLL(如果存在)
                if (depDllFile.exists() && depDllFile.canRead()) {
                    try {
                        System.load(depDllPath);
                        System.out.println("成功加载依赖DLL: " + depDllName + " (路径: " + depDllPath + ")");
                    } catch (UnsatisfiedLinkError e) {
                        // 依赖DLL可能已经加载过,或者加载失败
                        // 如果错误信息不是"已加载",则打印警告
                        if (e.getMessage() == null || !e.getMessage().contains("already loaded")) {
                            System.err.println("警告: 依赖DLL加载失败(继续尝试加载主DLL): " + depDllName);
                            System.err.println("错误信息: " + e.getMessage());
                        }
                    }
                }
                
                // 加载主DLL
                System.load(mainDllPath);
                System.out.println("成功从绝对路径加载DLL: " + mainDllName + " (路径: " + mainDllPath + ")");
                System.out.println("DLL文件大小: " + mainDllFile.length() + " 字节");
                libraryLoaded = true;
                return;
            } else {
                System.err.println("DLL文件不存在或不可读: " + mainDllPath);
            }
        } catch (UnsatisfiedLinkError e) {
            // 如果是依赖库错误,提供更详细的错误信息
            if (e.getMessage() != null && e.getMessage().contains("dependent libraries")) {
                System.err.println("DLL依赖库加载失败: " + e.getMessage());
                System.err.println("请确保所有依赖DLL文件都在同一目录中");
            }
            System.err.println("从绝对路径加载DLL失败: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("从绝对路径加载DLL失败: " + e.getMessage());
            e.printStackTrace();
        }
 
        /* 方法2(已注释): 尝试从resources目录提取到临时目录加载
        try {
            // 创建专门的临时目录来存放所有DLL(只创建一次)
            if (tempLibDir == null) {
                tempLibDir = createTempLibDirectory();
            }
            
            if (tempLibDir != null && tempLibDir.exists()) {
                // 先提取所有DLL到同一目录
                File depDll = extractDllToDir("/lib/" + depDllName, depDllName, tempLibDir);
                File mainDll = extractDllToDir("/lib/" + mainDllName, mainDllName, tempLibDir);
                
                if (mainDll != null && mainDll.exists()) {
                    // 先尝试加载依赖DLL(如果存在)
                    if (depDll != null && depDll.exists()) {
                        try {
                            System.load(depDll.getAbsolutePath());
                            System.out.println("成功加载依赖DLL: " + depDllName + " (路径: " + depDll.getAbsolutePath() + ")");
                        } catch (UnsatisfiedLinkError e) {
                            if (e.getMessage() == null || !e.getMessage().contains("already loaded")) {
                                System.err.println("警告: 依赖DLL加载失败(继续尝试加载主DLL): " + depDllName);
                                System.err.println("错误信息: " + e.getMessage());
                            }
                        }
                    }
                    
                    System.load(mainDll.getAbsolutePath());
                    System.out.println("成功从resources目录加载DLL: " + mainDllName + " (路径: " + mainDll.getAbsolutePath() + ")");
                    return;
                }
            }
        } catch (Exception e) {
            System.err.println("从resources目录加载DLL失败: " + e.getMessage());
            e.printStackTrace();
        }
        */
 
            // 方法3: 尝试从系统PATH或java.library.path加载(参考demo项目的方式)
            try {
                System.loadLibrary("com_rfid_uhf288_Device");
                System.out.println("成功从系统PATH加载DLL: " + mainDllName);
                System.out.println("注意:如果后续native方法调用失败,请检查DLL版本是否匹配");
                libraryLoaded = true;
                return;
            } catch (UnsatisfiedLinkError e) {
                System.err.println("无法从系统PATH加载RFID设备DLL库: " + mainDllName);
                System.err.println("错误信息: " + e.getMessage());
                System.err.println("请确保DLL文件在系统PATH或项目lib目录中");
                e.printStackTrace();
            }
            
            // 如果所有方法都失败,记录错误
            if (!libraryLoaded) {
                System.err.println("========================================");
                System.err.println("RFID设备DLL加载失败!");
                System.err.println("请检查:");
                System.err.println("1. DLL文件是否存在: D:\\work\\work-zy\\zy-wcs\\src\\main\\resources\\lib\\com_rfid_uhf288_Device.dll");
                System.err.println("2. DLL文件是否完整");
                System.err.println("3. DLL文件是否在系统PATH中");
                System.err.println("4. 从demo项目中复制正确的DLL文件");
                System.err.println("========================================");
            }
        }
    }
 
    /**
     * @param args
     */
    public native int OpenComPort(int port,byte[]comAddr,byte baud,int[]PortHandle);
    public native int CloseSpecComPort(int PortHandle);
    public native int OpenNetPort(int Port,String IPaddr,byte[]comAddr,int[] PortHandle);
    public native int CloseNetPort(int PortHandle);
    public native int GetReaderInformation(byte[]comAddr,byte[]versionInfo,byte[]readerType,byte[]trType,byte[]dmaxfre,
                                           byte[]dminfre,byte[]powerdBm,byte[]InventoryScanTime,byte[]Ant,byte[]BeepEn,
                                           byte[]OutputRep,byte[]CheckAnt,int PortHandle);
 
    public native int Inventory_G2(byte[]comAddr,byte QValue,byte Session,byte MaskMem,byte[]MaskAdr,byte MaskLen,
                                   byte[]MaskData,byte MaskFlag,byte AdrTID,byte LenTID,byte TIDFlag,byte Target,
                                   byte InAnt,byte Scantime,byte FastFlag,byte[]pEPCList,byte[] Ant,int[]Totallen,
                                   int[]CardNum,int PortHandle);
 
    public native int InventoryMix_G2(byte[]comAddr,byte QValue,byte Session,byte MaskMem,byte[]MaskAdr,byte MaskLen,
                                      byte[]MaskData,byte MaskFlag,byte ReadMem,byte[]ReadAdr,byte ReadLen,byte[]Psd,
                                      byte Target,byte InAnt,byte Scantime,byte FastFlag,byte[]pEPCList,byte[] Ant,
                                      int[]Totallen,int[]CardNum,int PortHandle);
 
    public native int ReadData_G2(byte[]comAddr,byte[] EPC,byte ENum,byte Mem,byte WordPtr,byte Num,byte[]Password,
                                  byte MaskMem,byte[]MaskAdr,byte MaskLen,byte[]MaskData,byte[]Data,int[]Errorcode,
                                  int PortHandle);
 
    public native int WriteData_G2(byte[]comAddr,byte[] EPC,byte WNum,byte ENum,byte Mem,byte WordPtr,byte[] Wdt,
                                   byte[]Password,byte MaskMem,byte[]MaskAdr,byte MaskLen,byte[]MaskData,int[]Errorcode,
                                   int PortHandle);
 
    public native int WriteEPC_G2(byte[]comAddr,byte[] Password,byte[] EPC,byte ENum,int[]Errorcode,int PortHandle);
 
    public native int KillTag_G2(byte[]comAddr,byte[] EPC,byte ENum,byte[] Killpwd,byte MaskMem,byte[]MaskAdr,byte MaskLen,
                                 byte[]MaskData,int[]Errorcode,int PortHandle);
    public native int Lock_G2(byte[]comAddr,byte[] EPC,byte ENum,byte selectid,byte setprotect,byte[] Password,byte MaskMem,
                              byte[]MaskAdr,byte MaskLen, byte[]MaskData,int[]Errorcode,int PortHandle);
    public native int BlockErase_G2(byte[]comAddr,byte[] EPC,byte ENum,byte Mem,byte WordPtr,byte Num,byte[] Password,
                                    byte MaskMem,byte[]MaskAdr,byte MaskLen, byte[]MaskData,int[]Errorcode,int PortHandle);
    public native int SetRegion(byte[]comAddr, byte dmaxfre,byte dminfre,int PortHandle);
    public native int SetAddress(byte[]comAddr, byte ComAdrData,int PortHandle);
    public native int SetInventoryScanTime(byte[]comAddr, byte ScanTime,int PortHandle);
    public native int SetBaudRate(byte[]comAddr, byte baud,int PortHandle);
    public native int SetRfPower(byte[]comAddr, byte PowerDbm,int PortHandle);
    public native int SetWorkMode(byte[]comAddr, byte Read_mode,int PortHandle);
    public native int GetSystemParameter(byte[]comAddr,byte[]Read_mode,byte[]Accuracy,byte[]RepCondition,byte[]RepPauseTime,
                                         byte[]ReadPauseTim,byte[]TagProtocol,byte[]MaskMem,byte[]MaskAdr,byte[]MaskLen,
                                         byte[]MaskData, byte[]TriggerTime,byte[]AdrTID,byte[]LenTID,int PortHandle);
    public native int SetAntennaMultiplexing(byte[]comAddr, byte Ant,int PortHandle);
    public native int SetAntenna(byte[]comAddr, byte SetOnce,byte AntCfg1,byte AntCfg2,int PortHandle);
    public native int WriteRfPower(byte[]comAddr, byte PowerDbm,int PortHandle);
    public native int ReadRfPower(byte[]comAddr, byte[] PowerDbm,int PortHandle);
    public native int RetryTimes(byte[]comAddr, byte[] Times,int PortHandle);
    public native int SetReadMode(byte[]comAddr, byte ReadMode,int PortHandle);
    public native int SetCheckAnt(byte[]comAddr, byte CheckAnt,int PortHandle);
    public native int GetSeriaNo(byte[]comAddr, byte[] SeriaNo,int PortHandle);
    public native int SetBeepNotification(byte[]comAddr, byte BeepEn,int PortHandle);
    public native int SetReal_timeClock(byte[]comAddr, byte[] paramer,int PortHandle);
    public native int GetTime(byte[]comAddr, byte[] paramer,int PortHandle);
    public native int SetDRM(byte[]comAddr, byte DRM,int PortHandle);
    public native int GetDRM(byte[]comAddr, byte[] DRM,int PortHandle);
    public native int GetReaderTemperature(byte[]comAddr, byte[] PlusMinus,byte[]Temperature,int PortHandle);
    public native int MeasureReturnLoss(byte[]comAddr, byte[] TestFreq,byte Ant,byte[]ReturnLoss,int PortHandle);
    public native int SetGPIO(byte[] comAddr, byte OutputPin, int PortHandle);
    public native int GetGPIOStatus(byte[] ComAdr, byte[] OutputPin, int PortHandle);
    public native int ReadActiveModeData(byte[] ScanModeData, int[] ValidDatalength, int PortHandle);
    public native int SetRelay(byte[] comAddr, byte RelayTime, int PortHandle);
    public native int SwitchProtocol(byte[] comAddr, byte[] protocol, int PortHandle);
 
    public native int Inventory_JB(byte[]comAddr,byte Algo,byte MaskMem,byte[]MaskAdr,byte MaskLen,
                                   byte[]MaskData,byte MaskFlag, byte InAnt,byte Scantime,byte FastFlag,byte[]pEPCList,byte[] Ant,int[]Totallen,
                                   int[]CardNum,int PortHandle);
 
    public native int InventoryMix_JB(byte[]comAddr,byte Algo,byte MaskMem,byte[]MaskAdr,byte MaskLen,
                                      byte[]MaskData,byte MaskFlag,byte ReadMem,byte[]ReadAdr,byte ReadLen,byte[]Psd,
                                      byte InAnt,byte Scantime,byte FastFlag,byte[]pEPCList,byte[] Ant,
                                      int[]Totallen,int[]CardNum,int PortHandle);
 
    public native int ReadData_JB(byte[]comAddr,byte[] TagID,byte TNum,byte Mem,int WordPtr,byte WordNum,byte[]Password,
                                  byte MaskMem,byte[]MaskAdr,byte MaskLen,byte[]MaskData,byte[]Data,int[]Errorcode,
                                  int PortHandle);
 
    public native int WriteData_JB(byte[]comAddr,byte[] TagID,byte WNum,byte TNum,byte Mem,int WordPtr,byte[] Wdt,
                                   byte[]Password,byte MaskMem,byte[]MaskAdr,byte MaskLen,byte[]MaskData,int[]Errorcode,
                                   int PortHandle);
    public native int KillTag_JB(byte[]comAddr,byte[] TagID,byte TNum,byte[] Killpwd,byte MaskMem,byte[]MaskAdr,byte MaskLen,
                                 byte[]MaskData,int[]Errorcode,int PortHandle);
    public native int Lock_JB(byte[]comAddr,byte[] EPC,byte TNum,byte LockMem,byte Cfg,byte Action,byte[] Password,byte MaskMem,
                              byte[]MaskAdr,byte MaskLen, byte[]MaskData,int[]Errorcode,int PortHandle);
    public native int BlockErase_JB(byte[]comAddr,byte[] EPC,byte TNum,byte Mem,int WordPtr,int Num,byte[] Password,
                                    byte MaskMem,byte[]MaskAdr,byte MaskLen, byte[]MaskData,int[]Errorcode,int PortHandle);
 
 
    public native int SetCfgParameter(byte[] comAddr, byte opt, byte cfgNum, byte[]data,int len, int FrmHandle);
 
    public native int GetCfgParameter (byte[] comAddr, byte cfgNum,byte[] data, int[] len, int FrmHandle);
 
    public native int SetProfile(byte[] comAddr, byte[] Profile, int FrmHandle);
    public native int SetExtProfile(byte[] comAddr,byte Opt, int[] Profile, int FrmHandle);
 
    public native int GetEx10Version(byte[] comAddr,byte[] Version, byte[] ex10Tpye, int FrmHandle);
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
    }
 
}