Posted on 2009-02-16
Last Modified: 2012-05-06
Im using the Serial Port.BytesToRead
If I step through the code im getting the returned data from the device.
When I run my app without breakpoints (Stepping Through) I do not get the info returned.
Is the read to slow for my Hardware. How will I know this ?
public override List<byte> ReadData()


            List<byte> bBuffer = new List<byte>();           

            byte[] data = new byte[sPort.BytesToRead];

            sPort.Read(data, 0, data.Length);


            if (sPort.BytesToRead == 0 && sPort.IsOpen)




            return bBuffer;


Open in new window

Question by:u2envy1
    LVL 29

    Accepted Solution

    This is a common mistake when using the Stream.Read method. The Read method returns the number of bytes actually read, which may be less than the number of bytes requested. If you ignore the return value, you will not read all the data, and you will be using uninitialised data in the array.

    You have to loop until you have got all the bytes that you requested, or until the Read method returns zero which means that the stream has ended.

    The reason that it works when you are debugging is that you pause the code so the entire stream has already arrived when you call the Read method, so you get all the data in one call.
    public override List<byte> ReadData() {
       byte[] data = new byte[sPort.BytesToRead];
       int len, pos = 0;
       while ((len = sPort.Read(data, pos, data.Length - pos)) > 0) {
          pos += len;
       List<byte> result;
       if (sPort.BytesToRead == 0 && sPort.IsOpen) {
          result = new List<byte>(data);
       } else {
          // partial result
          result = new List<byte>(pos);
          for (int i = 0; i < pos; i++) {
       return result;

    Open in new window


    Author Comment

    Thx, That works perfect with one command send to the device.
    Another issue with getting many read backs.
    I have a clocking device. An employee use a card to clock in & out on the clock.
    To get the punches from the clock I run a loop command to get all the punches & I put them into a List<string>
    When stepping through the code I get some punches. When running the app no punches are returned.
    Am I doing the loop too fast ?
    LVL 29

    Assisted Solution

    by:Göran Andersson
    The BytesToRead property contains the number of bytes currently in the buffer, but that might not be all the bytes that you want. If you use the BytesToRead property to determine the size of the data block, you will only get the number of bytes that has been recieved when you start to read. If you expect a data block of a certain size, you should use that as size of the array instead.

    Author Closing Comment

    Thx, All your help is highly appreciated.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now