2016-12-08 253 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實現讀取數據很容易您感興趣的端口名稱。

希望它有幫助。