4

I'm trying to use JNA to talk over a USB device plugged into the computer. Using Java and a .dll that was provided to me. I am having trouble with the Write function:

C code:

typedef struct {
    unsigned int id;
    unsigned int timestamp;
    unsigned char flags;
    unsigned char len;
    unsigned char data[16];
} CANMsg;

CAN_STATUS canplus_Write(
        CANHANDLE handle, //long
        CANMsg *msg
    );

Java Equivalent:

public class CANMsg extends Structure{
    public int id = 0;
    public int timestamp = 0;
    public byte flags = 0;
    public byte len = 8;
    public byte data[] = new byte[16];
}

int canplus_Write(NativeLong handle, CANMsg msg);

I have confirmed that I can open and close the device. The close requires the NativeLong handle, so i am assuming that the CANMsg msg is the issue here. I have also confirmed that the device works when tested with C only code.

I have read the the JNA documentation thoroughly... I think. Any pointers. Thanks all.

4

4 回答 4

1

len 是结构的大小吗?如果是; 那么你给出的值是错误的。做这个:

CANMsg msg = new CANMsg();
msg.len = msg.size();
于 2009-07-29T17:44:52.750 回答
1

我也遇到了 canplus_write 接口的问题。到目前为止,一切都指向提供的驱动程序中的错误——我认为新的 USBCANPlus 模块还没有经过适当的测试阶段。从您的代码中,我可以看到您使用的是旧版本的驱动程序,因为数据字段应包含 8 个字节(这是 CAN 消息中的最大数据字节数)。我通过自己的调查发现,驱动程序无法将数据正确转换为 ASCII 字符,即如果您要发送 01 02 03,它将向模块发送 ASCII 字符 '1' '2' '3' '0' '1' '0' '2' '0' '3' - 你可以使用USB监控软件来验证这一点。

希望这也能解决您的问题,我也建议您与他们联系。

于 2009-08-06T16:48:59.787 回答
1

我对 JNA 了解不多,但是当指针作为简单地址传输时,语言间数据传输通常会失败。

如果它指向您要发送的数据,则可能在某处有一个打包方法调用。如果您不是自己编写的,那么它可能是由这个 JNA 框架生成的......将它添加到您的问题中可能很有用。

将 C 字符映射到 Java 字节对我来说也有点奇怪,但我可以看到它的来源。您在什么操作系统上运行此代码?

于 2009-06-01T17:48:45.587 回答
0

我对您正在使用的 dll 一无所知,但 CANMsg.len 很可能指的是 byte[] 数据中实际有多少字节。因此,您要么需要跟踪写入 byte[16] 数据数组的数量,要么根据终止的 null char 计算 len(假设 String ASCII 是数据)。我不认为 CANMsg.size() 存在或像上面的 Rusty 建议的那样实现。

于 2010-06-10T13:17:34.853 回答