none
Timer定时器不能关联使用 RRS feed

  • 问题

  • 我想用Timer定时器做个PLC通讯程序,在Timer1_tick事件里关闭timer2(看门狗定时器)写给PLC发送的命令,发送完命令后关闭timer1并立即开启Timer2(当做看门狗来用),等待serialport_datareceived事件,在serialport_datareceived事件中关闭看门狗timer2,读取串口数据、校验数据并将数据写入数据库。如果timer2设定的时间内没有serialport_datareceived事件发生,用timer2_tick事件重新启动timer1.但是发现serialport_datareceived事件没能将timer1重启,timer2也没能将timer1重启。

     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer2.Enabled = False
            Timer1.Enabled = False
            SendMessage()
            Timer2.Enabled = True
        End Sub
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Timer2.Enabled = False
            SerialPort1.Read()
            CheckData()
            WriteDataBase()
            Timer2.Enabled = True
        End Sub
        Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            Timer1.Enabled = True
        End Sub

    2012年9月6日 10:43

答案

  • 在我这里测试了一下,倒是没什么问题,Timer正确的被启动和停止。不过建议你使用System.Timers里面的TImer类,功能更多更强大~http://msdn.microsoft.com/zh-cn/library/system.timers.timer

    Public Class Form1
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer2.Enabled = False
            Timer1.Enabled = False
            Console.WriteLine("Timer1 Now in DISABLED")
            Console.WriteLine("Timer1" & Timer1.Enabled.ToString)
            Timer2.Enabled = True
            Console.WriteLine("Timer2 Now in ENABLED")
            Console.WriteLine("Timer2" & Timer2.Enabled.ToString)
    
            Console.WriteLine("Timer11111111----------------------------------------")
        End Sub
        Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            Timer1.Enabled = True
            Console.WriteLine("Timer1 Now is  ENABLED")
            Console.WriteLine("Timer1" & Timer1.Enabled.ToString)
            Console.WriteLine("Timer22222222222-------------------------------------")
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 500
            Timer2.Interval = 1000
    
            Timer1.Enabled = True
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer2.Enabled = False
            Console.WriteLine("Timer1 Now in ENABLED")
            Console.WriteLine("Timer1" & Timer1.Enabled.ToString)
            '....
            Timer2.Enabled = True
            Console.WriteLine("Timer2 Now in ENABLED")
            Console.WriteLine("Timer2" & Timer2.Enabled.ToString)
            Console.WriteLine("Buttonnnnnnnnnnnnnnnn-------------------------------------")
        End Sub
    End Class
    


    Think again!

    2012年9月10日 7:46

全部回复

  • timer1无法重启,好像SerialPort_Datareceived事件无法重启timer1

    Public Class MainForm
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 500
            Timer2.Interval = 1000
            myserialPort.PortName = "COM2"
            myserialPort.BaudRate = 9600
            myserialPort.Parity = IO.Ports.Parity.None
            myserialPort.DataBits = 8
            myserialPort.StopBits = 1
            Timer1.Enabled = True
        End Sub
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer1.Enabled = False
            Label1.BackColor = Color.Red
            myserialPort.Write(".....") '写数据
            Timer2.Enabled = True
        End Sub
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            Timer2.Enabled = False
            Label1.BackColor = Color.Blue
            Timer1.Enabled = True
        End Sub
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles myserialPort.DataReceived
            Timer2.Enabled = False
            myserialPort.Read() '读数据
            Timer1.Enabled = True
        End Sub
    End Class

    2012年9月6日 13:28
  • 在我这里测试了一下,倒是没什么问题,Timer正确的被启动和停止。不过建议你使用System.Timers里面的TImer类,功能更多更强大~http://msdn.microsoft.com/zh-cn/library/system.timers.timer

    Public Class Form1
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer2.Enabled = False
            Timer1.Enabled = False
            Console.WriteLine("Timer1 Now in DISABLED")
            Console.WriteLine("Timer1" & Timer1.Enabled.ToString)
            Timer2.Enabled = True
            Console.WriteLine("Timer2 Now in ENABLED")
            Console.WriteLine("Timer2" & Timer2.Enabled.ToString)
    
            Console.WriteLine("Timer11111111----------------------------------------")
        End Sub
        Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            Timer1.Enabled = True
            Console.WriteLine("Timer1 Now is  ENABLED")
            Console.WriteLine("Timer1" & Timer1.Enabled.ToString)
            Console.WriteLine("Timer22222222222-------------------------------------")
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 500
            Timer2.Interval = 1000
    
            Timer1.Enabled = True
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer2.Enabled = False
            Console.WriteLine("Timer1 Now in ENABLED")
            Console.WriteLine("Timer1" & Timer1.Enabled.ToString)
            '....
            Timer2.Enabled = True
            Console.WriteLine("Timer2 Now in ENABLED")
            Console.WriteLine("Timer2" & Timer2.Enabled.ToString)
            Console.WriteLine("Buttonnnnnnnnnnnnnnnn-------------------------------------")
        End Sub
    End Class
    


    Think again!

    2012年9月10日 7:46