[title]UPDATE: The information in this post is no longer required. The code on the wiki has been updated.[/title]
[line]
I started playing with my shiny new DL40, but I hit a snag when trying to upload the standard firmware, it would either hang at the ‘Connecting…’ stage or fail the ‘Verification Stage’. Investigating the code I realized that the issue is with the Read method in the SerialPortToNXP class or rather with the code that calls Read and assumes that the read would get all the data in a single call.
When retrieving larger buffers, sometimes multiple reads are required to fetch the requested amount of data. Since there are many different places in the code that call read I opted to make the read function read until it as received all the data or has timed-out.
The original code is as follows
public void Read(byte[] buffer, int offset, int size, int timeout)
{
//serial.ReadTimeout = timeout;
int count;
if ((count = serial.Read(buffer, offset, size)) < size)
throw new Exception("Timeout");
}
I have modified it to the following, which seems to work fine.
public void Read(byte[] buffer, int offset, int size, int timeout)
{
DateTime startTime = DateTime.Now;
do
{
int bytesRead = serial.Read(buffer, offset, size);
offset += bytesRead;
size -= bytesRead;
if ((DateTime.Now - startTime).Milliseconds > timeout)
throw new Exception("Timeout");
} while (size > 0);
if (size != 0) throw new Exception("Data size mismatch");
}