Skip to content

Instantly share code, notes, and snippets.

@UnaNancyOwen
Last active July 17, 2021 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UnaNancyOwen/f442f615e0a4abde4eb433a87b565ce0 to your computer and use it in GitHub Desktop.
Save UnaNancyOwen/f442f615e0a4abde4eb433a87b565ce0 to your computer and use it in GitHub Desktop.
utility functions for query serial number and open device by specify serial number

Query Serial-Number from All Connected Devices and Open Device by specify Serial-Number

About

This is utility functions for query serial number and open device by specify serial number.
You can open device by specify serial number in Azure Kinect SDK similar to RealSense SDK.
I think these functions will be useful for all Azure Kinect developers.

There is no need to make any changes to the Azure Kinect SDK to use these functions.
Just add these file to your project.

Sample

  • C++
#include <iostream>
#include <sstream>

#include <k4a/k4a.hpp>
#include "utility.h"

int main( int argc, char* argv[] )
{
    try{
        // Query serial numbers from all connected devices
        std::vector<std::string> serial_numbers = k4a::device_query();
        if( serial_numbers.empty() ){
            throw k4a::error( "serial_numbers is empty!" );
        }

        // Open device by specify serial number
        k4a::device device = k4a::device_open( serial_number[0] ); // Specify string retrieved from k4a::device_query()
        //k4a::device device = k4a::device_open( "000000000000" ); // Specify 12-digits string directly
        if( !device.is_valid() ){
            throw k4a::error( "failed open device!" );
        }

        device.close();
    }
    catch( k4a::error& error ){
        std::cout << error.what() << std::endl;
    }

    return 0;
}
  • C#
using System;
using System.Linq;
using System.Collections.Generic;
using K4A = Microsoft.Azure.Kinect.Sensor;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Query serial numbers from all connected devices
            var serial_numbers = K4A.Utility.Device.Query();
            if (!serial_numbers.Any())
            {
                throw new K4A.AzureKinectException("devices not found!");
            }

            // Open device by specify serial number
            var device = K4A.Utility.Device.Open(serial_numbers[0]); // Specify string retrieved from Device.Query()
            //var device = K4A.Utility.Device.Open("000000000000"); // Specify 12-digits string directly
            if (device == null)
            {
                throw new K4A.AzureKinectException("failed open device!");
            }

            device.Dispose();
            device = null;
        }
        catch (K4A.AzureKinectException error)
        {
            Console.WriteLine(error.Message);
        }
    }
}
/*
This is utility to that provides query serial number and open device by specify serial number.
// Query serial numbers from all connected devices
List<string> serial_numbers = Utility.Device.Query();
if( !serial_numbers.Any() ){
throw new Microsoft.Azure.Kinect.Sensor.AzureKinectException( "devices not found!" );
}
// Open device by specify serial number
Microsoft.Azure.Kinect.Sensor.Device device = Utility.Device.Open( serial_number[0] ); // Specify string retrieved from Utility.Device.Query()
//Microsoft.Azure.Kinect.Sensor.Device device = Utility.Device.Open( "000000000000" ); // Specify 12-digits string directly
if( device == null ){
throw new Microsoft.Azure.Kinect.Sensor.AzureKinectException( "failed open device!" );
}
Copyright (c) 2020 Tsukasa Sugiura <t.sugiura0204@gmail.com>
Licensed under the MIT license.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System.Collections.Generic;
using K4A = Microsoft.Azure.Kinect.Sensor;
namespace Microsoft.Azure.Kinect.Sensor.Utility
{
class Device
{
public static List<string> Query()
{
var device_count = K4A.Device.GetInstalledCount();
if (device_count == 0)
{
return new List<string>();
}
var serial_numbers = new List<string>();
for (var device_index = 0; device_index < device_count; device_index++)
{
try
{
var device = K4A.Device.Open(device_index);
serial_numbers.Add(device.SerialNum);
device.Dispose();
device = null;
}
catch (K4A.AzureKinectOpenDeviceException)
{
continue;
}
}
return serial_numbers;
}
public static Microsoft.Azure.Kinect.Sensor.Device Open(string serial_number)
{
const int length = 12;
if (serial_number == null || serial_number.Length != length)
{
return null;
}
var device_count = K4A.Device.GetInstalledCount();
if(device_count == 0)
{
return null;
}
for (var device_index = 0; device_index < device_count; device_index++)
{
try
{
var device = Microsoft.Azure.Kinect.Sensor.Device.Open(device_index);
if (device.SerialNum.Equals(serial_number))
{
return device;
}
device.Dispose();
device = null;
}
catch (K4A.AzureKinectOpenDeviceException)
{
continue;
}
}
return null;
}
}
}
/*
This is utility to that provides query serial number and open device by specify serial number.
// query serial numbers from all connected devices
const std::vector<std::string> serial_numbers = k4a::device_query();
if( serial_numbers.empty() ){
throw k4a::error( "devices not found!" );
}
// open device by specify serial number
k4a::device device = k4a::device_open( serial_number[0] ); // specify string retrieved from k4a::device_query()
//k4a::device device = k4a::device_open( "000000000000" ); // specify 12-digits string directly
if( !device.is_valid() ){
throw k4a::error( "failed open device!" );
}
Copyright (c) 2020 Tsukasa Sugiura <t.sugiura0204@gmail.com>
Licensed under the MIT license.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef __DEVICE_UTILITY__
#define __DEVICE_UTILITY__
#include <string>
#include <vector>
#include <k4a/k4a.hpp>
namespace k4a
{
std::vector<std::string> device_query()
{
const int32_t device_count = k4a::device::get_installed_count();
if( device_count == 0 ){
return std::vector<std::string>();
}
std::vector<std::string> serial_numbers;
for( int32_t device_index = 0; device_index < device_count; device_index++ ){
try{
k4a::device device = k4a::device::open( device_index );
serial_numbers.push_back( device.get_serialnum() );
device.close();
}
catch( const k4a::error& error ){
continue;
}
}
return serial_numbers;
}
k4a::device device_open( const std::string serial_number )
{
constexpr int32_t length = 12;
if( serial_number.empty() || serial_number.length() != length ){
return k4a::device( nullptr );
}
const int32_t device_count = k4a::device::get_installed_count();
if( device_count == 0 ){
return k4a::device( nullptr );
}
for( int32_t device_index = 0; device_index < device_count; device_index++ ){
try{
k4a::device device = k4a::device::open( device_index );
if( serial_number == device.get_serialnum() ) return device;
device.close();
}
catch( const k4a::error& error ){
continue;
}
}
return k4a::device( nullptr );
}
}
#endif // __DEVICE_UTILITY__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment