[英]Reading data from USB Port
currently I am working on a project for university where i have to create a program which controls the humidity in a terrarium. 目前我正在开展一个大学项目,我必须创建一个控制玻璃容器湿度的程序。 On this purpose i've got an hygrometer.
为此,我有一个湿度计。
First of all the program has to import the raw data from the hygrometer, but i have no idea how it works. 首先,程序必须从湿度计导入原始数据,但我不知道它是如何工作的。 The documentation says there is an USB interface for it, but i can only find the way how to parse the raw data.
文档说它有一个USB接口,但我只能找到解析原始数据的方法。 I also wrote an Email to the company which sells this hygrometer.
我还给销售这种湿度计的公司写了一封电子邮件。 They said theres an external software which imports and handle with this data.
他们说这是一个导入和处理这些数据的外部软件。 However I am not allowed to use external softwares.
但是我不允许使用外部软件。 Therefore I am forced to read the raw data directly from the USB Port.
因此,我被迫直接从USB端口读取原始数据。 I tried to work with usb4java but i was only able to find all connected usb devices.
我尝试使用usb4java,但我只能找到所有连接的usb设备。 I have no idea how to go on.
我不知道该怎么做。 Please help me documentation documentation
请帮我文档 文档
Code below 代码如下
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());
}
I think usb4java is very good but quite complex. 我认为usb4java非常好但非常复杂。 You might accomplish reading data from usb port very easily with jssc
您可以使用jssc轻松完成从USB端口读取数据
Example of reading data from 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);
}
}
}
Notice that '/dev/tty.usbmodem1435' is just an example name, you should use the port name you are interested on. 请注意,'/ dev /tty.usbmodem1435'只是一个示例名称,您应该使用您感兴趣的端口名称。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.