Java SCSI 访问
我想将 Linux C 程序移植到 Java。该程序控制通过 USB 电缆连接到 PC 的相机。 C 代码使用 Linux SCSI Generic (sg)。
C 程序的示例代码:
#include <linux/../scsi/sg.h>
...
static int scsi_write(int sg_fd, uint8_t *cmd, uint32_t cmdLen,
uint8_t *buf, uint32_t bufLen) {
sg_io_hdr_t io;
int r;
memset(&io, 0, sizeof(io));
io.interface_id = 'S';
io.cmd_len = cmdLen;
...
r = ioctl(sg_fd, SG_IO, &io);
...
}
有没有办法将此程序移植到 Java?我正在寻找为 Java 编写的跨平台 SCSI 库,但没有找到。我也在寻找 SCSI/sg 上的 JNI,但也没有成功。
I'd like to port a Linux C program to Java. This program controls a camera which is connected to the PC with a USB cable. The C code uses Linux SCSI Generic (sg).
Sample code from the C program:
#include <linux/../scsi/sg.h>
...
static int scsi_write(int sg_fd, uint8_t *cmd, uint32_t cmdLen,
uint8_t *buf, uint32_t bufLen) {
sg_io_hdr_t io;
int r;
memset(&io, 0, sizeof(io));
io.interface_id = 'S';
io.cmd_len = cmdLen;
...
r = ioctl(sg_fd, SG_IO, &io);
...
}
Is there a way to port this program to Java? I was searching for a cross-platform SCSI library written for Java, but found none. I was also searching for a JNI over SCSI/sg, also no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然 Java 支持许多 POSIX API,但 ioctl 系统调用并不是其功能的一部分。您需要做的是使用 JNI 来允许 Java 调用函数,例如您在问题中编写的
scsi_write
。考虑到您正在谈论与外部硬件的接口,使用更多垫片的额外成本是最小的。cmd
和buf
参数自然地映射到 Java 字节数组(并且由于 Java 的数组知道它们的长度,因此您不会对cmdLen
和 < Java 级别的 code>bufLen 参数)。While Java supports a lot of the POSIX API, the
ioctl
system call is not part of what it does. What you'll need to do is to use JNI to allow Java to call a function such as thescsi_write
you wrote in the question. The extra cost of using more shims is minimal given that you're talking about interfacing to external hardware anyway. Thecmd
andbuf
arguments map naturally to Java byte arrays (and since Java's arrays know their length, you won't model thecmdLen
andbufLen
arguments at the Java level at all).您可能会更幸运地使用基于 Java 的 USB 库,例如 JSR080 (javax.usb) 的实现。您可以在此处找到参考实现,但只有 Linux 实现可以用于生产。
You may have more luck with a Java based USB library, like an implementation of JSR080 (javax.usb). You can find the reference implementation here, but only the Linux implementation is kind of production ready.
请尝试IOCTL,您可能想查看sg3_utils源代码以了解如何通过ioctl发送SCSI PDU,它是C代码,但PDU和ioctl是相同的。然后你就知道你可以控制相机了。
Please try IOCTL, you may want to have a look at sg3_utils source code to learn how to send SCSI PDU by ioctl, it's C code, but PDU and ioctl are the same. Then you know you can control the camera.