为什么我的串行通信在循环启动时停止工作? [英] Why my serial communication stop working when loop is started?
本文介绍了为什么我的串行通信在循环启动时停止工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到串行数据通信问题,如果我开始循环,它不会更新我的秤中的数据。我无法弄清楚如何继续通信以及运行循环。我的代码的逻辑将在循环中,所以我可以检查我的整数值并将其与串行数据中的数据进行比较(称重)
这是我收到数据的代码
I have problem with serial data communication if I started a loop it is not updating the data from my weighing scale. I can't figure out how to continue the communication as well as to run the loop. The logic of my code will be in the loop so I could check the value from my integer and compare it to the data from serial data (Weighing Scale)
This is the code where I receive the data
Private Sub conWeight_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles conWeight.DataReceived
receivedText(conWeight.ReadExisting())
End Sub
Private Sub receivedText(ByVal [text] As String)
If Me.lblWeight.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf receivedText)
Me.Invoke(x, New Object() {(text)})
Else
Dim reverseString As String = [text]
Dim revString As String = StrReverse(reverseString)
Dim clean As String
clean = revString.Replace("=", "")
Me.lblWeight.Text = clean 'append text
End If
End Sub
这是循环函数
This is the function with loop
Function batching() As String
BunifuFlatButton1.Enabled = False
If binWeight = 0 Then
targetweight = CInt(txtbSilo1.Text) + CInt(txtbSilo2.Text) + CInt(txtbSilo3.Text) + CInt(txtbSilo4.Text)
If CInt(txtbSilo1.Text) > 0 Then
currentWeight = CInt(txtbSilo1.Text)
frmAutomaticControl.conControl.Open()
frmAutomaticControl.conControl.Write("motr1")
frmAutomaticControl.conControl.Close()
MsgBox("check")
Do
If binWeight >= currentWeight Then
frmAutomaticControl.conControl.Open()
frmAutomaticControl.conControl.Write("moto1")
frmAutomaticControl.conControl.Close()
Exit Do
End If
Loop
Else
End If
BunifuFlatButton1.Enabled = True
Else
MsgBox("Empty The Bin")
End If
Return 0
End Function
我尝试过:
我已将其更改为do或其他循环。
What I have tried:
I already to changed it into do while or other loop.
推荐答案
如果没有您的设备和数据,我们无法分辨。
所以,这取决于你。
幸运的是,你有一个工具可以帮你找到发生了什么:调试器。如果你不知道如何使用它,谷歌应该给你你需要的信息。
在函数的第一行放一个断点,然后运行你的代码通过调试器。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。
对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
Without your device and data we can't tell.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then Google should give you the info you need.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
这篇关于为什么我的串行通信在循环启动时停止工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文