matlabideas

All things MATLAB. And Simulink.

Using .NET Driver with Unsupported Hardware

MATLAB supports a huge arsenal of hardware accessing methods through its various toolboxes. However, there are still some hardware which are not directly supported by MATLAB. This happens when you are trying to access a custom hardware or a commercial hardware which is not commonly used along with MATLAB.

To overcome this, one of the easiest ways is to access the hardware via .NET [1]. Commercial hardware vendors will normally provide driver for user to communicate with their hardware and the .NET driver is one of the most common types of drivers provided.

To illustrate the usage of .NET driver, we choose USB to I2C controller from Total Phase [2]. This hardware let the user to have I2C communication with external I2C devices, and is not a supported hardware under MATLAB and its toolboxes. The hardware .NET driver is provided free of charge from the vendor’s website and comes with a number of example usage through programming language such as C#, C++ and VB.

So, the first step to try to use this hardware from MATLAB is obviously to download the .NET driver from the vendor. Specific to this vendor, the driver to be downloaded is named USB Drivers (Windows) and Aardvark Software API and Shared Library.

For the case of the USB to I2C controller, after downloading and installing the driver, we will find that there are 2 DLL (dynamic link libraries)- aardvark.dll and aardvark_net.dll. The first one is the native windows DLL while the latter is the corresponding .NET library. We will copy the native DLL to c:\windows\system32 . We will then place the .NET dll in the location which our MATLAB script is able to access.

The next step is to write the MATLAB script to access the .NET driver, or more correctly term as .NET assembly. As illustration, consider these lines of code:

function Speak(text)

NET.addAssembly(‘System.Speech’);

speak = System.Speech.Synthesis.SpeechSynthesizer;

speak.Volume =
100;

speak.Speak(text);

end

Here, the function Speak will call the .NET assembly System.Speech, and from there access the speech synthesizer class available in .NET Framework Version 3.0 and above. Try this code and you will notice that your PC will speak out the text that you pass in as variable to the function.

In similar manner, to access the functionality of the hardware via the .NET driver, we need to know what functions are provided via the assembly. Normally, we can refer to the API reference provided by the vendor itself and also the attached example. For our example hardware, via the attached C# example, we know that the .NET driver provides function such as aa_i2c_write to write data to a specific address, and aa_i2c_read to read data. The example script below show how to call some specific functions made available through the hardware .NET driver.

%%

try

NET.addAssembly(‘C:\matlab_files\i2c\totalphase\aardvark-api-windows-i686-v5.13\MATLAB\aardvark_net.dll’);

catch e

e.message


if(isa(e, ‘NET.NetException’))

e.ExceptionObject


end

end

obj=TotalPhase.AardvarkApi;

%%

port = 0;

bitrate = 100;

BUS_TIMEOUT = 150; % ms

%% Open the device

handle = obj.aa_open(port);


if (handle <= 0)

fprintf(‘Unable to open Aardvark device on port %d\n’,

port);

disp(obj.aa_status_string(handle));


return;


end

%%Ensure that the I2C subsystem is enabled

obj.aa_configure(handle, TotalPhase.AardvarkConfig.AA_CONFIG_SPI_I2C);

%% Enable the I2C bus pullup resistors (2.2k resistors).

% This command is only effective on v2.0 hardware or greater.

% The pullup resistors on the v1.02 hardware are enabled by default.

obj.aa_i2c_pullup(handle, obj.AA_I2C_PULLUP_BOTH);

%% Power the EEPROM using the Aardvark adapter’s power supply.


% This command is only effective on v2.0 hardware or greater.


% The power pins on the v1.02 hardware are not enabled by default.

obj.aa_target_power(handle,

obj.AA_TARGET_POWER_BOTH);

%% Set the bitrate

bitrate = obj.aa_i2c_bitrate(handle, bitrate);

fprintf(‘Bitrate set to %d kHz\n’, bitrate);

%% Set the bus lock timeout

bus_timeout = obj.aa_i2c_bus_timeout(handle, BUS_TIMEOUT);

fprintf(‘Bus lock timeout set to %d ms\n’, bus_timeout);

%% operation here

%put in other functions you want to do here

%% Close the device and exit

obj.aa_close(handle);

As you can see, although this hardware is not supported directly by MATLAB and its toolboxes, we can still make use of .NET driver to communicate and control it. Please note that .NET driver is not the only possible way. MATLAB can also communicate via the native driver but this topic is left for the reader to explore.

Reference

[1] The Microsoft .NET Framework is an integral Windows component that provides a large body of pre coded solutions to common program requirements, and manages the execution of programs written specifically for the Framework. Note: MATLAB software supports the .NET Framework on the Windows platform only

[2] http://www.totalphase.com/products/aardvark_i2cspi/

Comments are closed.