Reading data from USB Port
目前,我正在为一个大学项目工作,我必须创建一个程序来控制玻璃容器中的湿度。
为此,我有一个湿度计。
首先,程序必须从湿度计导入原始数据,但我不知道它是如何工作的。
该文档说有一个USB接口,但是我只能找到解析原始数据的方法。
我还写了一封电子邮件给销售这种湿度计的公司。他们说有一个外部软件可以导入并处理这些数据。但是,我不允许使用外部软件。
因此,我被迫直接从USB端口读取原始数据。我尝试使用usb4java,但我只能找到所有已连接的USB设备。
我不知道如何继续。请帮我
文件资料
文档
下面的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 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()); } |
我认为usb4java很好,但是很复杂。您可以使用jssc
轻松完成从USB端口读取数据的操作
从usb读取数据的示例:
1 2 3 4 5 6 7 8 9 10 11 | 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 \\'只是一个示例名称,您应该使用您感兴趣的端口名称。
希望有帮助。