VBB DEV CSharpExpress2008

Embed Size (px)

Citation preview

  • 8/2/2019 VBB DEV CSharpExpress2008

    1/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 Lampcomponent 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 librarywhich will be instantiatedby VBB3 using reflection

    Step 2: Add VirtualBreadboard References

    Add references to theIOpenVBB.DLL andOpenVBBLib.DLLcontained in the

    Normally this is in theC:\VBB3 directory

  • 8/2/2019 VBB DEV CSharpExpress2008

    2/13

    Step 3: Rename the default class to MyLamp and enter thefollowing code

    What this code actually does will be explained in a future tutorial. First wejust 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

    {

    publicclassMyLamp : OpenVBBComponent

    {

    IopenVBBSVGFrame myLampON;

    IopenVBBSVGFrame myLampOff;

    IopenVBBXMLDOMNode myPinout;

    IopenVBBVoltage myInputPin;

    publicoverridevoid 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);

    }

    publicoverridevoid wirePins(IopenVBBPortInterface portInterface,

    IopenVBBWiringErrors errors)

    {

    myInputPin = portInterface.getPin(1);

    myInputPin.RegisterAsVoltageSink(this);

    }

    publicoverrideint getPinCount()

    {

    return 1;

    }

    publicoverridevoid sweepPins(double elapsedTime)

    {

    myLampON.setVisible(myInputPin.isHigh());

    myLampOff.setVisible(!myInputPin.isHigh());

    }

    }

    }

  • 8/2/2019 VBB DEV CSharpExpress2008

    3/13

    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

  • 8/2/2019 VBB DEV CSharpExpress2008

    4/13

  • 8/2/2019 VBB DEV CSharpExpress2008

    5/13

    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

    /skin/vbb/User.xml

    Update the tag with the name of your component not forgetting to prepend

    the project namespace to the component. Also update the tag of the assembly

    to point to the .dll you built in step 4

    UserUser defined lamp

    MyVBBComponent.MyLamp

    assembly

    C:\Users\James\Documents\VisualStudio 2008\Projects\MyVBBComponent\MyVBBComponent\bin\Debug\

    MyVBBComponent.dll

    Step 6: Locatingyour Componentin VBB

    Run VirtualBreadboard

    and create a new VBB

    Project. Your component

    is located in the UserToolbox

  • 8/2/2019 VBB DEV CSharpExpress2008

    6/13

    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.

  • 8/2/2019 VBB DEV CSharpExpress2008

    7/13

    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

  • 8/2/2019 VBB DEV CSharpExpress2008

    8/13

    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 Projectoption. This configures the solution to launch the VBBLancher application on startup.

  • 8/2/2019 VBB DEV CSharpExpress2008

    9/13

    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 referencesof the VBBLauncher application and selecting Add reference. From the Projects Tab

    select the MyVBBComponent and click OK.

  • 8/2/2019 VBB DEV CSharpExpress2008

    10/13

  • 8/2/2019 VBB DEV CSharpExpress2008

    11/13

    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 System;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace VBBLauncher

    {

    publicpartialclassForm1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    privatevoid button1_Click(object sender, EventArgs e)

    {

    AppDomain vbb = AppDomain.CreateDomain("VBB");

    vbb.ExecuteAssembly("C:\\VBB3\\VirtualBreadboard.exe" );

    }

    }

    }

  • 8/2/2019 VBB DEV CSharpExpress2008

    12/13

  • 8/2/2019 VBB DEV CSharpExpress2008

    13/13