Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

SerialPort.DataReceived

SerialPort documentation

  • Please log in to reply
2 replies to this topic

#1 MLA44

MLA44

    Member

  • Members
  • PipPip
  • 10 posts

Posted 06 November 2014 - 02:37 PM

Hi all,

 

Can anyone guide me to some documentation or explain to me how I can figure out what is going on inside SerialPort.DataRecieved?

 

I use it as follows:

serial.DataReceived += new SerialDataReceivedEventHandler(SerialRecieveHandler);

I have an Arduino (Leonardo) sending data on serial interface to the Netduino+2 periodically (once pr sec).

 

Inside SerialRecieveHandler() I do like this:

if (serial.BytesToRead > 0)
{
    while (serial.BytesToRead > 0)
    {
        // read data
    }
    // process data
}
else
//write log to SD

My problem is that it seams like the event handler is triggered twice since:

- I get the data

- and I get the log in "else"

- every second.

I did see something on the forum about SerialHelper class for buffering data. Is my problem that the event handler is triggered on the first byte and again set in queue "later"? In this case I'm just lucky that my data is not cut in half! I don't like the uncertainty.

My full string that I receive every second is 9 bytes and it does get read right and converted to an int. I'm just wondering why the event handler is triggered twice.

 

So, how does it work and should I be using it differently?

 

PS, I use VS2010, NETMF 4.2 and firmware 4.2.2



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 07 November 2014 - 08:32 PM

Hi MLA44,

Yes, the event is fired as soon as data starts arriving. With serial bytes, the framework doesn't know how much data you're going to get. And it queues up another event once more data arrives.

Because of that, and because you're checking for "more" received data in a loop, multiple fired events is a normal thing. Just exit the event handler if there's no data left and you should be good to go.

And if you're waiting for a specific amount of data then go ahead and store the data you get in an appropriately-scope byte array...and then process the data all at once when you've received everything you need. [Also...in case you got some data from the NEXT batch of data...go ahead and move that to the front of your buffer.]

Does that get you started?

Welcome to the Netduino community,

Chris

#3 MLA44

MLA44

    Member

  • Members
  • PipPip
  • 10 posts

Posted 11 November 2014 - 11:39 AM

Hi Chris,

 

That was exactly what I was thinking. Nice to have it confirmed. Thank you.

 

Regards,

Martin







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.