本文內容
● 發(fā)現已連接設備數
● 打開(kāi)設備
● 識別特定設備
● 打開(kāi)默認設備
● 后續步驟
本文介紹如何查找然后打開(kāi) Femto Bolt。 本文將解釋如何處理有多個(gè)設備連接到計算機的情況。
你還可以參考 SDK 枚舉示例,其中演示了如何使用本文所述的函數。
本文將介紹以下函數:
● k4a_device_get_installed_count()
發(fā)現已連接的設備數
首先使用 k4a_device_get_installed_count() 獲取當前已連接的 Femto Bolt 設備數。
打開(kāi)設備
若要獲取設備的相關(guān)信息或從中讀取數據,首先需要使用 k4a_device_open() 打開(kāi)該設備的句柄。
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);
}k4a_device_open() 的 index 參數指示當連接了多個(gè)設備時(shí)要打開(kāi)哪個(gè)設備。 如果你預期只會(huì )連接一個(gè)設備,可以傳遞 K4A_DEVICE_DEFAULT 的參數或 0 來(lái)指示第一臺設備。
用完句柄后,每當打開(kāi)設備時(shí),都需要調用 k4a_device_close()。 在關(guān)閉句柄之前,無(wú)法打開(kāi)同一設備的其他句柄。
識別特定的設備
在附加或分離設備之前,設備按索引枚舉的順序不會(huì )更改。 若要識別物理設備,應使用設備的序列號。
若要讀取設備中的序列號,請在打開(kāi)句柄后使用 k4a_device_get_serialnum() 函數。此示例演示如何分配適量?jì)却鎭?lái)存儲序列號。
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);打開(kāi)默認設備
在大多數應用程序中,只會(huì )將單個(gè)Femto Bolt 附加到同一臺計算機。 如果預期只需連接單個(gè)設備,可以結合 K4A_DEVICE_DEFAULT 的 index 調用 k4a_device_open() 打開(kāi)第一臺設備。
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;
}后續步驟