Link to home
Start Free TrialLog in
Avatar of josedelrio
josedelrio

asked on

Problems calling Timer.Enabled=True from SerialPort.DataReceived in VB .NET05

I am trying to enable a timer from a SerialPort.DataReceived rutine. The thing is not working and I suspect it is because I am calling Timer10.Enabled=true from an independent subprocess. I tried to use a delegate defined within a module but I didn't succeed. It is VB .NET 05 and this is the fragment of code:

 Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
comando = SerialPort1.ReadTo(";")
               inicial = Mid(comando, 1, 4)
                Select Case inicial
 Case "VERS"
               Timer1.enabled=false ' This works fine ! but Timer10.Enabled=True don't
                Dim msd As Midelegado = AddressOf Me.ActivaTimer10
                msd.Invoke(True)
                End Select
End Sub

In a Module:
Public Delegate Sub Midelegado(ByVal a As Boolean)

In the Form (Form1):
Sub ActivaTimer10(ByVal a As Boolean)
        Timer10.Enabled = a
End Sub

Of course in the program there are more things but these are the relevant ones for the current case.
The program was made with VB6.0 and converted to .NET05 and then modified to use SerialPort instead of MSComm. I checked the serial port sub is working properly and data is being read. I save to copy the Serial port inicialization and so forth.

I have got no error msg but the Timer is not activated. In fact, after the data arrive in the serial port, I can't activate the Timer even from a button on the form.

Thanks in advance
Josedelrio
Avatar of kdwood
kdwood

Josedelrio,

Where does you serial port routine reside?   On Form1 or in a module?

Regards,

Keith
Avatar of josedelrio

ASKER

kdwood,
On Form1. I dragged SerialPort on the Form1 and then selected the event SerialPort1.ReceivedData.
I hope it helps you to help me.
Thanks in advance.
Regards,
josedelrio
Josedelrio,

Are you sure that your code is making it to the CASE "VERS" section.   Try putting a message box just before the msd.Invoke(True).   The reason I ask this is because I don't see any reason why the timer shouldn't start.  In fact I created a little test project based based on your code which I included below.   The only difference is that I could not simulate the serial port data capture in my sample.  

------
In Form1:

  Private Sub ActivaTimer10(ByVal a As Boolean)

        Me.Timer1.Enabled = a

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myTestVal As Integer = 2
        Dim msd As Midelegado = AddressOf Me.ActivaTimer10

        Select Case myTestVal

            Case 2

                msd.Invoke(True)

        End Select

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        MsgBox("The Timer Has Ticked")

    End Sub

-----------
In Module1:

  Public Delegate Sub Midelegado(ByVal a As Boolean)




I am sure the program enters the "VERS" case. I checked the Timer1 (which is sending a msg through the serial port) and stopts when "VERSION;" is received. Still I put a Debug.Print("I am going through") and I could see it printed in the screen when "VERSION;" is received. It seems that SerialPort1.DataReceived is a separate subprocess in a separate thread, but I have not a clear idea about what that means and which are the implications.
Thanks again
Regards,
josedelrio
Just for further clarification, If you put a Debug.Print message inside of the ActivaTimer10 sub, does it make it there after the msd.Invoke is called?


No, it doesn't. Even if I try to activate the Timer from a button on the form, I can't, AFTER the "VERSION;" msg arrives. It works fine (the button) prior to the arrival though.
ASKER CERTIFIED SOLUTION
Avatar of kdwood
kdwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This works fine in my code just for updating the content of a textBox and must work the same with the timer (I tryed before the textbox and in my way failed too). So I will try soon the complet thing in my application and I am sure it will work fine.
Many thanks for your support, the article you have found is very good.
josedelrio
My pleasure, glad I could help.