我正在通过 COM 端口发送数据,并以 4000 的间隔启动我的计时器,如果我在 4 秒之前没有收到任何消息,我会发回我的消息。
{加载}
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
[...]
SendCard(0)
[...]
End Sub
{SendCard Sub} 我通过设置通过端口发送数据的属性“LastCommand”来通过此 Sub 发送所需信息。
Private Sub SendCard(ByVal cardIndex As Integer)
If cardIndex < Setting.Machine.Cards.Length Then
'Calling LastCommand's Set()
LastCommand = "*" & Setting.Machine.Cards(cardIndex) & ": STRQ" & ControlChars.CrLf
Else
'Blah blah...
End If
End Sub
该属性发送数据并启动计时器。
Public Property LastCommand() As String
Get
Return lastCommandString
End Get
Set(ByVal value As String)
lastCommandString = value
MainCOMSerialPort.Write(value)
ResponseTimer.Start()
End Set
End Property
当我收到答案时,我会停止计时器,然后询问下一张卡片的信息。
Private Sub MainCOMSerialPort_DataReceived(...)
[...]
ResponseTimer.Stop() 'After this stop it does not trigger the Tick event any longer.
SendCard(Array.IndexOf(Setting.Machine.Cards, CardString) + 1) 'Sending the next card
[...]
End Sub
滴答事件适用于第一张卡片
Private Sub ResponseTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResponseTimer.Tick
'MessageBox.Show("No Respone Yet")
LastCommand = LastCommand
End Sub
所以它每 4 秒询问一次我的第一个命令,直到我得到一些回报,但是当我从第二个命令重新开始时,计时器不会打勾。
我正在使用 .NET 4.0 和 {Windows.Forms.Timer},我尝试使用 Timers.Timer 并且没有工作我不明白为什么它不会重新启动。