本文內容
● 發(fā)現已連接設備數
● 打開設備
● 識別特定設備
● 打開默認設備
● 后續(xù)步驟
本文介紹如何查找然后打開 Femto Bolt。 本文將解釋如何處理有多個設備連接到計算機的情況。
你還可以參考 ,其中演示了如何使用本文所述的函數。
本文將介紹以下函數:
●
●
●
●
發(fā)現已連接的設備數
首先使用 獲取當前已連接的 Femto Bolt 設備數。
打開設備
若要獲取設備的相關信息或從中讀取數據,首先需要使用 打開該設備的句柄。
k4a_device_t device = NULL; for (uint8_t deviceIndex = 0; deviceIndex < device_count; deviceIndex++) { if (K4A_RESULT_SUCCEEDED != k4a_device_open(deviceIndex, &device)) { printf("%d: Failed to open device\n", deviceIndex); continue; } ... k4a_device_close(device); }
的 index 參數指示當連接了多個設備時要打開哪個設備。 如果你預期只會連接一個設備,可以傳遞 K4A_DEVICE_DEFAULT 的參數或 0 來指示第一臺設備。
用完句柄后,每當打開設備時,都需要調用 。 在關閉句柄之前,無法打開同一設備的其他句柄。
識別特定的設備
在附加或分離設備之前,設備按索引枚舉的順序不會更改。 若要識別物理設備,應使用設備的序列號。
若要讀取設備中的序列號,請在打開句柄后使用 函數。此示例演示如何分配適量內存來存儲序列號。
char *serial_number = NULL; size_t serial_number_length = 0; if (K4A_BUFFER_RESULT_TOO_SMALL != k4a_device_get_serialnum(device, NULL, &serial_number_length)) { printf("%d: Failed to get serial number length\n", deviceIndex); k4a_device_close(device); device = NULL; continue; } serial_number = malloc(serial_number_length); if (serial_number == NULL) { printf("%d: Failed to allocate memory for serial number (%zu bytes)\n", deviceIndex, serial_number_length); k4a_device_close(device); device = NULL; continue; } if (K4A_BUFFER_RESULT_SUCCEEDED != k4a_device_get_serialnum(device, serial_number, &serial_number_length)) { printf("%d: Failed to get serial number\n", deviceIndex); free(serial_number); serial_number = NULL; k4a_device_close(device); device = NULL; continue; } printf("%d: Device \"%s\"\n", deviceIndex, serial_number);
打開默認設備
在大多數應用程序中,只會將單個Femto Bolt 附加到同一臺計算機。 如果預期只需連接單個設備,可以結合 K4A_DEVICE_DEFAULT 的 index 調用 打開第一臺設備。
k4a_device_t device = NULL; uint32_t device_count = k4a_device_get_installed_count(); if (device_count != 1) { printf("Unexpected number of devices found (%d)\n", device_count); goto Exit; } if (K4A_RESULT_SUCCEEDED != k4a_device_open(K4A_DEVICE_DEFAULT, &device)) { printf("Failed to open device\n"); goto Exit; }
后續(xù)步驟