如何在Java中映射void *

时间:2019-05-07 16:26:43

标签: java jna can-bus

我正在尝试将vxlapi.dll提供的以下C方法映射到Java:

XLstatus xlCanTransmit (
XLportHandle portHandle,
Xlaccess accessMask,
unsigned int *messageCount,
void *pMessages)

我的问题与 void * pmessages 有关,我尝试按照JNA映射页中的指示使用Pointer,但dll似乎不接受该类型。 我需要传递一个s_xl_event消息,它是一个如此定义的结构:

public class s_xl_event extends Structure {
    public class ByReference extends s_xl_event implements Structure.ByReference {}

    public byte tag;
    public byte chanIndex;
    public short transId;
    public short portHandle;
    public byte flags;
    public byte reserved;
    public long timeStamp;
    public s_xl_tag_data tagData;

    public s_xl_event() {
        allocateMemory(56); // vxlapi.h: s_xl_event requires 48 byte
    }

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList("tag","chanIndex","transId","portHandle","flags","reserved","timeStamp","tagData");
    }   
}

如果我传递s_xl_event项目,则dll响应正常,但未按预期运行,将所有值发送为0。使用指针返回XL_ERR_WRONG_PARAMETER。 您能帮我了解我的错吗?谢谢

2 个答案:

答案 0 :(得分:0)

我创建了一种名为send的方法,该方法执行以下步骤:

IntByReference pEventCount = new IntByReference(1);
s_xl_event eventMsg = new s_xl_event();

eventMsg.tag = 10; //  XL_TRANSMIT_MSG
eventMsg.tagData.msg.id = Long.parseLong(message.getID());
eventMsg.tagData.msg.dlc = (short) message.getSize();
eventMsg.tagData.msg.flags = (short) 0;
try {
    eventMsg.tagData.msg.data = Hex.decodeHex(message.getDefaultPayload());
} catch(Exception e) {}

eventMsg.write();

short status = driver.canTransmit(portHandle, accessMask, pEventCount, eventMsg.getPointer());

方法canTrasmit调用扩展库的接口中定义的函数:

public short canTransmit(long portHandle, long accessMask, IntByReference messageCount, Pointer message) {
            short status = device.xlCanTransmit(portHandle, accessMask, messageCount, message);

        return status;
}
public interface VectorInterface extends Library {
short xlCanTransmit(long portHandle, long accessMask, IntByReference messageCount, Pointer message);
}

我想API中已指出Structure已经通过引用。我需要分配内存,因为在接收中分配的数量(24)不足,并且会导致JVM崩溃。

答案 1 :(得分:0)

我发现结构放置错误,并且缺少静态类型。我正确地重写了该结构,但现在遇到的问题是,在s_xl_event中,除s_xl_tag_data外的所有字段都已填充。我不知道这是否取决于它是其他结构的结合...