Java scsi access
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 开发者_如何学Pythonto 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.
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 the scsi_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. The cmd
and buf
arguments map naturally to Java byte arrays (and since Java's arrays know their length, you won't model the cmdLen
and bufLen
arguments at the Java level at all).
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.
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.
精彩评论