Strange entangled code. I cannot see where you add a handler to an invocation list of the instance of the event
DataRecieved
. Why are you trying to read data in the event handler which itself should be called when data is received? Why you invoke it? Invocation will cause a call in the UI thread, but you use a blocking (read) call, which you should not do in a UI thread. All this is wrong.
Also, what is actually read depends on the device on the other end of RS-232 cable. How can you be sure the data is sent from that end?
You know what? I don't think asynchronous APIs makes sense these days when threading is a common place. Do a simple thing: create a separate thread for your serial I/O. In this thread, you can do any blocking calls, such as serial port read operations. I will make your code straightforward, each thread will be sequential. Much easier to implement and debug.
To work with UI, you will need invocation anyway. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method
Invoke
or
BeginInvoke
of
System.Windows.Threading.Dispatcher
(for both Forms or WPF) or
System.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[
^],
Control events not firing after enable disable + multithreading[
^].
—SA