VBB DEV CSharpExpress2008

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Creating a VBB Component with C# Express 2008

C# Express 2008 is the freely available power entry level development tool from Microsoft. Here we use the C# language in C# Express 2008 to create the Lamp component for VBB3. It is assumed you are somewhat familiar with C# Express and VBB3.

Step 1: Create a C# Class Library Project


Your VBB components will reside in a class library which will be instantiated by VBB3 using reflection

Step 2: Add VirtualBreadboard References


Add references to the IOpenVBB.DLL and OpenVBBLib.DLL contained in the <VBB Install Directory> Normally this is in the C:\VBB3 directory

Step 3: Rename the default class to MyLamp and enter the following code
What this code actually does will be explained in a future tutorial. First we just want to see how we can build it and plug it into C#
using com.virtualbreadboard.interfaces; using com.virtualbreadboard.sim; using com.virtualbreadboard.graphics.svg; namespace MyVBBComponent { public class MyLamp : OpenVBBComponent { IopenVBBSVGFrame myLampON; IopenVBBSVGFrame myLampOff; IopenVBBXMLDOMNode myPinout; IopenVBBVoltage myInputPin; public override void initSVGRenderer() { IopenVBBSVG svg = getSVG(); myPinout = svg.createGroup(); myPinout.appendChild(svg.createRectangle(0, 0, 5 * SVGDefs.GRIDSIZE, 5 * SVGDefs.GRIDSIZE, "black", "black")); myPinout.appendChild(svg.createPinCircle(2.5F * SVGDefs.GRIDSIZE, 6.0F * SVGDefs.GRIDSIZE, 2, 1)); svg.setActiveGraphic(myPinout); myLampON = svg.createFrame(); myLampON.append(svg.createCircle(2.5F * SVGDefs.GRIDSIZE, 2.5F * SVGDefs.GRIDSIZE, 2 * SVGDefs.GRIDSIZE, "red")); myLampON.setVisible(false); myLampOff = svg.createFrame(); myLampOff.append(svg.createCircle(2.5F * SVGDefs.GRIDSIZE, 2.5F * SVGDefs.GRIDSIZE, 2 * SVGDefs.GRIDSIZE, "darkred")); myLampOff.setVisible(true); } public override void wirePins(IopenVBBPortInterface portInterface, IopenVBBWiringErrors errors) { myInputPin = portInterface.getPin(1); myInputPin.RegisterAsVoltageSink(this); } public override int getPinCount() { return 1; }

public override void sweepPins(double elapsedTime) { myLampON.setVisible(myInputPin.isHigh()); myLampOff.setVisible(!myInputPin.isHigh()); } } }

Step 4: Save the project and configure the target .net runtime
Next we need to configure the target .net platform. This is needed because VBB3 is currently being developed using VS2005 and the .NET 2.0 runtime. C# Express 2008 presently defaults to .NET 2.5 runtime but it does allow you to target earlier platforms. However before you can target a different platform C# Express requires you to save the project. I Saved the project as MyVBBComponent and C# Express placed this automatically in the default development directory which for me on Vista is C:\Users\James\Documents\Visual Studio 2008\Projects\MyVBBComponent\MyVBBComponent\bin\Debug Keep note of this directory as we will use it later. Now that the project is saved you can configure the runtime. Select the project properties and select the target framework as .NET Framework 2.0

Step 4: Build the Project and Note the DLL Location


When you build a class library project you will generate a DLL file by the same name as your project by default. First you need to save your project. For example, when I build saved and build this MyVBBComponent project on Vista it generated the following .dll C:\Users\James\Documents\Visual Studio 2008\Projects\MyVBBComponent\MyVBBComponent\bin\Debug\ MyVBBComponent.dll

Step 5: Edit the User.xml


Next we need to tell VBB where to find your new component. This is done via the plugin XML documents. There is one document User.xml ready for use with your own components. Edit the document <VBBInstallDir>/skin/vbb/User.xml Update the <CLASS> tag with the name of your component not forgetting to prepend the project namespace to the component. Also update the <value> tag of the assembly to point to the .dll you built in step 4
<VISIJCONFIG> <MUVIUM-LIB name="User" sheetType="Breadboard" /> <TOOLBOX> <BUTTON> <ICON>User</ICON> <DESCRIPTION>User defined lamp</DESCRIPTION> <CLASS>MyVBBComponent.MyLamp</CLASS> <properties> <property> <name>assembly</name> <value> C:\Users\James\Documents\Visual Studio 2008\Projects\MyVBBComponent\MyVBBComponent\bin\Debug\ MyVBBComponent.dll</value> <description /> </property> </properties> </BUTTON> </TOOLBOX> </VISIJCONFIG>

Step 6: Locating your Component in VBB


Run VirtualBreadboard and create a new VBB Project. Your component is located in the User Toolbox

Step 7: Testing your component


You can test out your new component by placing it on a breadboard and wiring it up. A simple test is to connect it to a DIP power component.

Running the breadboard you can select the DIP high to power the light.

Using C# Express in debug mode


You can also configure C# Express to debug your component using the full power of Visual Studio including dynamic hotcode development. C# Express does not allow you to launch a component library directly. Instead we first need to add a separate launcher project. First right click on the solution and Add a New Project

Next select a Windows Form Application Project and call it VBBLauncher or similar

Step 1: Configuring the VBBLauncher project


There are a couple of things we need to do to configure the new project in no particular order Set the VBBLauncher as the default startup project Set the .net runtime target as .net 2.0 Add a reference to the MyVBBComponent library Deselect the loaderlock in the Exceptions manager Remove unneeded references

Set the VBBLauncher as the default startup project

From the VBBLauncher application right click and choose Set as Startup Project option. This configures the solution to launch the VBBLancher application on startup.

Set the .net runtime target as .net 2.0


Select the project VBBLauncher properties and select the .NET Framework 2.0

Add a reference to the MyVBBComponent library


Add a reference to the MyVBBComponent library by right licking on the references of the VBBLauncher application and selecting Add reference. From the Projects Tab select the MyVBBComponent and click OK.

Disable the LoaderLock in the Managed Debugging Assistant


There is a known work around for some problems with the Managed directX

Remove unneeded references


There are some references and classes added by C# Express by default for use with the .net 3.5 platform such as the System.Linq; Remove these references if it interferes with your build

Step 2: Add the launcher code to the default Form


Now the project is configured you just need to add a launch button to the default form. Add a button on the default form called Launch VBB and double click the button to enter the startup code.

Enter the following code


using using using using using using System; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms;

namespace VBBLauncher { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { AppDomain vbb = AppDomain.CreateDomain("VBB"); vbb.ExecuteAssembly("C:\\VBB3\\VirtualBreadboard.exe"); } } }

Then you can set debug breakpoints in your component and you are ready to launch

Start the application and you should see the Launch VBB Button. Click this to launch VBB and begin the debugging session. VirtualBreadboard will launch and when your component is placed onto a breadboard the breakpoint will be hit and you can debug your component code.

You might also like