Device Inventory vs Plug And Play

Device Inventory vs Plug And Play

If you have to code a Yoctopuce devices inventory, you would probably want to explicitly build a list using the appropriate API functions. But this is not the most convenient method if you wish to handle devices plug/unplug events during the application life.



How to keep a device list up to date?
How to keep a device list up to date?


Indeed, the simplest method only requires the use of FirstModule and nextModule functions. The following example, written in C# would build a devices list and store it in a dictionary indexed by serial numbers.


string errmsg = "";
Dictionary<string, YModule> modules = new Dictionary<string, YModule>();

YAPI.RegisterHub("usb", ref errmsg); // API init

YModule m = YModule.FirstModule();
while (m != null)
  {  modules.Add(m.get_serialNumber(), m);
     m = m.nextModule();
  }
 



The previous example is very simple, but it can turn into a complex piece of code if you want to handle devices plug / unplug. You would have to perform new inventories on a regular basis, find out which devices are new and which ones have left, then update your device list.

Well, the Yoctopuce API provides a very interesting feature: it can call a callback function each time a device is plugged or unplugged. This callback's only parameter is the related module. So you only have to add or remove the device from the list each time one of these callbacks are called. Then you will be sure your device list is always up to date. These callbacks can be configured with the function RegisterDeviceArrivalCallback and RegisterDeviceRemovalCallback.

Callback programming model is fun and often quite effective, but there is a catch: callbacks can be called at any moment, including when the application is not ready to process them. This can lead to dead locks, usually painful to reproduce and fix. To avoid this, the arrival/removal callback are called only when the UpdateDeviceList is called. You only need to call this function from a timer or a parallel thread to have full control on the callbacks triggering.

Here is an example, written in C#, using callbacks to continually count how many devices are connected.


static Dictionary<string, YModule> modules = new Dictionary<string, YModule>();

static void deviceArrival(YModule m)
  {
     modules.Add(m.get_serialNumber(), m);
  }

static void deviceRemoval(YModule m)
  {
     modules.Remove(m.get_serialNumber());
  }
 
static void Main(string[] args)
  {
    string errmsg = "";

    YAPI.RegisterHub("usb", ref errmsg);  // API init
    YAPI.RegisterDeviceArrivalCallback(deviceArrival); // callbacks init
    YAPI.RegisterDeviceRemovalCallback(deviceRemoval);
   
    while (true)
      {
        YAPI.UpdateDeviceList(ref errmsg);
        Console.WriteLine(modules.Count.ToString()+ " connected devices");
        YAPI.Sleep(1000,ref errmsg); // wait for 1 sec
      }
  }
 



For your information, when UpdateDeviceList is called for the first time, deviceArrival callback will be invoked for each of the already connected devices. You won't have to make a classic inventory at program startup.

The program parts shown here do not perform any error handling, do not copy/paste them as is in a production application.

Add a comment No comment yet
Back to blog












Yoctopuce, get your stuff connected.