Start-Stop program

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:

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
 
Hello.

The easiest why would lead over an timer. Just drag a Timer Object from the toolbox to your form, and set the Interval to 2000.

VB.NET:
    Private stnByte as Byte = &H41

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        btnStart.Enabled = False
        btnStop.Enabled = True
        If SerialPort.IsOpen = 0 Then
            SerialPort.Open()
        End If

        Me.yourTimer.Enabled = True
    End Sub

    Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
        Me.yourTimer.Enabled = False

        btnStart.Enabled = True
        btnStop.Enabled = False
        SerialPort.Close()
    End Sub

    Private Sub yourTimer_Tick(Byval sender as Object, Byval e as TimerEventArgs) Handles yourTimer.Tick
        Me.yourTimer.Enabled = False

            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

        Me.yourTimer.Enabled = True
    End Sub

Bobby
 
I tried this, but the code always seem to return "Port is closed" and goes to Debug mode (I didn't have a Try..Catch routine on close/open ports). And there are only times in the program when I can press Stop to actually stop the sending, even if I used DoEvents(). What could be wrong? It seems like the program is hanging when I press Start.

VB.NET:
    Private Sub tmrStart_Tick1(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrStart.Tick
        Dim stnByte As Byte = &H41 ' For POLLING
        tmrStart.Enabled = False
        If (SerialPort.IsOpen()) Then
            SerialPort.Close()
        Else : SerialPort.Open()
        End If
        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
            Application.DoEvents()
            tmrStart.Enabled = True
        End Try

        tmrStart.Enabled = True
    End Sub
 
Back
Top