错误的输出来自JNA中的结构字段值

问题描述 投票:0回答:1

我正在尝试通过Java代码使用Vector XL Driver Library(vxlapi64.dll)。我创建了一个JNA项目来读取vxlapi64.dll和映射的数据类型,但是输出为该结构的元素生成了错误的值。

C代码结构:

    typedef struct s_xl_channel_config {
          char                name [XL_MAX_LENGTH + 1];
          unsigned char       hwType;                               
          unsigned char       hwIndex;                              
          unsigned char       hwChannel;                           
          unsigned short      transceiverType;                    
          unsigned short      transceiverState;                   
          unsigned short      configError;                          
          unsigned char       channelIndex;                        
          XLuint64            channelMask;                          
          unsigned int        channelCapabilities;                  
          unsigned int        channelBusCapabilities;             


          // Channel          
          unsigned char       isOnBus;                              
          unsigned int        connectedBusType;                  
          XLbusParams         busParams;
          unsigned int        _doNotUse;                            

          unsigned int        driverVersion;            
          unsigned int        interfaceVersion;                     
          unsigned int        raw_data[10];

          unsigned int        serialNumber;
          unsigned int        articleNumber;

          char                transceiverName [XL_MAX_LENGTH +1]

          unsigned int        specialCabFlags;                      
          unsigned int        dominantTimeout;                    
          unsigned char       dominantRecessiveDelay;         
          unsigned char       recessiveDominantDelay;          
          unsigned char       connectionInfo;                      
          unsigned char       currentlyAvailableTimestamps;  
          unsigned short      minimalSupplyVoltage;             
          unsigned short      maximalSupplyVoltage;            
          unsigned int        maximalBaudrate;                     
          unsigned char       fpgaCoreCapabilities;               
          unsigned char       specialDeviceStatus;               
          unsigned short      channelBusActiveCapabilities;   s)
          unsigned short      breakOffset;                          
          unsigned short      delimiterOffset;                    
          unsigned int        reserved[3];
        } XL_CHANNEL_CONFIG; 

Java映射结构:

  public static class XLchannelConfig extends Structure{
      public byte[] name = new byte[XL_MAX_LENGTH + 1];
      public byte hwType;
      public byte hwIndex;
      public byte hwChannel;
      public short transceiverType;
      public short transceiverState;
      public short configError;     
      public byte channelIndex;
      public UnsignedInt channelMask;
      public UnsignedInt channelCapabilities;
      public UnsignedInt channelBusCapabilities;
      public byte isOnBus;
      public UnsignedInt connectedBusType;
      public XLbusParams busParams;
      public UnsignedInt _doNotUse;  
      public UnsignedInt driverVersion;
      public UnsignedInt interfaceVersion;
      public UnsignedInt[] raw_data = new UnsignedInt[10];
      public UnsignedInt serialNumber;
      public UnsignedInt articleNumber;
      public byte[] transceiverName = new byte[XL_MAX_LENGTH +1];
      public UnsignedInt specialCabFlags;
      public UnsignedInt dominantTimeout;
      public byte dominantRecessiveDelay;  
      public byte recessiveDominantDelay;
      public byte connectionInfo;
      public byte currentlyAvailableTimestamps;
      public short minimalSupplyVoltage;
      public short maximalSupplyVoltage;
      public UnsignedInt maximalBaudrate;   
      public byte fpgaCoreCapabilities; 
      public byte specialDeviceStatus;
      public short channelBusActiveCapabilities;
      public short breakOffset;       
      public short delimiterOffset; 
      public UnsignedInt[] reserved = new UnsignedInt[3];

      @Override
      protected List getFieldOrder() {
          return Arrays.asList(
                  "name",
                  "hwType",
                  "hwIndex",
                  "hwChannel",
                  "transceiverType",
                  "transceiverState",
                  "configError",
                  "channelIndex",
                  "channelMask",
                  "channelCapabilities",
                  "channelBusCapabilities",
                  "isOnBus",
                  "connectedBusType",
                  "busParams",
                  "_doNotUse",
                  "driverVersion",
                  "interfaceVersion",
                  "raw_data",
                  "serialNumber",
                  "articleNumber",
                  "transceiverName",
                  "specialCabFlags",
                  "dominantTimeout",
                  "dominantRecessiveDelay",  
                  "recessiveDominantDelay",
                  "connectionInfo",
                  "currentlyAvailableTimestamps",
                  "minimalSupplyVoltage",
                  "maximalSupplyVoltage",
                  "maximalBaudrate",   
                  "fpgaCoreCapabilities",
                  "specialDeviceStatus",
                  "channelBusActiveCapabilities",
                  "breakOffset",       
                  "delimiterOffset",
                  "reserved");
      }
      public XLchannelConfig() {
          super();
          read();
      }
       @Override
          protected void allocateMemory() {
              super.allocateMemory();
          }
      public XLchannelConfig(Pointer pointer) {
          super();
          useMemory(pointer);
          read();
      }

      public static class ByReference extends XLchannelConfig implements Structure.ByReference {
          public ByReference(){
              super();
              read();
          }
          public ByReference(Pointer p) { super(p);
          ensureAllocated();
          useMemory(p);
          byte[] buffer = new byte[size()];
          this.getPointer().read(0, buffer, 0, buffer.length);
          getPointer().write(0, buffer, 0, buffer.length);
          read();}
      };

      public static class ByValue extends XLchannelConfig implements Structure.ByValue {
          public ByValue() {
              super();
              read();
          }
          public ByValue(Pointer p) { super(p);
          useMemory(p);
          read();}
      };
  }

public class UnsignedInt extends IntegerType {
      public UnsignedInt() {
           super(4, false);
      }
  }

我有示例C代码,它读取dll,并且得到正确的值,但是使用JNA,它没有给出正确的输出值。结构映射正确还是我错过了任何事情?

使用c代码的实际输出:

  • 02通道硬件配置-
  • Ch:00,CM:0x001,虚拟通道1,没有出租车!
  • Ch:01,CM:0x002,虚拟通道2,没有出租车!

以上映射的输出:

  • [2通道硬件配置-
  • Ch:01,CM:0x000,虚拟通道1无驾驶室!
  • Ch:00,CM:0x000,虚拟通道2无驾驶室!

    Daniel Widdis如建议的建议,如果我可以将UnsignedInt更改为int并将XLuint64更改为长输出,如下所示>]

  • [2通道硬件配置-

  • Ch:00,CM:0x1000000070000,虚拟通道1无驾驶室!
  • Ch:700,CM:0x1010001000100,al Channel 2

我正在尝试使用Java代码使用Vector XL驱动程序库(vxlapi64.dll)。我创建了一个JNA项目来读取vxlapi64.dll和映射的数据类型,但是输出为...

java jna
1个回答
0
投票

正如我在注释线程中指出的那样,您不正确地为8字节结构元素UnsignedInt使用了4字节映射(XLuint64)。应将其替换为8字节的变量(例如long)。尽管从技术上讲是正确的,但为每个无符号int值创建一个对象会产生大量开销,因此通常最好简化结构并仅在其中放置int结构元素,并处理无符号性。解释价值时。

© www.soinside.com 2019 - 2024. All rights reserved.