Netduino home hardware projects downloads community

Jump to content

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

SerialPort's BytesToRead property doesn't work correctly?

  • Please log in to reply
3 replies to this topic

#1 jiwonoh


    Advanced Member

  • Members
  • PipPipPip
  • 42 posts

Posted 09 January 2013 - 08:34 AM

void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)   {   try   {   int length = sp.BytesToRead;     if (length == 0)   return;


  sp.Read(buffer, 0, length);     queue.Enqueue(buffer, 0, length);   Interpret();


  sp.DiscardInBuffer();   }   catch (Exception ex)   {   Log.Write("sp_DataReceived", ex.ToString());   }   }




This code a part of my code, and I tried to get whole length of data from submodule sending 16bytes through Uart pin. The sub module is consists of Attiny2313a, and checked it work correctly using logic analyzer.


What I curious is why one of SerialPort instacens named "sp" cannot return BytesToRead property correctly. In my guess, the property should be return 16, but it spilt out numbers fewer than real size or just 1. It makes data collector just get first byte of each packet.


I cannot make sure it is caused by sender or micro framework or characteristics of NP2. I need your clever  idea for checking it.


Thanks, Jiwon.

#2 CW2


    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 09 January 2013 - 11:04 AM

What is the purpose of sp.DiscardInBuffer() call? You are discarding all the data received during Interpret() execution. You'd probably want to have the data processing done in different thread (indicated by the usage of queue).

#3 tim c

tim c

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts

Posted 10 January 2013 - 11:35 PM

It may be that the DataReceived event is firing before all the data has completed sending/receiving. In that case you need to wait for it to fire again to get some more of the data and combine them.

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 11 January 2013 - 01:09 AM

As tim mentioned, SerialPort is asynchronous. So your DataReceived event will get called as soon as one byte is received. So it's quite possible that you'll only have the first few bytes right away. If you want to wait until all the bytes are received, you could put a check at the top of the event which specified a minimum number of bytes and then waited for the next event if not enough bytes were received. For instance...
if (sp.BytesToRead < PACKET_LENGTH)     return;

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.