【问题标题】:Reading data from USB Port从 USB 端口读取数据
【发布时间】:2016-12-08 10:06:46
【问题描述】:

目前我正在为大学做一个项目,我必须创建一个程序来控制玻璃容器中的湿度。 为此,我有一个湿度计。

首先程序必须从湿度计中导入原始数据,但我不知道它是如何工作的。 文档说它有一个 USB 接口,但我只能找到解析原始数据的方法。 我还给销售这个湿度计的公司写了一封电子邮件。他们说有一个外部软件可以导入和处理这些数据。但是我不允许使用外部软件。 因此我不得不直接从 USB 端口读取原始数据。我尝试使用 usb4java,但我只能找到所有连接的 USB 设备。 我不知道该怎么做。请帮我 documentation documentation

代码如下

public class DumpDevices
{
/**
 * Dumps the specified USB device to stdout.
 * 
 * @param device
 *            The USB device to dump. 
 */


private static void dumpDevice(final UsbDevice device)
{
    // Dump information about the device itself
    System.out.println(device);
    final UsbPort port = device.getParentUsbPort();
    if (port != null)
    {
        System.out.println("Connected to port: " + port.getPortNumber());
        System.out.println("Parent: " + port.getUsbHub());
    }

    // Dump device descriptor
    System.out.println(device.getUsbDeviceDescriptor());

    // Process all configurations
    for (UsbConfiguration configuration: (List<UsbConfiguration>) device
        .getUsbConfigurations())
    {
        // Dump configuration descriptor
        System.out.println(configuration.getUsbConfigurationDescriptor());

        // Process all interfaces
        for (UsbInterface iface: (List<UsbInterface>) configuration
            .getUsbInterfaces())
        {
            // Dump the interface descriptor
            System.out.println(iface.getUsbInterfaceDescriptor());

            // Process all endpoints
            for (UsbEndpoint endpoint: (List<UsbEndpoint>) iface
                .getUsbEndpoints())
            {
                // Dump the endpoint descriptor
                System.out.println(endpoint.getUsbEndpointDescriptor());
            }
        }
    }

    System.out.println();

    // Dump child devices if device is a hub
    if (device.isUsbHub())
    {
        final UsbHub hub = (UsbHub) device;
        for (UsbDevice child: (List<UsbDevice>) hub.getAttachedUsbDevices())
        {
            dumpDevice(child);
        }
        System.out.println(hub);
    }
}


/**
 * Main method.
 * 
 * @param args
 *            Command-line arguments (Ignored)
 * @throws UsbException
 *             When an USB error was reported which wasn't handled by this
 *             program itself.
 */
public static void main(final String[] args) throws UsbException
{
    // Get the USB services and dump information about them
    final UsbServices services = UsbHostManager.getUsbServices();
    System.out.println("USB Service Implementation: "
        + services.getImpDescription());
    System.out.println("Implementation version: "
        + services.getImpVersion());
    System.out.println("Service API version: " + services.getApiVersion());
    System.out.println();

    // Dump the root USB hub
    dumpDevice(services.getRootUsbHub());
}

【问题讨论】:

  • 您需要查看传感器的文档。应该有某种驱动程序或 SDK 描述。当您联系时 - 您是否告诉他们这是用于教育用途?可以在许可政策方面有所作为。
  • 是的,我告诉他这是为了教育用途。但最后他只是再次给我发了文件。我会在我在家的时候添加一些文档的图片。
  • 从文档看来,他们经常进行 rs232 仿真。所以你必须连接到一个虚拟的com端口。然后,您可以按照第 23 页及以下内容的说明读出数据。

标签: java usb usb4java


【解决方案1】:

我认为 usb4java 非常好,但相当复杂。使用jssc

,您可以很容易地完成从 USB 端口读取数据

从usb读取数据的例子:

SerialPort serialPort = new SerialPort("/dev/tty.usbmodem1435");
serialPort.openPort();//Open serial port
serialPort.setParams(4800, 8, 1, 0);//Set params.
while(true) {
    byte[] buffer = serialPort.readBytes(10);
    if(buffer!=null) {
        for(byte b:buffer) {
            System.out.print(b);
        }
    } 
}

注意“/dev/tty.usbmodem1435”只是一个示例名称,您应该使用您感兴趣的端口名称。

希望对你有帮助。

【讨论】:

  • 对我不起作用。当我调用 SerialPortList() 时,只需返回一个空列表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 2012-08-29
  • 1970-01-01
相关资源
最近更新 更多