connecting stamp with VB2008.NET
Mike15
Posts: 109
I am trying to communicate with the stamp using VB. I want to send the pin number and pin state to the stamp and have a LED execute.
I have found alot of examples using VB6.
·On the VB side:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
Dim pin, lowHigh As Byte
pin = cmbPin.SelectedItem
If RadioButton1.Checked = True Then
lowHigh = 0
Else
lowHigh = 1
End If
SerialPort1.Write(Chr("255") & Chr(pin) & Chr(lowHigh))
SerialPort1.Close()
On the Stamp side
pinnumber· VAR· Byte
pinstate·· VAR· Byte
Main:
SERIN 16, 16780, [noparse][[/noparse]WAIT (255), pinnumber, pinstate]
BRANCH pinstate, [noparse][[/noparse]golow, gohigh]
GOTO main
golow:
LOW pinnumber
GOTO main
gohigh:
HIGH pinnumber
GOTO main
In some of the examples I found online used this:
·· SerialPort1.Write(Chr$(255) & Chr$(pin) & Chr$(lowHigh))
instead of:
·· SerialPort1.Write(Chr("255") & Chr(pin) & Chr(lowHigh))
Any suggestions?
I have found alot of examples using VB6.
·On the VB side:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
Dim pin, lowHigh As Byte
pin = cmbPin.SelectedItem
If RadioButton1.Checked = True Then
lowHigh = 0
Else
lowHigh = 1
End If
SerialPort1.Write(Chr("255") & Chr(pin) & Chr(lowHigh))
SerialPort1.Close()
On the Stamp side
pinnumber· VAR· Byte
pinstate·· VAR· Byte
Main:
SERIN 16, 16780, [noparse][[/noparse]WAIT (255), pinnumber, pinstate]
BRANCH pinstate, [noparse][[/noparse]golow, gohigh]
GOTO main
golow:
LOW pinnumber
GOTO main
gohigh:
HIGH pinnumber
GOTO main
In some of the examples I found online used this:
·· SerialPort1.Write(Chr$(255) & Chr$(pin) & Chr$(lowHigh))
instead of:
·· SerialPort1.Write(Chr("255") & Chr(pin) & Chr(lowHigh))
Any suggestions?
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering
The value 16780 you are using for SERIN on the STAMP makes it 2400bps 8N1, you need to make sure that the SerialPort1 object in VB2008 is set the same way, or else none of this will work.
There is absolutely no difference between these statements as far as VB is concerned...they will do exactly the same thing:
SerialPort1.Write(Chr$(255) & Chr$(pin) & Chr$(lowHigh))
SerialPort1.Write(Chr("255") & Chr(pin) & Chr(lowHigh))
Are you getting errors running the code?
What sort of behavior are you seeing?
More details make it easier to help.
I am trying to light 4 LEDs with the stamp by sendig the information from VB (pin number and state)
First we sent only the pin number
SerialPort1.Write(pin)·· this works with the program pinnumber.bs2. When used with terminal emmulator no CR is needed
Then we tried to send just the state
SerialPort1.Write((lowHigh) & vbCrLf)· this works with the program pinstate.bs2. The dec formater is needed in the Bs2 serin line. The CR is needed to get to work properly
But when we try to combine the two
SerialPort1.Write(pin & vbCrLf & lowHigh & vbCrLf) with the test.bs2 program we receive no response. When the terminal emmulator is used along with the return keystoke after each input the test.bs2 program works.
Checked baud rates they are right. I am not sure what VB is sending to the stamp(format?) or what the format of this line should be.
SerialPort1.Write(pin & vbCrLf & lowHigh & vbCrLf)
·
The issue is probably the baud rate , try the same test program at 2400 baud, not everything works at 9600 with a BS2.
As for the Visual Basic format·as written above·SerialPort1.Write (and also SerialPort1.WriteLine) transmit a string of the ASCII character codes representing your variables pin and lowHigh. For example if you want to set pin 4 high the ASCII character codes transmitted would be 52 (4)· and 49 (1). You have done the right thing at the Stamp by using the DEC formatter which takes those ASCII values and translates them as their decimal equivalent.
Jeff T.
P.S check out the following link if you have not already done so. http://forums.parallax.com/showthread.php?p=671804
vbCrLf is a typical windows newline command, that is Carriage Return / Line Feed (hence CrLf). As a result, using it the way you are, you are actually sending 2 BYTES of data, ASCII 13 (the cr) and ASCII 10 (the lf). You could just as easily use vbCr or send it in ASCII.
Personally, using .Net studio, I have gotten much better results using 9600 8N1 for settings, but your experience may be different.