我需要阅读USB量表,DYMO S100量表的数据。哪个Java软件包可以发挥最佳作用?
我已经尝试使用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上,如果这有所不同。
与往常一样,请感谢任何提供的帮助。
- 旁注。这是一个非常古老的软件包,多年来没有更新。想知道是否有更新的最新选项。
I have tried using LibUsb to connect I just really need to be able to parse the data from this scale, in the end it is going to be going into Filemaker pro. the issue I have with this current code:
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;
}
}
is I get a USB error:
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
I have quite a bit of experience with java itself in networking and other areas. I think my issue might be somewhere in the ByteBuffer area... I am not exactly sure about endpoints or buffers I am also trying to access the device through interface 0 which it seems to make the connection and claim the Interface just fine but I need now to get a reading back from the scale...
Is there a better Package out there that can do what I am looking for? or is there just something I am not doing properly in the Synchronous data transfer. Currently am writing this on a Mac if that makes any bit of difference.
As always, appreciate the help if any can provide.
- Side note. This is a decently old package and has not been updated in years. Wonder if there is an updated more recent option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弄清楚了这个问题。我正在发送8个字节。.比例寻找6个字节,尽管Dymo在任何地方都没有这么说。他们的手册是个玩笑……他们没有文件。
main.java:usbscaleinterface.java
:
只需要弄清楚皮革功能现在如何工作,以及从计算机打开比例的可能方法。但是,我认为这不是一件容易的事,因为这将是非常野蛮的力量...请注意,这两个课程都在同一包中。
Figured out the issue. I was sending 8 bytes.. scale looks for 6, although dymo does not say this anywhere. their manual is a joke... and they have no documentation.
Main.java:
UsbScaleInterface.java:
Just need to figure out how the tare function might work now and possible way to turn the scale on from the computer. however I don't think this will be an easy task as it would be pretty brute force... Mind you both of these classes are in the same package.