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
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.
Re: Question about event handler
Quote:
Originally Posted by
2005 user
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.
Re: Question about event handler
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
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 :)
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.
Re: Question about event handler
Ok,
Got it fixed,
Thanks!!