none
Windows application with serial port and three forms RRS feed

  • Question

  • I have a form1 with serial port 1(inserted through tool box) which connects with microcomputer. on serial port receive event

    "Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    End Sub"

    on serial port receive event,I want  do specific actions depending of the data received.Like some specific data received, I want to change back color of each button in form2 or form 3 etc.

    I tried to do it by calling a function through SerialPort1_DataReceived function of form1 like "form2.updatebuttons()" but nothing happens

    when i call the same function from inside Button1_Click (...)of form2 it works fine.

    plz, help me to update form2 and form2 through form1

    thanks.

    Thursday, August 16, 2012 1:29 PM

Answers

  • Hello,

    I'm not sure but I think You retrieve a illegal crossthread error. The DataReceived handle procedure runs on a secondary thread. Your UI element runs on the main UI thread. Therefore You have to follow the rules of threading.  Plwase try this:

      Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
                                             ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                             Handles SerialPort1.DataReceived
         
                Try
    
                    readBuffer = SerialPort1.ReadLine()
    
                Catch ex As InvalidOperationException
                    MsgBox(ex.Message & " #Readdata")
                    Exit Sub
                End Try
                Me.Invoke(New EventHandler(AddressOf DoUpdate))
           
        End Sub
    	
    	Private Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
    	
    	' call method for UI thread here
    	
    	End Sub

    I hope this helps

    regards Ellen


    VB2008 Express, VB2010 Express, VST2008 professional Ellens Codegallerie

    Friday, August 17, 2012 9:37 AM

All replies

  • Hello Mumbai,

    please post the code of function updateButtons

    regards Ellen


    VB2008 Express, VB2010 Express, VST2008 professional Ellens Codegallerie

    Thursday, August 16, 2012 1:46 PM
  • to change the button color of form2 or form3.I  call this following function of a class"custom color"

        Shared Sub SetBoardStatus(ByVal oContainer As Control, ByVal cmd As String)
            Dim value As ULong
            Dim lsw, msw, cmdvalue As String
            If cmd.Length() >= 8 Then
                lsw = cmd.Substring(0, 4)
                msw = cmd.Substring(4, 4)
                cmdvalue = msw + lsw
                value = Convert.ToInt64(cmdvalue, 16)
                Dim gBOX As GroupBox = oContainer
                Dim count As Integer = gBOX.Controls.Count()
                For i As Integer = 0 To (count - 1)
                    If (value And Convert.ToInt64(Math.Pow(2, i))) Then
                        gBOX.Controls.Item(i).BackColor = Color.Gray
                    Else
                        gBOX.Controls.Item(i).BackColor = Color.GreenYellow
                    End If
                    gBOX.Controls.Item(i).ForeColor = Color.Blue
                Next
            End If
        End Sub

    Friday, August 17, 2012 5:55 AM
  • Hello,

    I'm not sure but I think You retrieve a illegal crossthread error. The DataReceived handle procedure runs on a secondary thread. Your UI element runs on the main UI thread. Therefore You have to follow the rules of threading.  Plwase try this:

      Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
                                             ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                             Handles SerialPort1.DataReceived
         
                Try
    
                    readBuffer = SerialPort1.ReadLine()
    
                Catch ex As InvalidOperationException
                    MsgBox(ex.Message & " #Readdata")
                    Exit Sub
                End Try
                Me.Invoke(New EventHandler(AddressOf DoUpdate))
           
        End Sub
    	
    	Private Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
    	
    	' call method for UI thread here
    	
    	End Sub

    I hope this helps

    regards Ellen


    VB2008 Express, VB2010 Express, VST2008 professional Ellens Codegallerie

    Friday, August 17, 2012 9:37 AM
  • I put your code into Class1 and used the code below to call the function and it worked with no problems.  I did get a warning message when I used th efollowing

     classabc.SetBoardStatus(Me.GroupBox1, "123456789")

    The warning said " Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated."  This may be what is happening in your case.  I changed the code from classabe to Class1 to eliminate the warning message.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim classabc As Class1 = New Class1()
            Class1.SetBoardStatus(Me.GroupBox1, "123456789")
        End Sub

    jdweng

    Friday, August 17, 2012 9:39 AM
  • The handler for the DataReceived event runs on a thread pool thread. From that thread you are not allowed to call methods on the UI thread except for the four thread safe methods - BeginInvoke, Invoke, EndInvoke and CreateGraphics.

    In your case, use BeginInvoke or Invoke to either "wake" a method on the UI thread when the handler for the DataReceived event is triggered (use the standard delegate MethodInvoker) and receive the serialport data in the UI method, or receive the data in the handler for the DataReceived event and make your own delegate to transfer the received data to a method on the UI thread.

     

    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    Friday, August 17, 2012 9:44 AM
  • thanks for ur reply..........

    Friday, August 17, 2012 10:57 AM
  • thanks to all of u for ur reply..........
    Friday, August 17, 2012 10:58 AM
  • @ Ellen Ramcke and others

    Just a hint: 

    Since you don't need information about the instance that calls Invoke in your (Ellens) example (you know that it is the handler for the DataReceived event), it is more efficient to use the MethodInvoker delegate, which has no arguments, instead of the EventHandler delegate. This cuts some corners internal in .Net and speed things up.

    You also miss the "usual" warning that you should close the port from a different thread to avoid a deadlock when you use Invoke (instead of BeginInvoke). Many are not aware of this and it is difficult to find examples showing how to do this (I have it in my tutorial, but have not seen it elsewhere).


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    Friday, August 17, 2012 11:32 AM
  • @ Ellen Ramcke and others

    Just a hint: 

    Since you don't need information about the instance that calls Invoke in your (Ellens) example (you know that it is the handler for the DataReceived event), it is more efficient to use the MethodInvoker delegate, which has no arguments, instead of the EventHandler delegate. This cuts some corners internal in .Net and speed things up.

    You also miss the "usual" warning that you should close the port from a different thread to avoid a deadlock when you use Invoke (instead of BeginInvoke). Many are not aware of this and it is difficult to find examples showing how to do this (I have it in my tutorial, but have not seen it elsewhere).


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    Hi Karsten,

    I agree to You. Your tutorial is excellent. I have not seen a better description of threading elsewhere. Always when I have some time I study this.

    Thanks and regards Ellen


    VB2008 Express, VB2010 Express, VST2008 professional Ellens Codegallerie

    Friday, August 17, 2012 1:09 PM