我正在尝试将 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);
所以问题是由其他原因引起的,但它仍然存在。