Communicating via Serial Port with Visual Basic.NET Program
Hello everyone!
I've been working with serial port communication and I have came to a point to where I'm completely stuck.
For some reason I can send data to the BS2, and the BS2 will receive it. However, the VB.NET Program isn't receiving it. On my BOE Board both the blue and pink LEDs are lighting up, telling me the information has sent, but I'm just not convinced. Please look over my code so you can see what I'm doing wrong.
The VB.NET Program will "Write" a string of "1". When the BS2 receives that, it needs to respond with "Hello". The VB.NET program will receive it and put that message into a textbox.
BS2 Code:
If you have any knowledge of VB.NET, I also posted that code.
Please help! What am I doing wrong?!
Thanks!
- Ryan
I've been working with serial port communication and I have came to a point to where I'm completely stuck.
For some reason I can send data to the BS2, and the BS2 will receive it. However, the VB.NET Program isn't receiving it. On my BOE Board both the blue and pink LEDs are lighting up, telling me the information has sent, but I'm just not convinced. Please look over my code so you can see what I'm doing wrong.
The VB.NET Program will "Write" a string of "1". When the BS2 receives that, it needs to respond with "Hello". The VB.NET program will receive it and put that message into a textbox.
BS2 Code:
' {$STAMP BS2}' {$PBASIC 2.5}
SerialInput VAR Byte
Main:
SERIN 16, 16468, [STR SerialInput\1]
IF SerialInput = "1" THEN
SEROUT 16, 16468, ["HELLO"]
GOTO Done
ENDIF
GOTO Main
Done:
END
If you have any knowledge of VB.NET, I also posted that code.
Imports System.IO.Ports
Public Class Form1
Private WithEvents comPort As New IO.Ports.SerialPort
Private dataIn As String
Private readBuffer As String = String.Empty
Private Bytenumber As Integer
Private ByteToRead As Integer
Private byteEnd(2) As Char
Private comOpen As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With comPort
.ParityReplace = &H3B ' replace ";" when parity error occurs
.PortName = "COM5"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
.RtsEnable = False
.ReceivedBytesThreshold = 1 'threshold: one byte in buffer > event is fired
.NewLine = vbCr ' CR must be the last char in frame. This terminates the SerialPort.readLine
.ReadTimeout = 10000
End With
Try
comPort.Open()
comOpen = comPort.IsOpen
Catch ex As Exception
comOpen = False
MsgBox("Error Open: " & ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
comPort.Write("1")
End Sub
Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
If comOpen Then
Try
byteEnd = comPort.NewLine.ToCharArray
Bytenumber = comPort.BytesToRead
readBuffer = comPort.ReadLine
Me.Invoke(New EventHandler(AddressOf DoUpdate))
Catch ex As Exception
MsgBox("Read " & ex.Message)
End Try
End If
End Sub
Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
TextBox1.Text = readBuffer
End Sub
End Class
Please help! What am I doing wrong?!
Thanks!
- Ryan

Comments
Edit: Browse through this thread. It has pretty much everything you need for doing what you want to do,
http://forums.parallax.com/showthread.php?96973-VB-Express-to-Stamp-Template
While I was waiting, I actually did some searching on google and found out my problem. I was using the serialport.readline in vb.net. This reads up to the next line. So what I need to add was CR after the SEROUT message. Example: SEROUT 16, 16468, ["HELLO WORLD!", CR]. This worked.
Here's my only problem that I need help with. Every time I write to the COM Port and send it, when it returns to the VB Program, it has the original string at the beginning of the line. Sounds confusing. Let me solve that with some code.
' {$STAMP BS2}' {$PBASIC 2.5} SerialInput VAR Byte Main: SERIN 16, 16468, [SerialInput] IF SerialInput = "1" THEN SEROUT 16, 16468, ["11.111", CR] ELSEIF SerialInput = "2" THEN SEROUT 16, 16468, ["22.222", CR] ELSEIF SerialInput = "3" THEN SEROUT 16, 16468, ["33.333", CR] ENDIF GOTO MainBasically, I use the SerialPort.Write in VB.NET. This writes to the BS2 a number. In this case, "1", "2", or "3". Next it will send that number multiple times with a decimal after the first two digits. When I send "1", for example, the BS2 receives that, and then sends "11.111". But when it shows up on the vb.net program, it reads 111.111. It's keeping that first string I sent from the program. How do I clear the SerialPort on the BS2? That way the serial port is clear when I send it, therefore it only shows what I wanted it to show in the first place..
[SIZE=2][FONT=Times New Roman][COLOR=#000000]SEROUT 16, 16572,["RDY",10][/COLOR][/FONT][/SIZE] [SIZE=2][FONT=Times New Roman][COLOR=#000000]SERIN 16,16572,15,timeout,[WAIT("!"),DEC4 x,DEC4y][/COLOR][/FONT][/SIZE] [SIZE=2][FONT=Times New Roman][COLOR=#000000]timeout:[/COLOR][/FONT][/SIZE] [SIZE=2][FONT=Times New Roman][COLOR=#000000]PAUSE 15[/COLOR][/FONT][/SIZE] [SIZE=2][FONT=Times New Roman][COLOR=#000000]RETURN[/COLOR][/FONT][/SIZE]VB Code also needs the RDY command:
This is just examples from the .doc file in that thread. You will need to modify your code accordingly. The stamp can not send and receive data simultaneously and therefor your VB program needs to tell the stamp it is ready to send/rcv and vice versa. At least, that is the way I understood it when I had similar issues.
Everything with a header is read as data everthing else is discarded, including the echo
For example
VB looks for the "mydata" header with ReadLine and knows the following string is valid data.
In VB Readline looks for a string terminated with a Newline character which has a default value of 10, Carriage return has a value of 13. The default NewLine value can be modified in VB
eg SerialPort1.NewLine=10 or SerialPort.NewLine=13
This means the default would be SEROUT 16, 16468,
Changing to SerialPort.NewLine=13 would mean SEROUT 16, 16468,
Jeff T.