Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1619
  • Last Modified:

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
0
josedelrio
Asked:
josedelrio
  • 5
  • 4
1 Solution
 
kdwoodCommented:
Josedelrio,

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

Regards,

Keith
0
 
josedelrioAuthor Commented:
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
0
 
kdwoodCommented:
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)




0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
josedelrioAuthor Commented:
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
0
 
kdwoodCommented:
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?


0
 
josedelrioAuthor Commented:
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.
0
 
kdwoodCommented:
Ok, check out this text from an article that I found and compare it to your approach.   I also included a full link to the article.


--------------------------
One nice feature of the SerialPort class is that you need not constantly poll for incoming data. Instead, you just need to service the DataReceived event and it will automatically fire when incoming data is detected. However, as this event is running on a separate thread, any attempt to update the main Form directly will result in an error. Hence, you need to use a delegate to update controls on the main thread (Form1):

    '-------------------------------------------
    ' Event handler for the DataReceived
    '-------------------------------------------
    Private Sub DataReceived( _
       ByVal sender As Object, _
       ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
       Handles serialPort.DataReceived

        txtDataReceived.Invoke(New _
                       myDelegate(AddressOf updateTextBox), _
                       New Object() {})
    End Sub

The delegate and the updateTextBox() subroutine is defined as follows:

    '------------------------------------------------------
    ' Delegate and subroutine to update the Textbox control
    '------------------------------------------------------
    Public Delegate Sub myDelegate()
    Public Sub updateTextBox()
        With txtDataReceived
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red
            .AppendText(serialPort.ReadExisting)
            .ScrollToCaret()
        End With
    End Sub

-----------------
Article Link: http://www.devx.com/dotnet/Article/31001

0
 
josedelrioAuthor Commented:
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
0
 
kdwoodCommented:
My pleasure, glad I could help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now