Shop OBEX P1 Docs P2 Docs Learn Events
Interacting with the BS2 using Visual Basic — Parallax Forums

Interacting with the BS2 using Visual Basic

Basic JimBasic Jim Posts: 106
edited 2005-07-11 02:07 in BASIC Stamp
Hello Basic Stamp Mavens,

I would like to use visual basic VB5 to interact with a basic stamp. ie I would like to push a command button on my app. and have bs2·pin X go low and light an LED. I also want to monitor a pin and have my app. show the state of that pin.

I have tried a Nuts & Volts article " Data exchange with visual basic" wich works ok to store a number or string and reads them back. Unforunatly, I can't easily see how to tell the bs2 what to do via VB. I guess I could write a 0 or 1 in a memory location and have the bs2 poll that spot and drive an output pin that way. But, I was wondering if there is an easier way.

Are there other articles that address methods of interacting the bs2 to VB?

Is it possible for VB to use the default 9 pin connector to comunicate/control the BS2? I hate to waste all those precious pins and extra hardware if I don't have to.

Thanks,
Jim W.

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-15 23:34
    Yes, you can do that, but you must remember that the programming port echoes back everything sent to it. So what you have to do is filter that from your MSCOMM input buffer before you get to anything the BASIC Stamp has sent back. The other downside, possibly, is that you cannot do flow-control, unless you devise some kind of software scheme where the BASIC Stamp actually initiates communication -- that way the VB app won't try to send something when the BASIC Stamp isn't waiting for it.

    As far as telling the BASIC Stamp what to do, that's your call.· You can devise a simple protocol that uses messages to tell the BASIC Stamp to do something very simple, or something very complex (like run a subroutine and report back a piece of data).· There is no standard here -- you have to create for your specific needs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 6/15/2005 11:53:01 PM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-06-15 23:40
    Yup, IF you have VB 5 Pro, it comes with the MSComm control. Try the attached.
  • Basic JimBasic Jim Posts: 106
    edited 2005-06-16 00:24
    Hi alanlane5,
    I tried the VBDemo file you posted. I do have VB5 pro. However, I suspect I need to have some code on the BS2 side to make it work. Do you happen to have the stamp side of the code or is there an article that I can read up on?

    Thanks again
    Jim W.
  • MelMel Posts: 6
    edited 2005-06-16 00:34
    The Stamp program is in the ZIP file
  • Basic JimBasic Jim Posts: 106
    edited 2005-06-16 00:42
    Sorry about that
    Thanks again,
    Jim W.
  • Basic JimBasic Jim Posts: 106
    edited 2005-06-16 01:11
    The VB Demo file is exactly what I was looking for!! It works perfectly and is easy to understand.

    Thanks a million,
    Jim W.
  • Basic JimBasic Jim Posts: 106
    edited 2005-06-16 23:58
    OK here is another dumb question,
    The VB Demo file solved getting signals from the BS2 to my VB app That part works fine. What I'm trying to do now is to send a command from the VB app to the BS2. I created a command button in the vb app. If pushed I would like to send a '1' to the stamp.
    I'm running into a problem on how to capture it on the bs2 side though. Here is the code The program seems to hang at the serin line. I what I'm trying to do here is check to see if there is anything comming and if not, continue the program. I have a terible feeling I'm doing several things wrong here.
    '{$STAMP BS2}
    '{$PBASIC 2.5}
    SerBaud CON 84 + 16384 ' 9600 Baud, 'inverted'
    SerPort CON 16
    MyData VAR Byte
    Indata VAR Byte
    MyPin VAR IN5
    LED CON 4

    LOW LED
    INPUT MyPin
    MAIN:
    MyData = MyPin ' Read input state of 'In4'
    SEROUT SerPort, SerBaud, [noparse][[/noparse]DEC MyData] ' Output as "0" or "1"
    PAUSE 500 ' Wait 1000 mSec
    SERIN serport, serbaud, [noparse][[/noparse]DEC Indata] ' incomming data **program hangs here
    IF indata = "1" THEN

    HIGH 6
    ENDIF
    TOGGLE LED ' Optional...
    GOTO MAIN:
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-17 00:06
    Now you're running into the issues having to do with no flow control: If you click on that button when the Stamp is doing a PAUSE, the serial message is lost. Since you have no timeout in the SERIN, the program hangs. Use a timeout to escape the SERIN. Since you want to save pins and use the programming port, I suggest you write your Stamp code such that it gives the VB app permission to transmit; this will save you a lot of headaches.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Basic JimBasic Jim Posts: 106
    edited 2005-06-17 00:20
    can you give me an example of how to include a timeout to escape the serin? I see it as part of the syntax of serin but it doesn't give an example.

    Thanks,
    Jim W.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-17 01:17
    Sure ... here's an example copied right out of the online help file:

    result· VAR···· Byte

    Main:
    · SERIN 1, 16468, 2000, No_Data, [noparse][[/noparse]DEC result]
    · DEBUG CLS, ? result
    · STOP

    No_Data:
    · DEBUG CLS, "Timed out"
    · STOP

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-06-17 15:30
    It actually would have worked had you NOT had that "PAUSE 500" in there. And the PAUSE command 'tick' is 1 mSec, so that pauses for 1/2 second (500 mSec).

    A better example might be:

    Result VAR WORD

    Main:
    SERIN 16, 16468, 500, No_Data, [noparse][[/noparse]DEC Result] ' Wait 500 msec for data. Goto No_Data if times out
    DEBUG ? Result ' Outputs 'Result' value with some additional text...
    No_Data:
    GOTO MAIN

    There, that's an infinite loop, so you can keep testing.
  • Basic JimBasic Jim Posts: 106
    edited 2005-06-17 16:12
    Thanks to all,
    I can't wait to try it this evening. All in need to do is send 1 charactor from the VB app. when a command button is pushed. If the BS2 sees it, then it will go to a sub depending on which charactor was sent. If I can make it work I'll post the working program so others can use it. I'm a babe in the woods when it comes to serial communication and the BS2, So, it's great to have a place to pick the brains of people that have tread this path before.

    Best,
    Jim W.
  • christo1423christo1423 Posts: 19
    edited 2005-07-11 01:37
    I am interested in the Demo but unfortunately I don't hve VB5 , I only have VB4 and VB.NET do you have a version for one of these languages.
  • Basic JimBasic Jim Posts: 106
    edited 2005-07-11 02:07
    I think MSCOMM was a new addition in vb5 so, I don't think vb4 will work. I don't know about .Net. Anyway for your info. here is the code. The core of it is thanks to allenlan5.

    Here is the BS2 code. Change things around to suite your program

    '{$STAMP BS2}
    '{$PBASIC 2.5}
    SerBaud CON 84 + 16384 ' 9600 Baud, 'inverted'
    SerPort CON 16
    Counter VAR Word
    MyData VAR Byte
    InData VAR Byte
    MyPin VAR IN14
    LED CON 15
    d VAR Byte
    LOW LED
    INPUT MyPin
    MAIN:
    ' PAUSE 100
    TOGGLE LED ' toggle pin 15
    MyData = MyPin ' Read input state of 'In5'
    SEROUT SerPort, SerBaud, [noparse][[/noparse]DEC MyData] ' Output level of pin5 0 or 1
    SERIN 16, 16468, 75, No_Data, [noparse][[/noparse]Indata]' incomming data
    IF indata = "0" THEN TOGGLE 0 ' this one resets after 1 cycle
    IF indata = "1" THEN TOGGLE 1 ' this one resets after 1 cycle
    IF indata = "2" THEN TOGGLE 2 ' all the others work as expected
    IF indata = "3" THEN TOGGLE 3
    IF indata = "4" THEN TOGGLE 4
    IF indata = "5" THEN TOGGLE 5
    IF indata = "6" THEN TOGGLE 6
    IF indata = "7" THEN TOGGLE 7
    IF indata = "a" THEN
    FOR counter = 1 TO 150
    PULSOUT 11,1000
    PAUSE 1
    NEXT
    FOR counter = 1 TO 150 ' Just for fun loop for a light show
    PULSOUT 11,500
    PAUSE 1
    'TOGGLE D
    NEXT
    FOR counter = 1 TO 150 ' Just for fun loop for a light show
    PULSOUT 11,750
    PAUSE 1
    NEXT
    FOR d = 0 TO 7
    TOGGLE d
    PAUSE 5000
    NEXT
    ENDIF
    GOTO MAIN:
    No_Data: ' go here if there is no data comming in

    TOGGLE 14 ' I'm using this pin to flip the input pin14 to show data movement
    RETURN:

    Here is the VB side of the code. It's kind of fun to see the BS2 respond to a VB program you wrote. Even if it doesn't do much (yet!)

    Dim Cont As Long
    Dim cont1 As Integer
    Private Sub Command11_Click()
    MSComm1.Output = "a"
    End Sub
    Private Sub Command3_Click()
    MSComm1.Output = "0"
    End Sub
    Private Sub Command4_Click()
    MSComm1.Output = "1"
    End Sub
    Private Sub Command5_Click()
    MSComm1.Output = "2"
    End Sub
    Private Sub Command6_Click()
    MSComm1.Output = "3"
    End Sub
    Private Sub Command7_Click()
    MSComm1.Output = "4"
    End Sub
    Private Sub Command8_Click()
    MSComm1.Output = "5"
    End Sub
    Private Sub Command9_Click()
    MSComm1.Output = "6"
    End Sub
    Private Sub Command10_Click()
    MSComm1.Output = "7"
    End Sub
    Private Sub Command1_Click()
    Dim MyObj As Object
    Set MyObj = Command1
    If MSComm1.PortOpen Then
    MyObj.Caption = "Open"
    MSComm1.PortOpen = False
    Else
    MyObj.Caption = "Close"
    MSComm1.PortOpen = True
    Label2.Caption = Label2.Caption & vbCrLf
    End If
    End Sub
    Private Sub Command2_Click()
    End ' Ends program
    End Sub
    Private Sub Form_Load()
    Dim MyVal As Long
    Dim ErrMsg As String
    Dim TryStr As String
    On Error GoTo ErrHandle
    MSComm1.DTREnable = False
    MSComm1.Settings = "9600,n,8,1" ' 9600 baud, No Parity, 8 databits
    MSComm1.CommPort = 1
    MSComm1.RThreshold = 1 ' 0 (default) == NEVER kick off OnComm RX event
    TryStr = "Comm port " & MSComm1.CommPort
    If MSComm1.PortOpen <> False Then
    MSComm1.PortOpen = False ' Opens port
    End If
    Exit Sub
    ErrHandle:
    ErrMsg = "Error on " & TryStr & vbCrLf & _
    Err.Number & ": " & Err.Description
    MsgBox ErrMsg
    Resume Next
    End Sub
    Private Sub MSComm1_OnComm()
    Dim MyStr As String
    MyStr = Right(Label2.Caption, 1)
    Label2.Caption = MSComm1.Input
    If MyStr = "0" Then
    Cont = Cont + 1
    cont1 = cont1 + 1
    Shape1.FillColor = &HFF
    Else
    Shape1.FillColor = &HFF00&
    End If
    Label1.Caption = Cont
    If cont1 = 100 Then
    Command11.Value = True
    cont1 = 0
    End If
    End Sub
    Private Sub Timer1_Timer()
    If MSComm1.PortOpen Then
    Label2.Caption = Label2.Caption & MSComm1.Input
    End If
    End Sub

    Hope you can make it work.
    Best,
    Jim
Sign In or Register to comment.