//// Utility function to write an Intel HEX8 record to a file //public IntelHexStructure Write(StreamWriter outStream) //{ // int i; // // Check our record pointer and file pointer // if (irec == null || outStream == null) // { // status = IHEX_ERROR_INVALID_ARGUMENTS; // return null; // } // // Check that the data length is in range // if (irec.dataLen > IHEX_MAX_DATA_LEN / 2) // { // status = IHEX_ERROR_INVALID_RECORD; // return null; // } // try // { // // Write the start code, data count, address, and type fields // outStream.Write(string.Format("{0}{1:X2}{2:X4}{3:X2}", IHEX_START_CODE, irec.dataLen, irec.address, irec.type)); // // Write the data bytes // for (i = 0; i < irec.dataLen; i++) // outStream.Write(string.Format("{0:X2}", irec.data[i])); // // Last but not least, the checksum // outStream.WriteLine(string.Format("{0:X2}", Checksum())); // } // catch (Exception) // { // status = IHEX_ERROR_FILE; // return null; // } // status = IHEX_OK; // return irec; //} ///// <summary> ///// Utility function to print the information stored in an Intel HEX8 record ///// </summary> ///// <param name="verbose">A boolean set to false by default, if set to true will provide extended information.</param> ///// <returns>String which provides the output of the function, this does not write directly to the console.</returns> //public string Print(bool verbose = false) //{ // int i; // string returnString; // if (verbose) // { // returnString = string.Format("Intel HEX8 Record Type: \t{0}\n", irec.type); // returnString += string.Format("Intel HEX8 Record Address: \t0x{0:X4}\n", irec.address); // returnString += string.Format("Intel HEX8 Record Data: \t["); // for (i = 0; i < irec.dataLen; i++) // { // if (i + 1 < irec.dataLen) // returnString += string.Format("0x{0:X02}, ", irec.data[i]); // else // returnString += string.Format("0x{0:X02}", irec.data[i]); // } // returnString += string.Format("]\n"); // returnString += string.Format("Intel HEX8 Record Checksum: \t0x{0:X2}\n", irec.checksum); // } // else // { // returnString = string.Format("{0}{1:X2}{2:X4}{3:X2}", IHEX_START_CODE, irec.dataLen, irec.address, irec.type); // for (i = 0; i < irec.dataLen; i++) // returnString += string.Format("{0:X2}", irec.data[i]); // returnString += string.Format("{0:X2}", Checksum()); // } // status = IHEX_OK; // return (returnString); //} /// <summary> /// An internal utility function to calculate the checksum of an Intel HEX8 record /// </summary> /// <returns>byte which is the checksum of IntelHexStructure.</returns> internal byte Checksum(IntelHexStructure irec) { byte checksum; int i; // Add the data count, type, address, and data bytes together checksum = (byte)irec.dataLen; checksum += (byte)irec.type; checksum += (byte)irec.address; checksum += (byte)((irec.address & 0xFF00) >> 8); for (i = 0; i < irec.dataLen; i++) { checksum += irec.data[i]; } // Two's complement on checksum checksum = (byte)(~checksum + 1); return(checksum); }
public byte[] GetDataSegment(StreamReader inStream) { string recordBuff; int dataCount, i; // Check our record pointer and file pointer if (inStream == null) { return(new List <byte>().ToArray()); } try { List <byte> list = new List <byte>(); // Read Line will return a line from the file. while ((recordBuff = inStream.ReadLine()) != null) { // Check if we hit a newline if (recordBuff == null || recordBuff.Length == 0) { continue; } // Size check for start code, count, address, and type fields if (recordBuff.Length < (1 + IHEX_COUNT_LEN + IHEX_ADDRESS_LEN + IHEX_TYPE_LEN)) { continue; } // Check the for colon start code if (recordBuff[IHEX_START_CODE_OFFSET] != IHEX_START_CODE) { continue; } // Copy the ASCII hex encoding of the count field into hexBuff, convert it to a usable integer dataCount = Convert.ToInt16(recordBuff.Substring(IHEX_COUNT_OFFSET, IHEX_COUNT_LEN), 16); IntelHexStructure irec = new IntelHexStructure(); // Copy the ASCII hex encoding of the address field into hexBuff, convert it to a usable integer irec.address = Convert.ToUInt16(recordBuff.Substring(IHEX_ADDRESS_OFFSET, IHEX_ADDRESS_LEN), 16); // Copy the ASCII hex encoding of the address field into hexBuff, convert it to a usable integer irec.type = Convert.ToInt16(recordBuff.Substring(IHEX_TYPE_OFFSET, IHEX_TYPE_LEN), 16); // Size check for start code, count, address, type, data and checksum fields if (recordBuff.Length < (1 + IHEX_COUNT_LEN + IHEX_ADDRESS_LEN + IHEX_TYPE_LEN + dataCount * 2 + IHEX_CHECKSUM_LEN)) { continue; } // Loop through each ASCII hex byte of the data field, pull it out into hexBuff, // convert it and store the result in the data buffer of the Intel HEX8 record for (i = 0; i < dataCount; i++) { // Times two i because every byte is represented by two ASCII hex characters byte value = Convert.ToByte(recordBuff.Substring(IHEX_DATA_OFFSET + 2 * i, IHEX_ASCII_HEX_BYTE_LEN), 16); irec.data[i] = value; list.Add(value); } irec.dataLen = dataCount; // Copy the ASCII hex encoding of the checksum field into hexBuff, convert it to a usable integer irec.checksum = Convert.ToByte(recordBuff.Substring(IHEX_DATA_OFFSET + dataCount * 2, IHEX_CHECKSUM_LEN), 16); if (irec.checksum != Checksum(irec)) { continue; } irecList.Add(irec); } return(list.ToArray()); } catch (Exception) { return(new List <byte>().ToArray()); } }