简体   繁体   English

While循环永远不会以可视化的基本sendin数据发送到串行端口

[英]While loop never ends in visual basic sendin data to serial port

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... 实际上,当我选择一个值时,我使用从0到100的schroll条,可以说44数据连续连续地进入串行状态,但无法停止运行,并且程序不断冻结,因此我尝试了很多事情。这是代码...

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... 但是永远无法将0停止循环...

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! 想象一下,当我从0滚动到100并停止时说43,我希望43转到串行,直到我说不走! 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)! 我想要这个,因为43代表到arduino端口的pwm,所以arduino一直读取该数字并将其转换为pwm输出信号,如果它取零,我希望pwm信号停止..但是一旦我进入循环x永远不会归零(无限循环)! 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) 私有子HScrollBar1_Scroll(ByVal发送者作为System.Object,ByVal e作为System.Windows.Forms.ScrollEventArgs)处理HScrollBar1.Scroll Label6.Text =“” Label4.Text =“” Label1.Text =“ Pwm” +格式(HScrollBar1。值)+“%”滚动=格式(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

Your value of 'x' never changes, so the do ... while loop never terminates. 您的'x'值永远不会改变,因此do ... while循环永远不会终止。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM