Yoctopuce and the Mono framework

Yoctopuce and the Mono framework

Some time ago, someone asked us if we had planned a Yoctopuce library for the Mono framework. We didn't have such plans, however we promised to have a look at it. Let's do that together, shall we ?

Mono is an open-source implementation of C# and it is compatible with C# .NET. "What's the point?" you may ask, since Microsoft gives C# .NET for free. Well, there is a point: applications compiled with Mono can run on Windows, as well as Linux and Mac OS X. So, can we make the C# Yoctopuce library work on Linux or Mac OS X ?

The Yoctopuce API is made of two layers: the high-level API, which handles device "abstract" features such as Temperature, Voltage, Relay etc. and the low-level API which handles the communications with the hardware.

The high-level API is always written using only the target language, which is C# in our case. No fancy system calls are made in the high-level API, so the odds are it will work on any platform. The low-level API is written in C, and is available in multiple formats: source files, pre-compiled libraries, static or dynamic.

The Yoctopuce low level API is available as a dynamic library
The Yoctopuce low level API is available as a dynamic library

The Yoctopuce library for C# is distributed with the yapi.dll library only, which makes sense since (until now) the C# API was targeted for Windows only.

It's quite easy to check if the Yoctopuce API works with Mono on Windows. For instance, if we want to test the Yocto-Temperature demo program, we only need to copy main.cs, yocto_api.cs,yocto_temperature.cs et yapi.dll in a folder, type gmcs *.cs and that's it. We get a working executable file. So Mono does seem to be compatible with C# .NET :-)

Obviously, the difficulty is to do the same with other operating systems. To make it simple, we need to tell Mono to load the library matching the operating system instead of yapi.dll. Luckily, it looks like this has been taken care of :-) Mono can use a little configuration file describing how to match a DLL with an equivalent dynamic library for the others operating systems.

<configuration> <dllmap dll="yapi.dll" os="linux" target="libyapi.so.1.0.1"/> <dllmap dll="yapi.dll" os="osx" target="libyapi.dylib"/> </configuration>

So, to make the C# Yoctopuce API work with the OS of your choice, you need to retrieve the dynamic library for your OS (you can find it in the C++ API), copy the contents of the configuration file above in a file named executable_name.config and that's it.

Yocto-Temperature example program compiled with Mono on Linux
Yocto-Temperature example program compiled with Mono on Linux

To be completely honest, we had to make some minor modifications on the high-level API to make it work with Mono. One last word: if you planned to use Mono on a Raspberry Pi, you might be a bit disappointed. Mono does not support the HardFloat architecture (HF) yet, so floating point calculus doesn't work very well. You will be able to control a Yoctopuce relay, but until Mono fixes this issue, reading temperature from a Yoctopuce sensor is off the mark.

1 - robby Thursday,august 29,2013 16H19

Thanks for providing this -- you guys rock!

2 - nilse Saturday,january 11,2014 19H55

so, if you support mono - is there a chance to get your nice devices running on xamarin (= mono for ios & android)? that would be super awesome!

3 - martinm (Yocto-Team)Monday,january 13,2014 11H46

@Nilse: Actually, this is not that simple. We don't have any discrete version of the low level library on Android nor iOS. Moreover, programming paradigms on handheld devices are a bit different.

4 - doctordee Tuesday,october 13,2015 6H57

Has anyone managed to get this working with Xamarin on Android?

5 - seb (Yocto-Team)Tuesday,october 13,2015 7H32

@doctordee Sadly no. We are working on a solution but it will only work for TCP connection but it will not support USB connection.

The solution that has been used by some customers is to write a "compagnon app" that is written in Java and that use our Android library. Xamarin is used do display datas and the "compagnon app" handle all the IO. The two application communicate using Intent and Broadcast Intent.

Yoctopuce, get your stuff connected.