TOP

Is it just me, or is it hot in here?

Let me introduce you to the quickest and easiest way to start reading sensory data from your Phidgets Interface Kit. In the name of the title, we’re going to make an application that allows us to read the temperature. I own an PhidgetsTextLCD board which is two circuitry boards in one, you need to get your own. This is a fairly simple introduction, so DON'T PANIC.

1203

The first thing you need to do is obviously get yourself one of the interface kits from Phidgets. If you’re lucky and you already have a card, please read on.

Preperations

The very first thing you need to do before you connect the interface kit to your computer, is to download the drivers and software from Phidgets. Go to the following link and download the “Phidgets 21 MSI”.

After installation of the software, you should see a new icon in the tray bar on your start menu. The icon is one with orange letters that says Ph. You can double-click this icon to open up the control panel. Do this now to validate that you’re computer has recognized the interface kit correctly.

Phidget Control Panel

Now you can double-click the interface 8/8/8 item and you should see a debug window that allows you to analyze the values received from your interface kit. If you have no sensors attached to your kit, the values will obviously be 0 for everything. My example screenshot below shows values from 3 different analog sensors, it’s an Sharp infrared sensor, temperature sensor and the third is a touch sensor. Use this dialog to validate that the values changes when you manipulate your sensors.

InterfaceKit-full 

Next step in the process is to obtain an tool to do your programming. My programming language of choice is C#, but if you’re more familiar with others, there are a bunch of examples available on the Phidgets website.

The primary editor for programming C# on Windows is of course Microsoft Visual Studio. For the purpose of this exercise I’m going to use free version that you can download yourself.

Configurations

Now it’s time to have a look at the code we need to write that allows us to read the sensory input. First item of action is to create a new project in Visual Studio. We’ll make a Windows Forms project and call it Mostly Harmless. Now you can add a reference to the Phidget21.NET.dll.

If out of coincidence you are using 64-bit version of Windows, there are two important differences from those still running the older, outdated (and potentially a target for the Vorgons) 32-bit edition. They are as followed:

32-bit path: C:\Program Files (x86)\Phidgets\Phidget21.NET.dll
64-bit path: C:\Program Files (x86)\Phidgets\Phidget21.NET.dll

If you are using 64-bit version of Windows and Visual Studio Express, you can begin to panic just about now.

Next important difference is that you need to ensure your application is running as a 32-bit process. If you don’t do this step, you won’t be able to run it. As it happens, Microsoft did the silly mistake of removing the target option from Visual Studio Express. This means you need to get your hands dirty by editing the Mostly Harmless.csproj file using Notepad – only if you happen to be running a 64-bit version of Windows.

Start by saving everything within Visual Studio and close the tool. Open Notepad and locate your .csproj file. Search for the word “WarningLevel” and you should find two PropertyGroup sections where you need to add an extra element.

Add the following element below the WarningLevel elements within your project file:

<PlatformTarget>x86</PlatformTarget>

For those fortunate and lucky with Visual Studio Standard or better, you can set the build target from the project properties.

Build_Settings 

That was probably not the oddest thing in the universe, but let’s hope Microsoft makes it easier for those using new technology in the future (consider that a Visual Studio SP2 feature request).

Coding

It’s not without coincidence that the first thing you should do now is not coding, but you can start by designing yourself a modern and good looking interface. Drag a Label control onto the surface of your main form. I named it labelDegrees for further reference. If you want a more fancy UI, you could have a status label that indicates the current connection state of the kit.

Let’s start coding! Go into source view from the form.

First, add a using statement to the top of your source file:

using Phidgets;

Then add a property to your Form class:

public InterfaceKit Kit { get; set; }

Within your forms constructor, add the following lines with code:

public Form1(){    InitializeComponent();    Kit = new InterfaceKit();    // The event handler for the analog sensor.    Kit.SensorChange += new Phidgets.Events.SensorChangeEventHandler(Kit_SensorChange);    // Open connetion to the Phidgets kit.    Kit.open();}

Finally at the end, we add our code to update the label on the form to display the correct sensor value. Remember the Control Panel I mentioned earlier? From that dialog, you should check what index your temperature (or any other sensors you have) sensor is located at. You can see the same index number on the interface kit, so it’s very easy to find the correct index that you need for the next few lines of code.

void Kit_SensorChange(object sender, Phidgets.Events.SensorChangeEventArgs e){    if (e.Index == 1)        labelDegrees.Text = ((e.Value - 200) / 4) + "°";}

And there you have it, your very own computer based temperature gauge! I said don’t panic, it’s actually that easy! I leave it to you to come up with some fancy UI.

Mostly Harmless

Notes

Some final notes about this project.

It’s important to notice that there can be only one application connected to the interface kit at any time. While your application has a connection to the kit, you can’t use the debug tools in the Phidget Control Panel. This works both ways, so if you have the interface kit debug dialog open, your application will not receive any sensor values.

There are additional events on the InterfaceKit object that you can listen to, some important ones are Attach, Detach and Error. These can be useful to update your UI.

When I first started working on this example, I had some code that tried to clean up after the form was closing, by calling the Kit.close method. After some testing, this seem to hang the application a lot of the time, so I removed it. There seem to be no problem allowing the .NET framework to do it’s garbage collection like it should.

I hope you had fun reading this quick introduction to Phidgets communication. Now go and read a book!

Leave a Reply

Your email is never published nor shared.

You may use these HTML tags and attributes:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>