Results 1 to 8 of 8

Thread: Question about event handler

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    27

    Question about event handler

    Hello,

    I want to handle the events of my serial input buffer. I've seen the following article:

    http://msdn.microsoft.com/en-us/libr...VS.100%29.aspx

    My programming skills are not really that great so I've got a question:

    How can I trigger a sub (for example: ArduinoSerialInput) as soon as this event is handled?

    I've tried some code but without success, that's why I decided to post on the forum

    Some pseudocode:
    Code:
    Code:
    Public Delegate Sub SerialDataReceivedEventHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    
        Dim SerialBufferIn As SerialDataReceivedEventHandler = AddressOf ArduinoSerialInput
    
    
        Public Sub ArduinoSerialInput()
    
            MsgBox("test")
    
        End Sub
    I use a piece of hardware to send data to my COM port, but nothing happens.

    Is this a good approach? Some advice or suggestions please

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    27

    Re: Question about event handler

    Code:
    Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    
            MsgBox("test")
    
        End Sub
    This works but is it the best way to go?

    Cheers.

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Question about event handler

    Quote Originally Posted by 2005 user View Post
    Code:
    Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    
            MsgBox("test")
    
        End Sub
    This works but is it the best way to go?

    Cheers.
    Yes. That (the Handles statement) is the most basic way of using an event.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    27

    Re: Question about event handler

    Thanks Nick (dank je!!)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    27

    Re: Question about event handler

    Hello,

    Within this sub I want to append test to a Richtextbox at the end of the sub (Rtb1):

    Code:
    Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    
            Dim str As String = String.Empty
            Dim i As Integer
            Dim bytecount As Integer = SerialPort1.BytesToRead
            Dim byteBuffer(bytecount) As Byte
    
            Dim DisplayInput As String = String.Empty
    
            If bytecount > 0 Then
    
                SerialPort1.Read(byteBuffer, 0, bytecount)
    
            End If
    
            i = 0
    
            For i = 0 To bytecount 'Main for..Next loop to parse all data.
    
                If Convert.ToChar(byteBuffer(i)) = Chr(126) Then
    
                    Do
    
                        DisplayInput = DisplayInput + Convert.ToChar(byteBuffer(i))
    
                        i = i + 1
    
                    Loop Until Convert.ToChar(byteBuffer(i)) = Chr(35)
    
                    'i = i + 1
    
                    DisplayInput = DisplayInput + Chr(35)
    
                    MsgBox(bytecount)
                    MsgBox("i: " & i)
                    MsgBox("Length: " & DisplayInput.Length.ToString)
                    MsgBox("String: " & DisplayInput.ToString)
    
                    Rtb1.AppendText(DisplayInput)
    
                    DisplayInput = String.Empty
    
                End If
    
            Next
    
            i = 0
    
        End Sub
    But I get the following error:

    System.InvalidOperationException was unhandled

    Message = "You are not allowed to perform an operation through various threads, there was another thread from a given access to the control Rtb1 than the thread on which the element was created."

    Why can I not append text to the Richtextbox in this sub? I do not use it anywhere else??

    Thanks

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Question about event handler

    I am thinking that this problem has something to do with the serial port, which I know basically nothing about. So I'm going to leave this (hopefully) to someone with a little more knowledge on the subject if that's ok

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Question about event handler

    The SerialPort raises its events on a secondary thread. As is always the case, if you want to access controls from a secondary thread you need to cross the thread boundary first. The most common way to do that is using the ISynchronizeInvoke interface, i.e. the InvokeRequired property and Invoke method. Follow the CodeBank link in my signature and check out my submission on Accessing Controls From Worker Threads, which explains how it's done.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    27

    Re: Question about event handler

    Ok,

    Got it fixed,

    Thanks!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width