-- HOME --

Using the Lego RCX ("Mindstorms") with Visual Basic ... 

Lego's first Software Development Kit (SDK) was based on SPIRIT.OCX. It is relatively easy to incorporate functions from SPIRIT.OCX into Visual Basic (VB). SDK1 has several examples that outline the process.

SDK2 requires more direct control of the actual communication process and the function calls. 

The programmer must provide very low-level control when using the SDK2 in VB.

This page outlines a different approach ...


The Lego "RCX - Yellow Brick"

One alternative to using the Lego SDK2 is to use the MSWBRICK DLLs originally developed for MSW Logo. This approach by-passes all of Lego's software and addresses the RCX directly using Daniel Berger's C++ code. See here for details about the MSWBRICK DLLs.

NOTE: You need to download Lego's FIRMWARE to the RCX before Daniel's code (and the MSWBRICK DLLs) will work.

How to access DLLs in Visual Basic is also discussed here.

STEP 1     Download the appropriate MSWBRICK DLL to C:\WINDOWS\SYSTEM32

STEP 2     Start a new VB Project and add a new Module from the Project Menu.

STEP 3      Insert code into the Module as outlined below ...

STEP 4       Add Buttons and VB code as outlined below ...

WARNING: This code has NO error trapping. If there is a communication error between the Tower and the RCX, the code will 'hang' and you will probably crash VB. You will lose any unsaved work.

Save your work BEFORE you run the code!

 

Reading Input

Reading the RCX Sensors is a little more complicated. It is even more important to save your work before running the code. You can expect to 'crash' quite frequently while you are working out your code. The RCX is unforgiving. If you 'call' a function at the wrong time VB will crash and you will lose unsaved work.

' The following code is entered into the Module section ...

Public Declare Sub Sensor1On Lib "mswbrick2.dll" ()
Public Declare Sub Sensor1Off Lib "mswbrick2.dll" ()
Public Declare Function Sensor1Value Lib "mswbrick2.dll" () As Integer

 

' This is entered into the normal VB code window. ...

Dim MyValue As Integer
' The variable MyValue is defined in the "General" section at the top of the code window.

Private Sub Command5_Click()
   Call Sensor1On
End Sub

Private Sub Command6_Click()
   MyValue = Sensor1Value
End Sub

Private Sub Command7_Click()
   Call Sensor1Off
   Label1.Caption = MyValue
End Sub

For more about reading Input with VB see here.

Comments:

Writing and accessing DLLs may appear a little daunting at first, but it is all quite logical. Daniel Berger's code is well laid out. I suggest that you take the time to understand the techniques used in developing the MSWBRICK.DLLs. It will certainly clarify using the DLLs in VB. If you have access to MS Visual C++, you should experiment with writing some DLLs for yourself. It is not all that difficult.

This approach will presumably also work with Borland Delphi.