Winterburn
New member
- Joined
- Aug 12, 2008
- Messages
- 2
- Programming Experience
- 1-3
Hello all. I am new in this forum coz I can't find any forum that can answer my simple question..
Here's the scenario. I have two buttons in one form: Start button and Stop button. If I press Start button, the program will send packets to the serial port every 2 seconds. It will not stop sending packets every 2 seconds until the Stop button is hit. It's that simple.
The question is, how do I program that? I'm using VB Express Ed 2008 with .NET Framework 3.5. Here's my futile attempt to code the above scenario:
Here's the scenario. I have two buttons in one form: Start button and Stop button. If I press Start button, the program will send packets to the serial port every 2 seconds. It will not stop sending packets every 2 seconds until the Stop button is hit. It's that simple.
The question is, how do I program that? I'm using VB Express Ed 2008 with .NET Framework 3.5. Here's my futile attempt to code the above scenario:
VB.NET:
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim stnByte As Byte = &H41 ' For POLLING
btnStart.Enabled = False
btnStop.Enabled = True
If SerialPort.IsOpen = 0 Then
SerialPort.Open()
End If
Do
Array.Clear(buffSend, 0, buffSend.Length)
buffSend(0) = &H1
buffSend(1) = stnBase
buffSend(2) = stnByte
buffSend(3) = &H5
buffSend(4) = &H76
buffSend(5) = &H4
SerialPort.DiscardInBuffer()
SerialPort.DiscardOutBuffer()
SerialPort.ReadTimeout = 2500
Array.Clear(buffReceive, 0, buffReceive.Length)
SerialPort.Write(buffSend, 0, 6)
Try
Dim ctr1 As Integer = 0
Do
buffReceive(ctr1) = SerialPort.ReadByte
ctr1 = ctr1 + 1
Loop While ctr1 <= 5
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim TimeDelay As DateTime
TimeDelay = Now
Do
Application.DoEvents()
Loop While DateDiff(DateInterval.Second, Now, TimeDelay) <> 5
Loop While (btnStop.Enabled = True)
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
btnStart.Enabled = True
btnStop.Enabled = False
SerialPort.Close()
End Sub