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 |
瀏覽器相容性
載入中…