Interacting with the BS2 using Visual Basic
Basic Jim
Posts: 106
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.
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
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
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.
Thanks again,
Jim W.
Thanks a million,
Jim W.
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 Williams
Applications Engineer, Parallax
Thanks,
Jim W.
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
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.
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.
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