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.
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.