Using Mono on the PhidgetSBC2

So this Monday my PhidgetSBC2 arrived and I was super excited to get started playing around with the short-term goal of being able to open my door lock remotely.

I really wanted to write the code in C# because it's simply better than Java and it's lot easier to implement a server in C# than in C. While the manual for the SBC2 says you can get Mono working if you install the correct packages, I couldn't find any information on the Phidget website on how to do it, so I had to figure it out myself.

Getting Started

The first thing you need to do is plug your SBC2 in and bring up the configuration website. I won't do into detail about this because it is covered well in the manual. Once you are up and running you need to and navigate to the Network tab then the Settings tab and under SSH Server check the Enabled radio button and click Save Changes. This will take a while but eventually it will be enabled and we can move on.

SSHing into the SBC2

Unix

You should already have a ssh client installed but if you don't, you will have to install one from your system's package manager.

If you have changed the hostname of your SBC2, you will have to change the value here.

ssh root@phidgetsbc.local

Windows

I highly recommend installing PuTTY. Once installed, start it up and enter root@phdigetsbc.local in the Host Name field (if you changed the hostname, you will need to change the value here).

PuTTY Settings

Installing Mono

First you will need to go back to the configuration website and navigate to the System tab and then the Packages tab and check the Include full Debian Package Repository checkbox and click Save Changes.

If you wish to skip the remainder of the installation, I have written a installer script that will automate it. Head to the Quick Start section to run it.

Now back to the ssh terminal. We're going to update the list of packages and then install the mono package. Ideally we would install mono-complete but there is currently a conflict between mono-complete in debian's repository and some core packages from emdebian's repository so we'll have to settle for mono-runtime and mono-gmcs.

apt-get update
apt-get install -y mono-runtime mono-gmcs

Once Mono is installed, we can move on.

Hello World

While being a bit cliché, we need to know if Mono is working before we can get started.

cat <<-END > HelloWorld.cs
using System;
public class HelloWorld
{
  static public void Main()
  {
    Console.WriteLine("Hello World");
  }
}
END

Now we can compile and run our hello world application.

gmcs HelloWorld.cs && mono HelloWorld.exe

And if there are no problems, you should see Hello World in your terminal.

Getting the Library

First we need to install the Phidget library.

apt-get install -y libphidget21-dev

And then get the Phidgets C# library wrapper from the Phidgets driver page (if that page has a version newer than 2.1.7.20110203 and you want to use it, you will need to update the url and filename used in unzip command).

apt-get install -y wget unzip

Once wget and unzip are installed, we can download the library and extract the dll.

mkdir temp && cd temp
wget http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.7.20110203.zip
unzip -p Phidget21-windevel_2.1.7.20110203.zip
phidget21-windevel/Phidget21.NET.dll > ../Phidget21.NET.dll
cd .. && rm -r temp

Alternatively you can download the dll directly from me (version 2.1.7.20110203).

wget http://samuel.kadolph.com/wp-content/uploads/2011/02/Phidget21.NET.dll

Using Phidgets with Mono

With the library and wrapper available, we can now write a program to control our Phidgets in C#. This is just a simple example to test that it works.

cat <<-END > Example.cs
using System;
using Phidgets;

public class Example
{
  public static void Main()
  {
    var interfaceKit = new InterfaceKit();

    try
    {
      interfaceKit.open();

      Console.WriteLine("Waiting for InterfaceKit to be attached...");
      interfaceKit.waitForAttachment();

      Console.WriteLine("InterfaceKit attached: serial={0}",
        interfaceKit.SerialNumber);
    }
    finally { interfaceKit.close(); }
  }
}
END

And now compile and run.

gmcs Example.cs -r:Phidget21.NET.dll && mono Example.exe

And if everything is work you should see something like this.

Waiting for InterfaceKit to be attached...
InterfaceKit attached: serial=47521

Now get out there and start having some fun with C# & Mono and your Phidgets!

Optional: GAC Installation

If you don't want to have to specify a relative path to the library dll or keep it in your current directory, you can install it to the GAC and add it to the lib directory that gmcs searches for assemblies.

gacutil -i Phidget21.NET.dll
ln -s /usr/lib/mono/gac/Phidget21.NET/*/* /usr/lib/mono/2.0/Phidget21.NET.dll

Quick Start

I've written a script to automate the installation of Mono and the Phidget C# library. Run this command in the ssh terminal after enabling the debian packages.

apt-get update && 
apt-get install -y curl && 
bash < <( curl -L http://bit.ly/phidgetsbc2-mono-installer )

Once that is done head back up to Using Phidgets with Mono to continue.

Join the conversation

2 Comments

  1. Really a great job. I do appreciate. I will order my new 1072 and start trying mono.
    Alex

  2. got my phidgets working thanks to your great article now I am trying to get a vb.net application working in mono and I have a problem so your code
    using Phidgets works
    but my Imports Phidgets doesn’t got any idea
    Imports Phidgets
    the import “phidgets” could not be found. (VBNC40056)

Leave a comment

Your email address will not be published. Required fields are marked *