USBEndpoint
注意:此功能在 Web Workers 中可用。
USBEndpoint 接口(WebUSB API 的一部分)提供有关 USB 设备提供的端点的信息。端点表示进入或离开设备的单向数据流。
构造函数
USBEndpoint()实验性-
创建一个新的
USBEndpoint对象,该对象将使用提供的USBAlternateInterface对象、给定的端点编号和传输方向的信息进行填充。
实例属性
USBEndpoint.endpointNumber实验性-
返回此端点的“端点编号”,该编号是从定义此端点的端点描述符的
bEndpointAddress字段中提取的值(1 到 15)。调用USBDevice上的方法时使用此值来标识端点。 USBEndpoint.direction实验性-
返回此端点传输数据的方向,以下之一:
"in"- 数据从设备传输到主机。"out"- 数据从主机传输到设备。
USBEndpoint.type实验性-
返回此端点的类型,以下之一:
"bulk"- 提供大数据块的可靠数据传输。通过批量端点发送的数据保证会被传递或生成错误,但可能会被其他数据流量抢占。"interrupt"- 提供小数据块的可靠数据传输。通过中断端点发送的数据保证会被传递或生成错误,并且还享有专用的总线传输时间。"isochronous"- 提供必须定期传输的数据块的不可靠数据传输。它们享有专用的总线时间,但如果错过截止日期,数据将被丢弃。
USBEndpoint.packetSize实验性-
返回通过此端点发送的数据将被分割成的包的大小。
示例
虽然开发人员有时会提前知道设备端点的确切布局,但在某些情况下必须在运行时发现。例如,USB 串行设备必须提供批量输入和输出端点,但它们的端点编号将取决于设备提供的其他接口。
此代码通过查找实现 USB CDC 接口类的接口,然后根据它们的类型和方向识别候选端点来识别正确的端点。
js
let inEndpoint = undefined;
let outEndpoint = undefined;
for (const { alternates } of device.configuration.interfaces) {
// Only support devices with out multiple alternate interfaces.
const alternate = alternates[0];
// Identify the interface implementing the USB CDC class.
const USB_CDC_CLASS = 10;
if (alternate.interfaceClass !== USB_CDC_CLASS) {
continue;
}
for (const endpoint of alternate.endpoints) {
// Identify the bulk transfer endpoints.
if (endpoint.type !== "bulk") {
continue;
}
if (endpoint.direction === "in") {
inEndpoint = endpoint.endpointNumber;
} else if (endpoint.direction === "out") {
outEndpoint = endpoint.endpointNumber;
}
}
}
规范
| 规范 |
|---|
| WebUSB API # usbendpoint-interface |
浏览器兼容性
加载中…