Main Site Documentation

FileStream Error Reading Chunks


#1

Having trouble reading chunks of a FileStream using the .Read() method. Code below uses a 1024 chuck, but I have the same issue no matter the size. The first chunk is read just fine. The seconds raises the “System.ArgumentException”. Issue with the arguments I know, but I can’t see what it is. I use nearly the identical program structure to write to the UART and don’t have issues.

The file is 6K and stored on an SD card.

Without running the code below here is what the first 2 passes in the loop have for values on the read method:

Pass 1: Runs fine:

writeCount = FileStream.Read(data, 0, 1024);

Pass 2: Error:

writeCount = FileStream.Read(data, 1024, 1024);

Is there a trick to this in NETMF?



//load file contents to the print buffer stream
            try { Print_Buffer_Stream = new FileStream(rootDirectory + buffer_file, FileMode.Open, FileAccess.Read, FileShare.None); }
            catch (Exception ex)
            {
                SendUART_NAK(b_nul, "SD File Open Error! - " + ex.Message);
            }


                    int offset = 0;
                    int retries = 4;
                    while (offset < Print_Buffer_Stream.Length && retries > 0)
                    {
                        int count = System.Math.Min(1024, (int)Print_Buffer_Stream.Length - offset);
                        int chunkEnd = offset + count;

                        retries = 4;
                        while (offset < chunkEnd && retries-- > 0)
                        {
                            int writeCount = 0;
                            try
                            {
                                data = new byte[chunkEnd - offset];
                                writeCount = Print_Buffer_Stream.Read(data, offset, chunkEnd - offset);
                                usb_dataOut(data);
                                offset += writeCount;
                            }
                            catch { }

                            if (writeCount == 0)
                            {
                                Debug.Print("File chunk failed to send at offset " + offset.ToString() + ", len " + (chunkEnd - offset).ToString());
                                Thread.Sleep(250);
                            }
                            else
                            {
                                Thread.Sleep(0);
                            }
                        }
                    }

                    if (retries > 0)
                    {
                        Debug.Print("usb out completed");
                    }
                    else
                    {
                        Debug.Print("usb out failed");
                    }

Thanks -AP


#2

Try something more like this


byte[] bRead;
FileStream fs;
long bytesRemaining;
int bytesRead;

fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None);
bytesRemaining = fs.Length;

while (bytesRemaining > 0)
{
 bRead = new byte[(bytesRemaining > 1024) ? 1024 : bytesRemaining];
 bytesRead = fs.Read(bRead, 0, bRead.length);
 bytesRemaining -= bytesRead;
}