0

我正在尝试将 JNA 与来自 SiLabs 的 USBXPRESS 库(siusbxp.dll)一起使用,虽然基本功能工作正常,但 SI_GetDeviceProductString 函数存在问题。

public class usbxpress 
{

public interface SiUSBXp extends StdCallLibrary 
{
SiUSBXp INSTANCE = (SiUSBXp) Native.loadLibrary("SiUSBXp", SiUSBXp.class);

byte SI_GetNumDevices (IntByReference lpdwNumDevices);
byte SI_GetProductString( int dwDeviceNum, byte[] lpvDeviceString, int dwFlags);
byte SI_Open (int dwDevice, HANDLEByReference cyHandle);
byte SI_GetPartNumber (HANDLE cyHandle, ByteByReference lpbPartNum);        
byte SI_GetDeviceProductString (HANDLE cyHandle, PointerByReference lpProduct, ByteByReference lpbLength, int bConvertToASCII);
//byte SI_GetDeviceProductString (HANDLE cyHandle, LPVOID lpProduct, LPBYTE lpbLength, BOOL bConvertToASCII = TRUE); //original c function
}

public static void main(String[] args) 
{
//checking number of connected devices  
IntByReference lpdwNumDevices = new IntByReference();
SiUSBXp.INSTANCE.SI_GetNumDevices (lpdwNumDevices);        
System.out.println(lpdwNumDevices.getValue());

//opening the device
HANDLEByReference dev_handle_ref = new HANDLEByReference();
byte status = SiUSBXp.INSTANCE.SI_Open(0, dev_handle_ref);
System.out.printf("Status %d\n", status);

HANDLE device_handle = dev_handle_ref.getValue();

//checking part number
ByteByReference lpbPartNum = new ByteByReference();
SiUSBXp.INSTANCE.SI_GetPartNumber(device_handle, lpbPartNum);
System.out.printf("Part number is CP210%d\n", lpbPartNum.getValue());      

//checking product string - does not work
PointerByReference lpProduct = new PointerByReference();
ByteByReference lpbLength = new ByteByReference();
SiUSBXp.INSTANCE.SI_GetDeviceProductString(device_handle, lpProduct, lpbLength, 1);
}}

当我尝试运行它时,出现以下错误:

> Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'SI_GetDeviceProductString': at com.sun.jna.Function.<init>(Function.java:179) 
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:391) 
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:371) 
at com.sun.jna.Library$Handler.invoke(Library.java:205) 
at $Proxy0.SI_GetDeviceProductString(Unknown Source)

感觉就像是 C 函数的默认参数的问题。我尝试使用 int 作为参数并尝试省略它,但这些都没有帮助。我什至没有达到 SI_Write (HANDLE cyHandle, LPVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpdwBytesWritten, OVERLAPPED* o = NULL); 函数,这有望导致更多问题:)

谁能建议我如何处理 JNA 中的默认函数参数?

更新: SI_Write 函数以这种方式工作正常:

byte SI_Write (HANDLE cyHandle, PointerByReference lpBuffer, int dwBytesToWrite, IntByReference lpdwBytesWritten, Pointer o);
...
SiUSBXp.INSTANCE.SI_Write (device_handle, lpBuffer, message.length, lpdwBytesWritten, null);

所以问题是由其他原因引起的,但它仍然存在。

4

1 回答 1

0

我已经设法通过使用旧版本的 SiUSBXp 库解决了这个问题,该库是在互联网上找到的。从 SiLabs 网站下载的较新版本表现得很奇怪 - 有时 SI_GetDeviceProductString 函数在 Dependency Walker 中可见,有时则不可见,而较旧的版本很好。

于 2013-02-05T08:49:28.367 回答