我已经尝试过使用LibUsb来连接,我只是真的需要能够从这个规模解析数据,最后它将进入Filemaker pro。我对当前代码的问题:
package Model;
import javax.management.Descriptor;
import javax.usb.UsbDevice;
import javax.usb.UsbDeviceDescriptor;
import javax.usb.UsbHub;
import org.usb4java.*;
import java.nio.ByteBuffer;
import java.util.List;
public class main {
//Vendor ID = 0x0922
//Product ID = 0x8009
private static short Vendor_ID = 0x0922;
private static short Product_ID = (short) 0x8009;
public static void main(String[] args) {
Device Dymo_Scale = listDevices(Vendor_ID, Product_ID);
DeviceHandle handle = new DeviceHandle();
int result = LibUsb.open(Dymo_Scale, handle);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to open USB device", result);
try
{
result = LibUsb.setConfiguration(handle, 0);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to set Configuration", result);
// Use device handle here
result = LibUsb.claimInterface(handle, 0);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to claim interface", result);
try
{
ByteBuffer buffer = ByteBuffer.allocateDirect(8);
buffer.put(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
int transfered = LibUsb.controlTransfer(handle,
(byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
(byte) 0, (short) 0, (short) 0, buffer, 5);
if (transfered < 0) throw new LibUsbException("Control transfer failed", transfered);
System.out.println(transfered + " bytes sent");
}
finally
{
result = LibUsb.releaseInterface(handle, 0);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to release interface", result);
}
}
finally
{
LibUsb.close(handle);
}
}
private static Device listDevices(short vendorId, short productId) {
// Create the libusb context
Context context = new Context();
// Initialize the libusb context
int result = LibUsb.init(context);
if (result < 0)
{
throw new LibUsbException("Unable to initialize libusb", result);
}
// Read the USB device list
DeviceList list = new DeviceList();
result = LibUsb.getDeviceList(context, list);
if (result < 0)
{
throw new LibUsbException("Unable to get device list", result);
}
try
{
// Iterate over all devices and list them
for (Device device: list)
{
int address = LibUsb.getDeviceAddress(device);
int busNumber = LibUsb.getBusNumber(device);
DeviceDescriptor descriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device, descriptor);
if (result < 0)
{
throw new LibUsbException(
"Unable to read device descriptor", result);
}
if (descriptor.idVendor() == vendorId && descriptor.idProduct() == productId) return device;
}
}
finally
{
// Ensure the allocated device list is freed
LibUsb.freeDeviceList(list, true);
}
// Deinitialize the libusb context
LibUsb.exit(context);
return null;
}
}
是我得到一个USB错误:
Exception in thread "main" org.usb4java.LibUsbException: USB error 9: Control transfer failed: Pipe error
at Model.main.main(main.java:41)
Process finished with exit code 1
我在java本身的网络和其他领域有相当多的经验。我认为我的问题可能是在ByteBuffer区域的某个地方…我不完全确定端点或缓冲区,我也试图通过接口0访问设备,它似乎建立连接并声称接口很好,但我现在需要从刻度中获得读数…
有没有更好的有什么东西能满足我的需求吗?或者只是我在同步数据传输中没有做正确的事情。目前我在Mac上写这篇文章,如果这有任何不同的话。
一如既往,感谢您的帮助。
- 边注。这是一个相当老的软件包,多年没有更新了。不知道是否有更新的更近期的选项。
找到问题了。我发送了8个字节…尺度为6,尽管dymo在任何地方都没有这样说。他们的手册就是个笑话…而且他们没有文件。Main.java:
package Model;
import javax.management.Descriptor;
import javax.usb.*;
import javax.usb.event.UsbPipeListener;
import org.usb4java.*;
import java.nio.ByteBuffer;
import java.util.List;
public class main{
//Vendor ID = 0x0922
//Product ID = 0x8009
private static short Vendor_ID = 0x0922;
private static short Product_ID = (short) 0x8009;
public static void main(String[] args) throws UsbException {
UsbScaleInterface scale = UsbScaleInterface.findScale();
scale.open();
try {
for (int i = 0; i < 60; i++) {
scale.syncSubmit();
}
} finally {
scale.close();
}
}
}
UsbScaleInterface.java:
package Model;
import javax.usb.*;
import javax.usb.event.UsbPipeDataEvent;
import javax.usb.event.UsbPipeErrorEvent;
import javax.usb.event.UsbPipeListener;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class UsbScaleInterface implements UsbPipeListener {
private final UsbDevice device;
private UsbInterface iface;
private UsbPipe pipe;
private byte[] data = new byte[6];
private UsbScaleInterface(UsbDevice device) {
this.device = device;
}
public static UsbScaleInterface findScale() throws UsbException {
UsbServices services = UsbHostManager.getUsbServices();
UsbHub rootHub = services.getRootUsbHub();
// Dymo M10 Scale:
UsbDevice device = findDevice(rootHub, (short) 0x0922, (short) 0x8003);
// Dymo M25 Scale:
if (device == null) {
device = findDevice(rootHub, (short) 0x0922, (short) 0x8004);
}
// Dymo S100 Scale:
if (device == null) {
device = findDevice(rootHub, (short) 0x0922, (short) 0x8009);
}
if (device == null) {
return null;
}
return new UsbScaleInterface(device);
}
private static UsbDevice findDevice(UsbHub hub, short vendorId, short productId) {
for (UsbDevice device : (List<UsbDevice>) hub.getAttachedUsbDevices()) {
UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
if (desc.idVendor() == vendorId && desc.idProduct() == productId) {
return device;
}
if (device.isUsbHub()) {
device = findDevice((UsbHub) device, vendorId, productId);
if (device != null) {
return device;
}
}
}
return null;
}
public void open() throws UsbException {
UsbConfiguration configuration = device.getActiveUsbConfiguration();
iface = configuration.getUsbInterface((byte) 0);
// this allows us to steal the lock from the kernel
iface.claim(usbInterface -> true);
final List<UsbEndpoint> endpoints = iface.getUsbEndpoints();
pipe = endpoints.get(0).getUsbPipe(); // there is only 1 endpoint
pipe.addUsbPipeListener(this);
pipe.open();
}
public void syncSubmit() throws UsbException {
pipe.syncSubmit(data);
}
public void close() throws UsbException {
pipe.close();
iface.release();
}
@Override
public void dataEventOccurred(UsbPipeDataEvent upde) {
boolean empty = data[1] == 2;
boolean overweight = data[1] == 6;
boolean negative = data[1] == 5;
boolean grams = data[2] == 2;
int scalingFactor = data[3];
int weight = (data[4] & 0xFF) + (data[5] << 8);
if (empty) {
System.out.println("EMPTY");
} else if (overweight) {
System.out.println("OVERWEIGHT");
} else if (negative) {
System.out.println("NEGATIVE");
} else { // Use String.format since printf causes problems on remote exec
System.out.println(String.format("Weight = %,.1f%s",
scaleWeight(weight, scalingFactor),
grams ? "g" : "oz"));
}
}
private double scaleWeight(int weight, int scalingFactor) {
return weight * Math.pow(10, scalingFactor);
}
@Override
public void errorEventOccurred(UsbPipeErrorEvent usbPipeErrorEvent) {
Logger.getLogger(UsbScaleInterface.class.getName()).log(Level.SEVERE, "Scale Error", usbPipeErrorEvent);
}
}
只需要弄清楚现在的功能是如何工作的,以及从计算机上打开秤的可能方法。然而,我不认为这将是一个简单的任务,因为它将是相当蛮力…请注意,这两个类都在同一个包中。