USB 介紹
1994年,一個由四個行業伙伴(Compaq、Intel、Microsoft 和 NEC)組成的聯盟開始制定 USB 協議。該協議最初的目的是將 PC 與電話相連并提供輕易擴展和重新配置的 I/O 接口。1996年 1月,發表了 USB 規范的第一個版本,1998年 9月發表了后續版本(版本 1.1)。這個規范答應 127臺設備同時連接到一起,總的通信帶寬限制為 12 Mbps。后來,又有三個成員(Hewlett-Packard、LUCent 和 Philips)加入了這個聯盟。2000年 4月,發表了 USB 規范的 2.0版本,它支持高達 480 Mbps 的傳輸率。今天,USB 在高速(視頻、圖像、儲存)和全速(音頻、寬帶、麥克風)數據傳輸應用中起了要害作用。它還使各種低速設備(鍵盤、鼠標、游戲外設、虛擬現實外設)連接到 PC 上。
import usb.core.*;
public class ListUSB
{
public static void main(String[] args)
{
try
{
// Bootstrap by getting the USB Host from the HostFactory.
Host host = HostFactory.getHost();
// OBTain a list of the USB buses available on the Host.
Bus[] bus = host.getBusses();
int total_bus = bus.length;
// Traverse through all the USB buses.
for (int i=0; i
{
// access the root hub on the USB bus and obtain the
// number of USB ports available on the root hub.
Device root = bus[i].getRootHub();
int total_port = root.getNumPorts();
// Traverse through all the USB ports available on the
// root hub. It should be mentioned that the numbering
// starts from 1, not 0.
for (int j=1; j<=total_port; j++)
{
// Obtain the Device connected to the port.
Device device = root.getChild(j);
if (device != null)
{
// USB device available, do something here.
}
}
}
} catch (Exception e)
{
System.out.println(e.getMessage());
}
} if (device != null)
{
// Obtain the current Configuration of the device and the number of
// Interfaces available under the current Configuration.
Configuration config = device.getConfiguration();
int total_interface = config.getNumInterfaces();
// Traverse through the Interfaces
for (int k=0; k
{
// Access the currently Interface and obtain the number of
// endpoints available on the Interface.
Interface itf = config.getInterface(k, 0);
int total_ep = itf.getNumEndpoints();
// Traverse through all the endpoints.
for (int l=0; l
{
// Access the endpoint, and obtain its I/O type.
Endpoint ep = itf.getEndpoint(l);
String io_type = ep.getType();
boolean input = ep.isInput();
// If the endpoint is an input endpoint, obtain its
// InputStream and read in data.
if (input)
{
InputStream in;
in = ep.getInputStream();
// Read in data here
in.close();
}
// If the Endpoint is and output Endpoint, obtain its
// OutputStream and write out data.
else
{
OutputStream out;
out = ep.getOutputStream();
// Write out data here.
out.close();
}
}
}
} import javax.usb.*;
import java.util.List;
public class TraverseUSB
{
public static void main(String argv[])
{
try
{
// Access the system USB services, and access to the root
// hub. Then traverse through the root hub.
UsbServices services = UsbHostManager.getUsbServices();
UsbHub rootHub = services.getRootUsbHub();
traverse(rootHub);
} catch (Exception e) {}
}
public static void traverse(UsbDevice device)
{
if (device.isUsbHub())
{
// This is a USB Hub, traverse through the hub.
List attachedDevices = ((UsbHub) device).getAttachedUsbDevices();
for (int i=0; i {
traverse((UsbDevice) attachedDevices.get(i));
}
}
else
{
// This is a USB function, not a hub.
// Do something.
}
}
} public static void testIO(UsbDevice device)
{
try
{
// Access to the active configuration of the USB device, obtain
// all the interfaces available in that configuration.
UsbConfiguration config = device.getActiveUsbConfiguration();
List totalInterfaces = config.getUsbInterfaces();
// Traverse through all the interfaces, and access the endpoints
// available to that interface for I/O.
for (int i=0; i
{
UsbInterface interf = (UsbInterface) totalInterfaces.get(i);
interf.claim();
List totalEndpoints = interf.getUsbEndpoints();
for (int j=0; j
{
// Access the particular endpoint, determine the direction
// of its data flow, and type of data transfer, and open the
// data pipe for I/O.
UsbEndpoint ep = (UsbEndpoint) totalEndpoints.get(i);
int direction = ep.getDirection();
int type = ep.getType();
UsbPipe pipe = ep.getUsbPipe();
pipe.open();
// Perform I/O through the USB pipe here.
pipe.close();
}
interf.release();
}
} catch (Exception e) {}
} 新聞熱點
疑難解答