Example #1
0
        public static bool ReadHexAndVerify(string fileName)
        {
            try
            {
                FileInfo   hexFile     = new FileInfo(fileName);
                TextReader hexRead     = hexFile.OpenText();
                string     fileLine    = hexRead.ReadLine();
                bool       verified    = true;
                int        lastAddress = 0;
                while (fileLine != null)
                {
                    if ((fileLine[0] == ':') && (fileLine.Length >= 11))
                    { // skip line if not hex line entry,or not minimum length ":BBAAAATTCC"
                        int byteCount   = Int32.Parse(fileLine.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);
                        int fileAddress = Int32.Parse(fileLine.Substring(3, 4), System.Globalization.NumberStyles.HexNumber);
                        int recordType  = Int32.Parse(fileLine.Substring(7, 2), System.Globalization.NumberStyles.HexNumber);

                        if (recordType == 0)
                        {                                                // Data Record}
                            if ((fileAddress >= 0x2000) && (fileAddress < 0x7FE0))
                            {                                            // don't check bootloader stuff.
                                int startByte    = fileAddress & 0x000F; // read 16 bytes at a time.
                                int firstAddress = fileAddress & 0xFFF0;
                                if (lastAddress != firstAddress)
                                { // only read if next line in different 16-byte block
                                    Pk2.BL_ReadFlash16(firstAddress);
                                }
                                if (fileLine.Length >= (11 + (2 * byteCount)))
                                { // skip if line isn't long enough for bytecount.
                                    for (int lineByte = 0; lineByte < byteCount; lineByte++)
                                    {
                                        // get the byte value from hex file
                                        uint wordByte = UInt32.Parse(fileLine.Substring((9 + (2 * lineByte)), 2), System.Globalization.NumberStyles.HexNumber);
                                        if (Pk2.Usb_read_array[6 + startByte + lineByte] != (byte)(wordByte & 0xFF))
                                        {
                                            verified   = false;
                                            recordType = 1;
                                            break;
                                        }
                                    }
                                }
                                lastAddress = firstAddress;
                            }
                        } // end if (recordType == 0)



                        if (recordType == 1)
                        { // end of record
                            break;
                        }
                    }
                    fileLine = hexRead.ReadLine();
                }
                hexRead.Close();
                return(verified);
            }
            catch
            {
                return(false);
            }
        }