Reading a file 16 bytes at a time issue
<p>I'm in the process of writing a C# serial interface for FPGA project I'm working on and I've hit an issue. I need to read a file 16 bytes at a time, send the 16 bytes and then upon correct receipt send the next 16 bytes etc..</p>

<p>During debug though I am getting errors with reading the file however:</p>

<p>*(Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.)</p>

<p>The file is about 1200 bytes and the error is occurring on the second time the program tries to read 16 bytes ( i.e. bytes 16-31 of the file).</p>

<p>Any Ideas? Is using an offset with a count the wrong way to do this? I am extremely inexperienced with high level programming so you help would be much appreciated</p>

<p>Regards,
Michael</p>

<pre><code>        ///////////////////// Get file data ///////////////////
        else if (fileDataSent == false)
        {
            FileStream fs = File.OpenRead(tbFileToSend.Text);
            try
            {
                byte[] readBytes = new byte[16];
                fs.Read(readBytes, nextReadOffset, 16);
                fs.Close();
                for (int j = 0; j &lt; 16; j++)
                {
                    sendData[j + 3] = readBytes[j];
                }
            }
            finally
            {
                fs.Close();
            }
            nextReadOffset = nextReadOffset + 16;
            dataBytesSent = dataBytesSent + 16;
            sendData[0] = Convert.ToByte("10000001", 2);
            sendData[1] = ByteID;
            sendData[2] = Convert.ToByte("11000011", 2);
            sendData[19] = Convert.ToByte("11100111", 2);
            ByteID++;
            if (dataBytesSent == WriteFileSize)
            {
                fileDataSent = true;
            }
        }
</code></pre>
