2016-12-08 259 views
2

目前我正在进行大学项目,在那里我必须创建一个程序来控制玻璃容器中的湿度。 为此我有一个湿度计。从USB端口读取数据

首先程序必须从湿度计导入原始数据,但我不知道它是如何工作的。 该文件说,它有一个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()); 
} 
+2

您需要查看传感器的文档。应该有某种驱动程序或SDK描述。当你有联系 - 你是否告诉他们这是教育用途?可以在许可策略中有所作为。 – Fildor

+0

是的,我告诉他它的教育用途。但最后他只是再次给我发了文件。当我在家时,我会添加一些文件的图片。 – Xinren

+0

从文档看来,它似乎是如此经常,他们做rs232模拟。所以你必须连接到一个虚拟的COM端口。然后您可以按照第23页和以下所述读出数据。 – Fildor

回答

0

我觉得usb4java是非常好的,但相当复杂。

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”只是一个例子名字,你应该使用:您可以从USB端口读取数据的jssc

来自实例USB实现读取数据很容易您感兴趣的端口名称。

希望它有帮助。