2
votes

I am sending data through a COM port and starting my timer with a 4000 interval to send my message back if I did not receive anything before the 4 seconds.

{Load}

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    [...]    
    SendCard(0)
    [...]
End Sub

{SendCard Sub} I'm sending the require information through this Sub by setting the property "LastCommand" which sends the data through the port.

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

The property sends the data and start the Timer.

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

When I receive an answer I stop the timer and then ask the information for the next card.

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

The tick event works fine for the first card

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

So it's asking my first command every 4 second until I get something in return, but when I start over with the second command the Timer does not tick.

I'm using .NET 4.0 and the {Windows.Forms.Timer}, I tried with Timers.Timer and did not work either I don't understand why it does not start again.

2
I've manage to make it work by using System.Timers.Timer I guess I did something wrong the first time. - Gabriel Paquet-St-Onge

2 Answers

0
votes

In property 'LastCommand' check value not equals lastCommandString and also if ResponseTimer isnot enabled, maybe it will help:

Public Property LastCommand() As String
    Get
        Return lastCommandString
    End Get
    Set(ByVal value As String)
        if value = lastCommandString then exit property
        lastCommandString = value
        MainCOMSerialPort.Write(value)
        if Not ResponseTimer.Enabled Then ResponseTimer.Start()
    End Set
End Property
0
votes

It looked like System.Timers.Timer was the best options I managed to make the elapse event trigger every 4 second after I sent a command every time I do so.