java 从 USB 端口读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41036718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading data from USB Port
提问by Xinren
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. 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. I tried to work with usb4java but i was only able to find all connected usb devices. I have no idea how to go on. Please help me documentationdocumentation
首先,程序必须从湿度计导入原始数据,但我不知道它是如何工作的。文档说它有一个 USB 接口,但我只能找到如何解析原始数据的方法。我还给销售这种湿度计的公司写了一封电子邮件。他们说有一个外部软件可以导入和处理这些数据。但是我不允许使用外部软件。因此我被迫直接从 USB 端口读取原始数据。我尝试使用 usb4java,但我只能找到所有连接的 USB 设备。我不知道如何继续。请帮我 文档文档
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());
}
回答by Ramon Rius
I think usb4java is very good but quite complex. You might accomplish reading data from usb port very easily with jssc
我认为 usb4java 非常好但相当复杂。您可以使用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.
希望能帮助到你。