Wednesday, October 26, 2011

Controlling a Basic Stamp II With Visual Basic


Setting the Basic Stamp II's Input/Output or  I/O pins low or high (on and off) with Visual Basic Button_Click events.


...The BASIC Stamp II is a microcontroller with a small, specialized BASIC interpreter(PBASIC) built into its ROM. It is manufactured by Parallax, Inc.
In my example I use the Basic Stamp II Homework Board.
Heads up..
...Controlling a Basic Stamp II with Visual Basic is pretty easy.
...It is required to write a small program to the Basic Stamp itself and then to write a small program with Visual Basic.
...I assume you have some experience with the Basic Stamp Editor and with Visual Basic.
...I am not a programmer but I do consider myself pretty resourceful so through a little research I was able to determine how to do this.

 I connected my Basic Stamp to my laptop with a USB to serial connector.
I picked one up from Radio Shack for about 30 dollars.
Later noticed they do have a Basic Stamp prototyping model available that is ready for USB...
Story of my life.
I am pretty sure that there were some necessary drivers to be installed..I dont remember exactly.





In the PBASIC Editor enter the following code.

______________________________________________________________________________

' {$STAMP BS2}
' {$PBASIC 2.5}
' Hook up a two LEDs to Pins 3 and 4 to test this.
' Then VB code is set up for Com1 incase you have to change this to the Com
' port of your computer.


  Main:
  SERIN 16, 16468, [STR command\1]    ' Get 1-byte string


CheckCommand:
  IF command = "1" THEN
     GOTO Task1
  ENDIF
  IF command = "2" THEN
     GOTO Task2
  ENDIF
  IF command = "3" THEN
     GOTO Task3
  ENDIF
  IF command = "4" THEN
     GOTO Task4
  ENDIF
GOTO Main


Task1:
HIGH 3
GOTO Main
Task2:
LOW 3
GOTO Main
Task3:
HIGH 4
GOTO Main
Task4:
LOW 4
GOTO Main
______________________________________________________________________________

I downloaded a evaluation version  of Visual Basic 2010 Express
I hope that you have some experience with it.
Create the necessary controls and label them properly.
 Then enter the following code.

Public Class Form1
    Private WithEvents serialPort As New IO.Ports.SerialPort


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        IF serialPort.IsOpen THEN
            serialPort.Close()
        END IF
        Try
            With serialPort
                .PortName = "COM10"
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Handshake = IO.Ports.Handshake.None
            END With
            serialPort.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        END Try


    END Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        serialPort.WRITE("1")
        TextBox1.Text = "LED 1 ON"
    END Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        serialPort.WRITE("2")
        TextBox1.Text = "LED 1 OFF"
    END Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        serialPort.WRITE("3")
        TextBox2.Text = "LED 2 ON"
    END Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        serialPort.WRITE("4")
        TextBox2.Text = "LED 2 OFF"
    END Sub
    Private Sub Form1_close()
        serialPort.Close()
    END Sub


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged


    END Sub


    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged


    END Sub
End Class

' I hope that the information is useful.
' Forgive my brevity... Watching TV...and I'm hungry.
' I will respond to any comments and try to answer any questions.

12 comments:

  1. Yeah I think this is a bit beyond my level.

    Ok, maybe not just "a bit". :P

    ReplyDelete
  2. So thats how machines talk! Very informative and completely above my grasp!

    ReplyDelete
  3. I understand it! I thank you, this should help. I will be using the express version of Visual Basic 2008, but all the same, your code examples are very helpful. WKG, Virginia (U.S.)

    ReplyDelete
  4. with this work with this chip: http://www.robotshop.com/ca/netmedia-basicx-35-microcontroller.html

    ReplyDelete
  5. thanks helped alot

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Very useful, worked almost immediately, thanks!

    It seems like u forgot to declare the variable command in Basic Stamp which I did like this

    command VAR Byte

    ReplyDelete
  8. can you send me the VB project file for the code on this project. i am not that familiar with VS and am having some trouble getting started.
    send to wespiece at gmail dot com
    thank you

    ReplyDelete
    Replies
    1. I got this figured out. Hit me if I can help anyone else.

      Delete
    2. Hi Wes Pierce I am doing a project that I need to use display something by communicating from basic stamp editor to visual basic software. Any help?

      Delete
  9. I am having trouble. The light don't come on with the Visual Basic program. What am I doing wrong?

    ReplyDelete
  10. I am having trouble. The light don't come on with the Visual Basic program. What am I doing wrong?

    ReplyDelete