Day 11+12 – Connecting to my STK-500 board

So over the last two years my fellow students has been taught C and C++ and we got a driver to communicate to a STK-500 board where we could read/write stuff.
It has been ok – just one problem, we had to know which COM port we connected to (note: We used a COM-to-USB cable)… and solution has been to hard code it down. THIS SUCKS!

So I thought “What if I could just locate the USB I insert, check if it’s a STK-500 board and get the port”. So I went to Google:

It is hard to get help for this topic – damn!
This is written the day after I actually worked on this. And to find all my references now suddenly useful stuff appears on Google?!?

But here’s what I tried.
There’s a little something called WMI which can find (a lot of stuff but amoungst them) the connected devices:

void FindUSBDevices()
	ManagementObjectSearcher searcher =
        new ManagementObjectSearcher("root\\WMI",
        "SELECT * FROM MSSerial_PortName");

	foreach (ManagementObject queryObj in searcher.Get())
        Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
		Console.WriteLine("PortName: {0}", queryObj["PortName"]);

This is a nice little piece of code, that gives me the port name and name of the Instance – snatched from StackOverflow – it’s good, but… I need to have a thread running all the time to check for this.

I don’t think the OS has a thread running to say “yo – anything new?” all the time. It has an event and there’s an example here
That looks like this:

using System.Windows.Forms;

public class DeviceInsertionEvents : NativeWindow
	private const int WM_DEVICECHANGE = 0x0219;				// device state change
	private const int DBT_DEVICEARRIVAL = 0x8000;			// detected a new device
	private const int DBT_DEVTYP_COMSERIAL = 0x00000003;    // Port/SerialCom device
	protected override void WndProc(ref Message message)
		base.WndProc(ref message);

		if ((message.Msg == WM_DEVICECHANGE) && (message.LParam != IntPtr.Zero))
			DEV_BROADCAST_STK500 volume = (DEV_BROADCAST_STK500)Marshal.PtrToStructure(
				message.LParam, typeof(DEV_BROADCAST_STK500));

			if (volume.dbcv_devicetype == DBT_DEVTYP_COMSERIAL)
				switch (message.WParam.ToInt32())
						SignalDeviceChange(StkStateChange.Added, volume);

						SignalDeviceChange(StkStateChange.Removed, volume);

This little chunk of code will each time something hardware related happens call the WndProc(ref Message message). Read more about that here.

Ofcause it should do what it normally does and then it should check if
1) it’s a device that has changed
2) it’s a Serial/port device

When all this is checked we have A DEVICE. It’s not necessarily what I am looking for – it could be a mouse or a keyboard.
But something has happened and we call SignalDeviceChange(…):

	private void SignalDeviceChange(StkStateChange state, DEV_BROADCAST_STK500 volume)
		string name = ToUnitName(volume.dbcv_unitmask);

		var board = new StkBoard(name, volume.dbcv_devicetype, state);


public class StkBoard
	public StkBoard(string name, int port, StkStateChange state)
		Name = name;
		ComPort = port;
		State = state;

	public StkStateChange State { get; set; }
	public string Name { get; private set; }
	public int ComPort { get; private set; }

This creates a new StkBoard (even if it’s not a real StkBoard) and publish a new event with the data.

Validating the data is not a complicated task but will first be done on Day 13.

2 thoughts on “Day 11+12 – Connecting to my STK-500 board

  1. А есть еще что то похожее на эту тему?

    Google Translate: “Are there anything similar on this subject?”

    • Hey Gype

      Yes, but there’s a problem, which I did not know of when I started.
      What your computer actually sees is a small chip inside the COM-plug’s end. It’s a small converter, which translate the USB input/output to Serial input/output. THAT, is what you get on your computer.
      You will therefor never see the STK500 kit.

      But what you might do, is say “this cable is always the one I use for my kit – therefore that ID I receive is my kit”.

      But I haven’t worked more on it, sorry.

Leave a Reply

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

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=""> <s> <strike> <strong>