This is starting to get frustrating as it should just work.
I have now suspended all threads during the dial PPP and reduced the display update to only redraw the ProgressBar when the value is incremented. It sleeps for 500ms each loop.
This means that there is no other background tasks running. Only the code below.
I have tried, 9600, 19200, 38400, 57600 and 115200 bps and all give me overrun errors when the PPP connection is started and the HTTP request is sending back the ZIP file I am downloading.
I have RTS and CTS connected to the modem but even using handshaking makes no difference.
The following is the code that I am using that is getting the file.
bool GetFileFromServer(string url)
{
FileStream file = null;
Stream respStream;
byte[] byteData = new byte[2048];
char[] charData = new char[2048];
int bytesRead = 0;
long DownloadLength;
try
{
file = new FileStream("app.hex", FileMode.Create);
}
catch (Exception ex)
{
Debug.Print("Failed to create file to save download to");
return (false);
}
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
//
// Set request.KeepAlive to use a persistent connection.
//
request.KeepAlive = true;
//
// Get a response from the server.
//
WebResponse resp = null;
DLProgressPercent = 0;
try
{
resp = request.GetResponse();
}
catch (Exception e)
{
Debug.Print("Exception in HttpWebRequest.GetResponse(): " +
e.ToString());
}
//
// Get the network response stream to read the page data.
//
if (resp != null)
{
respStream = resp.GetResponseStream();
Decoder UTF8decoder = System.Text.Encoding.UTF8.GetDecoder();
//
// allow 5 seconds for reading the stream
//
respStream.ReadTimeout = 5000;
//
// If we know the content length, read exactly that amount of
// data; otherwise, read until there is nothing left to read.
//
if (resp.ContentLength != -1)
{
DownloadLength = resp.ContentLength;
for (long dataRem = resp.ContentLength; dataRem > 0; )
{
Thread.Sleep(50);
try
{
bytesRead = respStream.Read(byteData, 0, byteData.Length);
dataRem -= bytesRead;
//
// Save the bytes to the file
//
file.Write(byteData, 0, bytesRead);
//
// Work out percentage left
//
DLProgressPercent = 100 - (int)(((double)dataRem / (double)DownloadLength) * 100.0);
}
catch (Exception e)
{
Debug.Print(e.Message);
}
if (bytesRead == 0)
{
Debug.Print("Error: Received " +
(resp.ContentLength - dataRem) + " Out of " +
resp.ContentLength);
break;
}
}
}
else
{
//
// Read until the end of the data is reached.
//
while (true)
{
//
// If the Read method times out, it throws an exception,
// which is expected for Keep-Alive streams because the
// connection isn't terminated.
//
try
{
Thread.Sleep(1);
bytesRead = respStream.Read(byteData, 0, byteData.Length);
}
catch (Exception)
{
bytesRead = 0;
}
//
// Zero bytes indicates the connection has been closed
// by the server.
//
if (bytesRead == 0)
{
break;
}
//
// Save the bytes to the file
//
file.Write(byteData, 0, bytesRead);
}
}
//
// Close the file on FLASH
//
file.Close();
//
// Close the response stream. For Keep-Alive streams, the
// stream will remain open and will be pushed into the unused
// stream list.
//
resp.Close();
}
Any and all help appreciated.