PDA

View Full Version : VB6 and BASIC STAMP



Downs
08-28-2008, 02:47 AM
Hello all,

Can anyone point me in the right direction for learning how to send a command from Visual Basic 6 to a BASIC STAMP? Say I have a push button installed on my homework board and that push button starts a line of code once it pressed. How do I send a command from VB6 to tell that push button to fire? Sofar I'm able to link VB6 to my stamp and my stamp will send Text to my App. but it always sends a funny character for every CR command fired off by the BASIC STAMP. I think I'm some what on the right track but need some more help. All if any, would be greatly appreciated. Thanks!!!

Tom-n-Tony
08-30-2008, 10:10 PM
I'm attempting something similar. Is your BOE a serial version or a USB version? If it is USB, can you send me your VB code so I can see how you established serial communciaitons over a USB cable.

Thanks,
Tom

Downs
08-31-2008, 01:02 AM
Tom,

I'm using the HomeWork Board and using the Parallax USB to Serial Adapter. Below is my code to my little project I'm working on. I have commented what's not working.

Rex .....


Visual Basic 6.0 Professional Side of the project:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''VB6 Pro.''''''''''''''''''''''''''''''''''''''''''''
' '
' Example of controlling an LED on a BASIC STAMP '
' board, via Visual Basic 6.0 Professional '
' '
' BS HomeWork Board, USB to Serial Adapter '
' By Rex A. Downs '
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''


Option Explicit

Dim PinNumber As Long
Dim PinState As Long

Private Sub Form_Load()

' Use COM1
MSComm1.CommPort = 4

' 2400 baud, no parity, 8 data bits, 1 stop bit
MSComm1.Settings = "2400,N,8,1"

' Make sure DTR line is low to prevent Stamp reset
MSComm1.DTREnable = False

' Open the port
MSComm1.PortOpen = True

End Sub

Private Sub cmdOn_Click()

'Turn LED ON
PinNumber = 14 ' PIN # on Basic Stamp Board Where LED is connected.
PinState = 1 ' PIN State of Above PIN #.....( 1 = HIGH (On))
MSComm1.Output = Chr$(255) & Chr$(PinNumber) & Chr$(PinState) ' Send Pin Number & Pin State to BS Board

End Sub

Private Sub cmdOff_click()

'Turn LED OFF
PinNumber = 14 ' PIN # on Basic Stamp Board Where LED is connected.
PinState = 0 ' PIN State of Above PIN #.....( 0 = LOW (Off))
MSComm1.Output = Chr$(255) & Chr$(PinNumber) & Chr$(PinState) ' Send Pin Number & Pin State to BS Board
End Sub


Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False 'Close Port
End Sub

Private Sub MSComm1_OnComm() 'This section is not working!!!!
If MSComm1.CommEvent = comEvReceive Then 'Code to fire off if MSComm1 is receiving from the BS Board
Text1.Text = MSComm1.Input ' Put result from BS Board in Text Box 1
End If
End Sub


BASIC STAMP II Side of the project:

'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''

' {$STAMP BS2}
' {$PBASIC 2.5}


PinNumber VAR Byte
PinState VAR Byte

Main:

' Use the programming port to receive
' data at 2400 baud
' Wait for the synch byte (255) and then
' get the PinNumber and PinState

SERIN 16,16780,[WAIT(255),PinNumber,PinState]


' If PinState=0 Then go to GoLow
' otherwise go to GoHigh

BRANCH PinState,[GoLow,GoHigh]
GOTO Main


' Set The pin low
GoLow:
LOW PinNumber
PAUSE 500
SEROUT 16,16780,["Off"] 'This is not working!!!
GOTO Main


' Set the pin high
GoHigh:
HIGH PinNumber
PAUSE 500
SEROUT 16,16780,["On"] 'This is not working!!!!
GOTO Main

Unsoundcode
08-31-2008, 03:55 AM
Hi·Downs, although the following·link discuss's VB Express it contains some useful elements on how to send and receive data to and from the Stamp

http://forums.parallax.com/showthread.php?p=671804

Sending and receiving strings is perhaps the easier if you have a way to do that

MSComm1.Output = Chr$(255) &·"14" & Chr$(13)·&·"0" & Chr$(13)·' Send Pin Number & Pin State to BS Board,

the carriage returns in the code above indicate and seperate·the end of each piece of data, to receive the data use the code below

SERIN 16,16780,[WAIT(255),DEC2 PinNumber,DEC1 PinState]

without too much alteration to your code PinNumber should now contain the value 14 and PinState 0

Jeff T.

Downs
08-31-2008, 04:09 AM
Hey Jeff, My code is working well but the problem I'm having now is the part where the STAMP sends back the text ("On" or "Off") to my VB App. I've got the VB App. receiving the text from the STAMP but sometimes it's not complete, i.e.("n" instead of On or "ff" instead of Off) I'm not sure as to what the problem is. My code on the STAMP side is as follows:

' {$STAMP BS2}
' {$PBASIC 2.5}


PinNumber VAR Byte
PinState VAR Byte

Main:

' Use the programming port to receive
' data at 2400 baud
' Wait for the synch byte (255) and then
' get the PinNumber and PinState

SERIN 16,16780,[WAIT(255),PinNumber,PinState]

' If PinState=0 Then go to GoLow
' otherwise go to GoHigh

BRANCH PinState,[GoLow,GoHigh]

' Set The pin low
GoLow:
LOW PinNumber
SEROUT 16,16780,["Off] 'This is not working!!!
GOTO Main

' Set the pin high
GoHigh:
HIGH PinNumber
SEROUT 16,16780,["On"] 'This is not working!!!!
GOTO Main

Unsoundcode
08-31-2008, 05:26 AM
Hi Downs, is it possible you are experiencing the same problem as described in this thread http://forums.parallax.com/showthread.php?p=747306

when you use the programming port the sent characters are echoed from the Stamp back to the PC so when you send 255,14 and 0 to the stamp the stamp replies "255140Off" those characters are now in the read buffer and need to be parsed.

Jeff T.

Downs
08-31-2008, 07:29 AM
That makes sense! Now just have to figure out how to code that in VB. Thanks!!