我需要阅读USB量表,DYMO S100量表的数据。哪个Java软件包可以发挥最佳作用?

发布于 2025-02-05 14:11:00 字数 4162 浏览 1 评论 0原文

我已经尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2025-02-12 14:11:00

弄清楚了这个问题。我正在发送8个字节。.比例寻找6个字节,尽管Dymo在任何地方都没有这么说。他们的手册是个玩笑……他们没有文件。
main.java:usbscaleinterface.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();
        }
    }

}

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);
    }
}

只需要弄清楚皮革功能现在如何工作,以及从计算机打开比例的可能方法。但是,我认为这不是一件容易的事,因为这将是非常野蛮的力量...请注意,这两个课程都在同一包中。

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:

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);
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文