|
-
May 30th, 2007, 12:59 PM
#1
Thread Starter
Member
SerialDataReceivedEventArgs - How does it work?
If I have an event handler defined as such;
Code:
Private Sub Data_Received(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
end sub
How does it handle multiple bytes?
Say I receive a packet of 10 bytes, Will this event trigger on each byte? or is there a buffer that must be filled for this to trigger? or can I read all incoming bytes while in the sub?
if i have the following code inside the sub...
Code:
While ((sp.BytesToRead <> 0))
packet(i)
i += 1
end while
or would it depend on the time between the received bytes?
-
May 30th, 2007, 03:13 PM
#2
Re: SerialDataReceivedEventArgs - How does it work?
There is a property called ReceiveBytesThreshold, which enables the event to fire when the number of bytes received reach this level. In my opinion, this should probably be set to a value of 1 (which is the default anyway) so no data is missed. (The operating system decides when this event is fired.)
This does not mean the event will fire for every byte, it merely means that it will fire if there is at least one byte available. It may be 1, it may be 6, it may be 10, etc.
When the event is fired, you can read how many bytes are available by querying the BytesToRead property before performing a Read into your own buffer.
-
May 31st, 2007, 01:32 PM
#3
Member
Re: SerialDataReceivedEventArgs - How does it work?
Look here for a detailed listing of how the SerialPort namespace is set up:
http://www.vbforums.com/showthread.php?t=463392
One thing you will want to take into consideration is your connection speed vs. number of bytes in the inbuffer. When ReceivedBytesThreshold fires the OnDataReceived event you may or may not hold all the bytes you are looking for. I would suggest a looping read routine that concatenates the bytes received as they hit the buffer. A quick and dirty way to circumvent this looping read and see what you are actually reading would be to set your ReceivedBytesThreshold to the exact number of bytes you expect and then move on to the looping read later with ReceivedBytesThreshold = 1, the preferred setting.
I have a few other posts on this board that also touch on the SerialPort namespace which may help.
Good luck,
Ike
-
May 31st, 2007, 10:51 PM
#4
Re: SerialDataReceivedEventArgs - How does it work?
 Originally Posted by IkeConn
I have a few other posts on this board that also touch on the SerialPort namespace which may help.
Just to be precise, SerialPort is a class, not a namespace. It is a member of the System.IO.Ports namespace.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|