0
votes

I'm trying to send some data to serial port! actually i use a schroll bar that is from 0 to 100 when i choose a value lets say 44 the data goes to serial continiously but cant stop going and the programm freezes in continiously loop i tried many things.. here is the code...

Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
    Label6.Text = ""
    Label4.Text = ""
    Label1.Text = "Pwm " + Format(HScrollBar1.Value) + " %"

    x = Format(HScrollBar1.Value)

    Try
       Do While x > 0
           SerialPort1.Open()
           SerialPort1.Write(x)
           If x = 0 Then
              Exit Do
           End If
       Loop
       SerialPort1.Close()

       Catch ex As Exception               'se periptwsh pou paei na ginei exception dld na mhn uparxei katholou h seiriakh pou epilekxthke kanei catch exception kai emfanizei mhnuma lathous
          Label1.Text = "No Serial Port is Connected"
          Label4.Text = ""
     End Try
End Sub

But never can go to 0 for the loop to stop...

thank you!! but i thing that doesnt work for me!! Imagine that when i scroll the bar from 0 to 100 and stop lets say to 43 i want the 43 to go to to serial until i say dont go! i want this because 43 represents pwm to arduino port so the arduino reads all the time this number and transform it to pwm output signal , if it takes zero i want the pwm signal to stop..but once i am in the loop x cant never go to zero (infinite loop)! any other suggestion?

ok you are my GOD!!! hehehe i looked into this and is working pefcetly i think!! i added two extra buttons for start and stop so the code now is chenged to this :

Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll Label6.Text = "" Label4.Text = "" Label1.Text = "Pwm " + Format(HScrollBar1.Value) + " %" scroll = Format(HScrollBar1.Value)

End Sub



Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    'start
    If Not worker.IsBusy Then                   'button pou kanei thn methodo sub_foodowork na treksei
        worker.WorkerReportsProgress = True
        worker.WorkerSupportsCancellation = True
        worker.RunWorkerAsync()
    End If

    Label1.Text = "Pwm Started: " + Format(HScrollBar1.Value) + " %"

End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    'cancel
    If worker.IsBusy AndAlso worker.WorkerSupportsCancellation Then 'button pou kanei thn methodo foo_work na stamathsei
        worker.CancelAsync()
    End If
    Label6.Text = ""
End Sub

Private Sub foo_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles worker.DoWork

    Try

        Do While scroll > 0                         'h methodos pou stelnei pwm sthn seiriakh
            SerialPort1.Open()
            SerialPort1.Write(scroll)
            SerialPort1.Close()
            If worker.CancellationPending Then
                e.Cancel = True
                Exit Do

            End If

            worker.ReportProgress(scroll)
            'Threading.Thread.Sleep(100)
        Loop

    Catch ex As Exception               'se periptwsh pou paei na ginei exception dld na mhn uparxei katholou h seiriakh pou epilekxthke kanei catch exception kai emfanizei mhnuma lathous
        Label1.Text = "No Serial Port is Connected"  'einai delay sto thread orizw oso thelw egw 'h to bgazw teleiws gia na trexw me to clock tou pc
        Label4.Text = ""
    End Try
End Sub

Private Sub foo_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles worker.ProgressChanged
    'Label1.Text = e.ProgressPercentage.ToString
End Sub

Private Sub foo_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles worker.RunWorkerCompleted
    If e.Cancelled Then                             'mhnumata rermatismou kai teloys ths methodou foodowork
        Label1.Text = "Pwm Stoped"
    ElseIf Not IsNothing(e.Error) Then
        Label1.Text = "Error " & e.Error.Message
    ElseIf Not SerialPort1.IsOpen() Then
        Label1.Text = "No Serial Port is Connected"

    End If
    If scroll = 0 Then
        Label1.Text = "Pwm is set to 0"
    End If

End Sub
2
If you want to send continuously until the user takes some action, you'll need to offload this to a BackgroundWorker. - Jon B

2 Answers

2
votes

Your value of 'x' never changes, so the do ... while loop never terminates.

Do While x > 0
  SerialPort1.Open()
  SerialPort1.Write(x)
  If x = 0 Then
    Exit Do
  Else 
    x -= 1
  End If
Loop
1
votes

change code to:

Do While x > 0
       SerialPort1.Open()
       SerialPort1.Write(x)
       If x = 0 Then
          Exit Do
       End If
       x -= 1   '' this is newly added line
Loop