You are here: Home Documentation Tutorials .NET C#
Document Actions

.NET C#

Note: Return to tutorial view.

How to communicate with the Make Controller from with Microsoft's .NET C# environment.

Overview

How the Make Controller communicates with .NET C# apps, and a list of required downloads.

The Make Controller Kit can extend your .NET C# applications beyond the desktop and into the real world, interfacing to sensors, motors, lights and many other physical devices.

MakingThings has created a .NET C# library that makes it easy to connect the board to your apps via Ethernet. Communication with the board uses OSC, a popular protocol used to communicate between otherwise disparate environments and devices. This guide will walk through the basics of working with the Make Controller Kit, and how to integrate it into some simple example apps.

The .NET library reference will be an important resource when creating your own projects.


Downloads

The first step is to set up your environment. Download the Make Controller .NET assembly here, and unzip it. The dotnet directory has several relevant items in it:

  • MakeControllerOSC - the library.
  • Some example projects:
    • CPUMonitor - an app that turns a servo to display the current load on your CPU.
    • DeskTimer - an app that monitors your presence at your desk, and keeps track of how long you're there.
    • MCTest - an app that allows you to send OSC messages to the board via Ethernet or USB at a command line.
    • QuickTest - the simplest app that shows how to send and receive messages from within .NET
  • dotnet.sln - a project file which includes each of these assemblies.

It's a good idea to make sure you can build the project as you downloaded it before moving onto any code of your own. Next, we'll setup the board so that we can start reading and writing to it.

LED Control

We'll start with a simple example of how to control an LED on the Make Controller Kit.

Controlling an LED on the Application Board is one of the simplest things to do from .NET, making it an excellent first thing to try. We'll work with the code from the included QuickTest app for this example. QuickTest as its provided uses the USB connection to the board, but you can uncomment the Ethernet code to experiment with that as well.

Quick Test
The QuickTest UI


 

The name of the LED subsystem on the board is appled - Application LED. There are 4 LEDs, numbered 0 - 3, and they have a state - whether they're on or off. The argument that comes after the address sets the value of state. So the OSC message we create will be in the form /subsystem/device/property argument

A simple way to send the message is to create a UI form with a checkbox. Then, in the routine that gets called when the checkbox is changed, send out an OSC message. Creating forms and attaching events to them is beyond the scope of this tutorial - check out the Microsoft Dev Network, and click on C# in the left hand column for more info.

First of all, there's some setup to take care of in the constructor - namely, the network settings for talking to our board over the network. We'll also add a variable to hold the state of our LED.

public QuickTest()
{
// ...other initialization routines...
usbPacket = new UsbPacket();
usbPacket.Open();
osc = new Osc(usbPacket);
}
// this gets called when the checkbox changes
Led0_CheckedChanged( object sender, EventArgs e )
{
LedSet(0, Led0.Checked);
}

// then call LedSet() to create and send the message to the LED
LedSet(int index, bool value)
{
OscMessage oscMS = new OscMessage();
oscMS.Address = "/appled/" + index.ToString() + "/state";
oscMS.Values.Add(value?1:0);
oscUdp.Send(oscMS);
}

Explanation

When we get called from Led0_CheckedChanged(), we in turn call LedSet() with the device number of the LED (0 in this case), and the current value of the checkbox - Led0.Checked. In LedSet() we make a new OscMessage, and set its address to the OSC address we discussed earlier - /appled/0/state, add a value of either 1 or 0 for the state, and send it off.

Analog Inputs

Another example, this time on how to read values back from the board.

To read an analog input, send an OSC message to ask for the current value, and the board will send an OSC message back with the value. Unlike the OSC messages sent to the LEDs, an OSC message asking to read a value will not have an argument on the end of it. This is what distinguishes a read message from a write message - the lack of an argument.

The name of the analog input subsystem is analogin, there are 8 devices numbered 0 - 7, and we're interested in reading their value. 'Read' messages have the form /subsystem/device/property, with no argument. If you're monitoring a single value, it's convenient to use the SetAddressHandler( ) routine - you can pass it the address of a device on the board you're waiting for, and it will call a given method back with the value when it arrives. Include this in your constructor:

public QuickTest()
{
// ...other initialization routines...
osc.SetAddressHandler("/analogin/7/value", TrimPotReading);
}
void TrimPotReading( OscMessage oscM )
{
int value = (int)oscM.Values[0];
SetIndicator( value );
}

Explanation

In the constructor, we set the TrimPotReading( ) method to handle all the returning messages with an address of "/analogin/7/value". Then in the TrimPotReading( ) method, we take the value from the incoming message and use it to update the Indicator in the UI.

Sample Apps

A rundown of the sample apps included in the .NET C# download.

Three samples apps are included in the dotnet directory you downloaded. Use the code as a jumping off point for your own projects.

CPUMonitor - a servo motor moves an indicator to show how busy your machine is. The application uses the System.Diagnostics PerformanceCounter class to monitor CPU usage.
CPU Monitor
The dial is turned by the servo motor.

DeskTimer - a distance measuring sensor points out from the monitor on your computer and, if it finds you in front of it begins counting how long you're at your desk. If you go away, the work timer stops and the away counter begins.

Desk Timer
The distance measuring sensor monitors the workstation.

MCTest - a .NET application similar to mchelper that allows users to send OSC messages to the Make Controller Kit over USB and Ethernet from a command line. Great for testing and debugging the board, and familiarizing yourself with the OSC protocol that it runs.

MC Test

The source for each of these projects is included in the download, and should serve as a good jumping off point for your own applications.

Note about .NET USB communication

The MakingThings .NET library currently contains a USB workaround.

At the time the .NET library was originally written, there was an issue with the USB driver that did not allow for asynchronous reading and writing, and we included a workaround for this.  It has since been reported that the USB portions of the code do not work with current versions of the .NET framework. 

It may now be possible to use the standard USB driver instead of the workaround in the MakingThings library.  Another alternative, of course, is to use the Ethernet interface to the Make Controller.  If anybody has luck
with the standard driver, please contact us and we can integrate the changes into the library.