Fun with Motorbee - Part 1

-Written on 2009-08-03 19:03:21 by BONES

This last weekend my brother was visiting, and we decided to have a look at programming for the motor-bee control adapter.
The Motor-Bee is a cool little controller board with 6 digital inputs and 4 outputs.On top of this, it can control 4 DC motors (5-24v) or one servo. Its a very groovy little hobby board.
The big trick for me, is that while a DLL is supplied by the manufacturers, there are only instructions on how to use it from C++ and VB and I want to be able to write my software in Delphi.
motorbee screenshot So we took a break from the beer, bunny-chow and video games to do a little coding.

I found it strange that while the DLL was available from the install cd, there was no download link on the site. Its a bit wierd for this day and age, but I'm sure they must have their reasons. Once you've located the DLL, (Mtb.Dll) and put it in your Delphi project folder (or the same location as your EXE) we can start writing some code.

With some help from my brother's C++ skillz, we managed to figure out how to load and initialize the DLL using C++ (C++ and VB are well covered in the included documentation) and then translated what we had learned to Delphi 7.
Here's a brief breakdown :

Step 1 : Initialization
To Initialise the unit, first we must tell delphi what kind of arguments the the InitMotorBee function in the DLL is expecting, we do this by declaring a custom type :

type
	TInitMotorBee = function():boolean ;stdcall;

As you can see, the InitMotorBee function takes no arguments and returns a Boolean if successfull. To Initialize, we simply create a variable of the type TInitMotorBee and pass the structure of the function to it with GetProcAddress. We can then instantiate a variable and do all sorts of cool error checking.(I have noticed that IE sometimes breakes the formatting of the highlighted code, if you have this problem, please click the "View plain" link at the top of the section.)

var
procedure InitBee;
...

procedure InitBee;
var
	Handle:           THandle;
	InitMotorBee:      TInitMotorBee;
	i:Boolean;
begin
	// -- Load the DLL -- //
	Handle := LoadLibrary('mtb.DLL');
	if Handle <> 0 then begin
     		// -- Get the address of the InitMotorBee function from the DLL -- //	
     		@InitMotorBee := GetProcAddress(Handle, 'InitMotoBee');
     		if @InitMotorBee <> nil then begin
       			Form1.Memo1.Lines.Add('Found Proc address for InitMotorBee OK');
       			{ 	
				InitMotorBee passes back a boolean variable to indicate if
				Init was successfull of failed Note. For some reason, the DLL
				returns 0 if true and *something else* if false (An error number?)
				so we check for  NOT TRUE 
				}
       			i :=  InitMotorBee();
       			If not i then
         			Form1.Memo1.Lines.Add('Initialisation was successfull ')
       			else
         			Form1.Memo1.Lines.Add('Initialisation was not successfull ');
     		end else
      			Form1.Memo1.Lines.Add('Could not find proc address FOR InitMotorBee');
	end else
  		Form1.Memo1.Lines.Add('Could not load Mtb.DLL'); 
end;
Part 2 - Get yer motors runin' !!