• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4818
  • Last Modified:

SerialPort.BytesToRead

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);
            bBuffer.AddRange(data);
 
            if (sPort.BytesToRead == 0 && sPort.IsOpen)
            {
                sPort.Close();
            }          
 
            return bBuffer;
 
        }

Open in new window

0
u2envy1
Asked:
u2envy1
  • 2
  • 2
2 Solutions
 
Göran AnderssonCommented:
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) {
      sPort.Close();
      result = new List<byte>(data);
   } else {
      // partial result
      result = new List<byte>(pos);
      for (int i = 0; i < pos; i++) {
         result.Add(data[i]);
      }
   }
   return result;
}

Open in new window

0
 
u2envy1Author Commented:
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 ?
0
 
Göran AnderssonCommented:
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.
0
 
u2envy1Author Commented:
Thx, All your help is highly appreciated.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now