Error message: "Another process is already using yAPI"

Error message: "Another process is already using yAPI"

Yoctopuce support quite often receives calls for help from customers somewhat lost when they discover this error message: "Another process is already using yAPI". This issue is discussed in the documentation of Yoctopuce products but, this week, we are going to take the time to understand where this error message is coming from and how to avoid it.




The Yoctopuce API

In almost all programming languages supported by the Yoctopuce API, there are two ways to access the Yoctopuce modules:

Native access

In this mode, the API accesses the USB modules by talking directly to the part of the operating system managing USB. The typical example is a software managing Yoctopuce modules connected directly to the USB ports on the machine which is running this software. It's the most frequent and most intuitive case.

Network access

In network mode, the API uses Ethernet to contact an intermediary. This intermediary then talks directly to the Yoctopuce modules. This intermediary can be a Yoctopuce hub, such as the YoctoHub-Ethernet, or a VirtualHub.

From a programming standpoint, the only difference between code working in native mode and this same code in network mode is the call to RegisterHub. If the first parameter is "usb", access is in native mode. If it's an IP address, access is in network mode.

YAPI.RegisterHub("usb",errmsg);  // USB/native mode

YAPI.RegisterHub("192.168.1.3",errmsg);  // network mode


You can therefore very easily convert a program working in native mode into a program working in network mode.

Limitation of the Yoctopuce API

The Yoctopuce API suffers from a small limitation: on the same machine, you can have only one application at a time which accesses the Yoctopuce modules in native mode. This limitation is linked to the fact that two distinct processes cannot talk at the same time to a USB device. Generally, drivers take care of this type of problem. This avoids having several processes fighting for the same device. But as you have probably noticed it, Yoctopuce products don't use drivers. Therefore, the first process which manages to access the native mode keeps it for itself until a call to UnregisterHub or FreeApi.

Rather logically, the VirtualHub is an application which accesses the modules in native mode. This explains that when the VirtualHub is running, you cannot launch an application working in native mode under the penalty of obtaining the famous "Another process is already using yAPI" error message. Note that in the latest versions of the API, the error message also gives you the number of the process which monopolizes the API. This allows you to easily find the guilty party.

Working around this limitation

Obviously, this raises problems when you want to run several Yoctopuce applications on the same machine, even if they are completely independent from one another and access distinct modules. Fortunately, it's quite easy to work around this limitation. There are even several solutions:

  • If your application doesn't need a permanent access to the modules, you can settle for a call to RegisterHub("usb"), even if you need to try it several times. When you have obtained native access, do whatever you need with the modules, then free the API as soon as possible. But nothing guarantees that the other applications will show the same manners.
  • We have seen that the VirtualHub, among other things, provides access to Yoctopuce modules through the network. Nothing prevents you from running a VirtualHub on you machine and from using it to access your modules. The local network address of your own machine is always 127.0.0.1. Thus, a simple RegisterHub("127.0.0.1") solves this issue. As there is no limitation on the network mode, you can have several applications accessing your Yoctopuce modules at the same time, as long as they use a VirtualHub, which behaves more or less like a driver.
  • You can also use a slightly more subtle approach: you can try to detect if a VirtualHub is available with TestHub("127.0.0.1") and use it if there is one. If you don't detect any VirtualHub, you can then fall back on the native API:

    string errmsg ="";
    if (YAPI.TestHub("127.0.0.1",100, ref errmsg) == YAPI.SUCCESS) {
        YAPI.RegisterHub("127.0.0.1", ref errmsg)
    } else if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS) {
        MessageBox.Show(errmsg);
    }



Note that you can install the VirtualHub as a service/deamon, which allows it to start at the same time as the machine and to be available at all times.

The API in command line

Without any particular precaution, the API in command line works in native mode. Thus, if you want to know the list of your modules you type:

c:\>ymodule inventory


If a VirtualHub is already running, this won't work. But you can then ask the API to use the network mode with the -r flag.

c:\>ymodule -r 127.0.0.1 inventory



Conclusion

You cannot run two processes which use Yoctopuce modules in native mode at the same time, but you can work around this limitation by using a VirtualHub and the network mode. It's a matter of a single line of code. If you write applications working with Yoctopuce modules, we therefore recommend that you make sure that they can work with either mode: the effort on your part is negligible and your users will thank you.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.