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..
During debug though I am getting errors with reading the file however:
*(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.)
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).
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
Regards,
Michael
///////////////////// 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 < 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;
}
}
Readreads from the current position in the stream (which is zero upon opening the stream). The second parameter is the offset in the receiving array, not in the stream.Use
Seekto set the current position before reading.The reading and copying code I would rewrite as: