locked
SerialPort.BytesToRead returns 0 even though data in the buffer? RRS feed

  • Question

  • I have written a C# program that decodes 2D barcodes from a handheld barcode scanner.  The program works well except that every once in a while I will find a barcode that when scanned the call SerialPort.BytesToRead returns zero even though the read was successful and data was stored in the serial buffer.  This problem is quite vexing as it does not occur randomly; it occurs only on certain barcodes.  I have been scanning mainly 2D datamatrix type barcodes and once in a while I will find one or two that this issue raises itself.  Does anyone have any ideas on what could be causing this.  Thanks!

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {   
                bufsize = serialPort1.BytesToRead;         //integer of number of bytes
              
                for (int i = 0; i < bufsize; ++i)            
                {
                    scanbuf[i] = (Byte)serialPort1.ReadByte();
                }

                this.Invoke(new EventHandler(fetchData));  
            } 

    Sunday, August 21, 2011 4:28 PM

All replies

  • It can happen.  Code for it.

    What read is successful?  If bufxize is 0 how do you get to the ReadByte statement?

    Sunday, August 21, 2011 8:30 PM
  • I found the problem, the event handler is firing twice.  The first time through it gets the data and clears the buffer, so when the second event occurs (I haven't figured out why the second event is firing) the buffer is empty.  Hence, the DataReadBytes now will return 0;  I have now added the following to check for this:

    if (serialPort1.BytesToRead == 0)
        return;  

    I need to look into what other events could cause the handler to fire.

    Thanks.

     

    Sunday, August 21, 2011 9:07 PM
  • This is the suspicious line:  this.Invoke(new EventHandler(fetchData)); 

    Sunday, August 21, 2011 9:34 PM
  • Hello Datatree,

    Do you resolve the issue? And any update about it, please?

     


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 29, 2011 2:23 AM
  • Hi Datatree,

    I ran into the same problem reading data from a device. It's not a barcode scanner but still exibits the same issue. Reading most of the packets works fine but on some packets the port.BytesToRead returns 0 even though the previous DataReceived event read all the bytes. The packet is only 8 bytes long. The other 8 byte packets work fine. The protocol has many different length packets but I have only seen this on a few of the 8 byte packets and not all of them. For now I did what you did and just return if I see it return 0 and I know that the previous read got them all.

    I see the same thing. The event handler is firing twice for some reason and the second time it fires there are no bytes in the buffer to read so BytesToRead returns 0. This makes sense of course but I don't know why the event handler is fired twice.

    Thanks for you post. I'll post again if I find why this happens.

    Don

    Thursday, January 5, 2012 8:35 PM
  • Check to see if you have 0 bytes, if you do might want to check the EventType property for the DataReceivedEventArgs passed into the event. Might be that you're just getting an EOF and that's it. 
    Thursday, January 5, 2012 8:46 PM