我试图将一些数据发送到串口!实际上我使用的是0到100之间的schroll bar 当我选择一个值时,可以说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
但永远不能为0循环停止...
谢谢!!但我觉得这对我不起作用!!想象一下,当我将条形图从0滚动到100并停止让我们说43时我想要43去串口,直到我说不要去!我想要这个因为43代表pwm到arduino端口所以arduino一直读取这个数字并将其转换为pwm输出信号,如果它为零我想要pwm信号停止..但是一旦我在循环中x不能转到零(无限循环)!还有其他建议吗? 好吧,你是我的上帝!呵呵呵,我看着这个,正在努力工作,我想!我添加了两个额外的按钮用于启动和停止,所以代码现在变为这个:Private Sub HScrollBar1_Scroll(ByVal sender As System.Object,ByVal e As System.Windows.Forms.ScrollEventArgs)处理HScrollBar1.Scroll Label6.Text ="" Label4.Text ="" Label1.Text =" Pwm" +格式(HScrollBar1.Value)+" %" scroll =格式(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
答案 0 :(得分:2)
您的'x'值永远不会改变,因此do ... while
循环永远不会终止。
Do While x > 0
SerialPort1.Open()
SerialPort1.Write(x)
If x = 0 Then
Exit Do
Else
x -= 1
End If
Loop
答案 1 :(得分:1)
将代码更改为:
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