예제 #1
0
        /// <summary>
        /// Parses a
        /// </summary>
        /// <param name="hexRecord">The </param>
        /// <returns></returns>
        public static IntelHexRecord ParseHexRecord(this string hexRecord)
        {
            if (hexRecord == null)
            {
                throw new IOException("Hex record line can not be null");
            }
            if (hexRecord.Length < 11)
            {
                throw new IOException($"Hex record line length [{hexRecord}] is less than 11");
            }
            if (!hexRecord.StartsWith(":"))
            {
                throw new IOException($"Illegal line start character [{hexRecord}]");
            }

            var hexData = TryParseData(hexRecord.Substring(1));

            if (hexData.Count != hexData[0] + 5)
            {
                throw new IOException($"Line [{hexRecord}] does not have required record length of [{hexData[0] + 5}]");
            }

            if (!Enum.IsDefined(typeof(IntelHexRecordType), (int)hexData[3]))
            {
                throw new IOException($"Invalid record type value: [{hexData[3]}]");
            }

            var checkSum = hexData[hexData[0] + 4];

            hexData.RemoveAt(hexData[0] + 4);

            if (!VerifyChecksum(hexData, checkSum))
            {
                throw new IOException($"Checksum for line [{hexRecord}] is incorrect");
            }

            var dataSize = hexData[0];

            var newRecord = new IntelHexRecord
            {
                ByteCount  = dataSize,
                Address    = (uint)((hexData[1] << 8) | hexData[2]),
                RecordType = (IntelHexRecordType)hexData[3],
                Data       = hexData,
                CheckSum   = checkSum
            };

            hexData.RemoveRange(0, 4);

            return(newRecord);
        }
예제 #2
0
        private uint HandleAddress(IntelHexRecord hexRecord)
        {
            uint result = 0;

            switch (hexRecord.RecordType)
            {
            case IntelHexRecordType.Data:
                result = _addressBase + hexRecord.Address;
                break;

            case IntelHexRecordType.ExtendedSegmentAddress:
                _addressBase = (uint)((hexRecord.Data[0] << 8) | hexRecord.Data[1]) << 4;
                result       = _addressBase;
                break;

            case IntelHexRecordType.StartSegmentAddress:
                result = _addressBase;
                break;

            case IntelHexRecordType.ExtendedLinearAddress:
                _addressBase = (uint)((hexRecord.Data[0] << 8) | hexRecord.Data[1]) << 16;
                result       = _addressBase;
                break;

            case IntelHexRecordType.StartLinearAddress:
                _addressBase = (uint)((hexRecord.Data[0] << 24) | (hexRecord.Data[1] << 16) |
                                      (hexRecord.Data[2] << 8) | hexRecord.Data[3]);
                result = _addressBase;
                break;

            default:
                throw new ArgumentOutOfRangeException($"Unknown value read for [{nameof(hexRecord.RecordType)}]");
            }

            return(result);
        }