Results 1 to 4 of 4

Thread: SerialDataReceivedEventArgs - How does it work?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    43

    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?

  2. #2
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  3. #3
    Member
    Join Date
    Dec 2006
    Posts
    56

    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

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

    Re: SerialDataReceivedEventArgs - How does it work?

    Quote 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.
    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

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