1

我有这个本机功能,当我将设备连接到我的系统时,我在 JNA 中得到空值

CP210x_GetProductString( DWORD DeviceNum,LPVOID DeviceString,DWORD Options)
  1. DeviceNum— 需要产品描述字符串、序列号或完整路径的设备的索引。
  2. DeviceStringCP210x_DEVICE_STRING—返回以 NULL 结尾的序列号、设备描述或完整路径字符串的类型变量。
  3. Options— 确定是否DeviceString包含产品描述、序列号或完整路径字符串的标志

JNA 代码:

public class Helloworld {

    public interface CLibrary extends Library{
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
            (Platform.isWindows() ? "CP210xManufacturing.dll" : "c"),
            CLibrary.class);

        int CP210x_GetProductString(int dn,String [] ds,int op);
    }

    public static void main(String[] args) {
        int dn=0;
        String dsc = new String[100];
        if(CLibrary.INSTANCE.CP210x_GetProductString(dn, dsc,
               CP210x.CP210x_RETURN_SERIAL_NUMBER) == CP210x.CP210x_SUCCESS){
        {
            for(int i=0;i<dsc.length;i++)
                System.out.print(dsc[i]);
            }

        }
    }
}
4

1 回答 1

0

不知道这是否可行,但从CP210x_GetProductString 的描述来看,我认为您必须将长度为 256 的 byte[] 传递给函数(用字符串填充的缓冲区)。

 int CP210x_GetProductString(int dn,byte[] ds,int op);

 //use it like this
 byte[] rawString = new byte[256];
 int dn = ...;
 int op = ...;
 CP210x_GetProductString(dn,rawString,op);
 //You might have to choose a different Charset here
 //since I don't know what encoding the string uses I used the default
 String product = new String(rawString,Charset.defaultCharset());
于 2011-01-08T20:11:54.813 回答