JNA 通过引用传递结构帮助

发布于 2024-07-23 07:50:32 字数 791 浏览 4 评论 0原文

我正在尝试使用 JNA 通过插入计算机的 USB 设备进行通话。 使用 Java 和提供给我的 .dll。 我在使用 Write 函数时遇到问题:

C 代码:

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 等效项:

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);

我已确认可以打开和关闭设备。 关闭需要 NativeLong 句柄,因此我假设 CANMsg 消息是这里的问题。 我还确认该设备在使用纯 C 代码进行测试时可以正常工作。

我已经彻底阅读了 JNA 文档...我想。 任何指点。 谢谢大家。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花伊自在美 2024-07-30 07:50:32

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

如果它指向您想要发送的数据,则可能在某个地方存在打包方法调用。 如果您没有自己编写它,也许它是由这个 JNA 框架生成的...将其添加到您的问题中可能会很有用。

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

I don't know much about JNA but inter-language data transfer usually fails when pointers get transfered as a simple address.

If it's the data it points to that you want to send accross, there's presumably a packaging method call in there somewhere. If you didn't write it yourself, maybe it is generated by this JNA framework... Could be useful to add it to your question.

Mapping C char to Java byte is also a bit weird to me but I can see where that could come from. What operating system are you running this code on?

稳稳的幸福 2024-07-30 07:50:32

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

CANMsg msg = new CANMsg();
msg.len = msg.size();

Is len the size of the structure? If yes; then the value you have given is wrong. Do this:

CANMsg msg = new CANMsg();
msg.len = msg.size();
记忆里有你的影子 2024-07-30 07:50:32

我也遇到了 canplus_write 接口的问题。 到目前为止,一切都指向所提供的驱动程序中的错误 - 我认为新的 USBCANPlus 模块尚未经过适当的测试阶段。 从您的代码中我可以看到您开始使用旧版本的驱动程序,因为数据字段应包含 8 个字节(这是 CAN 消息中数据字节的最大数量)。 我通过自己的调查发现,驱动程序无法正确地将数据转换为 ASCII 字符,即如果您想发送 01 02 03,它会将 ASCII 字符 '1' '2' '3' 传输到模块'0' '1' '0' '2' '0' '3' - 您可以使用 USB 监控软件来验证这一点。 我目前正在与 FTDI 技术支持联系,希望他们尽快发布驱动程序的更新版本。

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

I too am having a problem with the canplus_write interface. So far everything is pointing towards a bug in the provided driver - I don't think the new USBCANPlus module has gone through a proper testing phase yet. From your code I can see that you are using an older version of the driver to begin with, as the data field should consist of 8 bytes (that's the maximum number of data bytes in a CAN message). What I have found through my own investigations is that the driver fails to properly convert the data into ASCII characters i.e. if you want to send 01 02 03, it will transmit the ASCII characters '1' '2' '3' to the module instead of '0' '1' '0' '2' '0' '3' - you can use USB monitoring software to verify this. I am in contact with the FTDI technical support at the moment and am hoping they will release an updated version of the driver soon.

Hope this solves your problem too, I would recommend getting in touch with them as well.

花辞树 2024-07-30 07:50:32

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

I don't know anything about the dll you are using but the CANMsg.len is more than likely referring to how many bytes are actually in the byte[] data. So you would either need to keep track of how much you write to the byte[16] data array or calculate the len based on the terminating null char (assuming String ASCII is the data). I don't think the CANMsg.size() exists or is implemented like Rusty above suggests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文