Ejemplo n.º 1
0
        // Konstruktor
        // vsLine enthält aus Datei gelesene Textzeile
        public HexFileLine(string vsLine)
        {
            int  i;
            uint sum           = 0;
            Byte twoscompl     = 0;
            bool Extended_Flag = false;

            if (vsLine.Length < 9)
            {
                throw new Exception("HexZeile zu kurz");
            }
            if (vsLine[0] != ':')
            {
                throw new Exception("Zeilenbeginn != ':'");
            }
            // Anzahl Bytes
            this._NumBytes = Convert.ToByte(vsLine.Substring(1, 2), 16);

            //sum += (Byte)this._NumBytes;
            // Adresse.

            this._Address = Convert.ToUInt16(vsLine.Substring(3, 4), 16);

            //sum += this._Address;
            //MessageBox.Show(sum.ToString());

            // Recordtype
            i = Convert.ToByte(vsLine.Substring(7, 2), 16);

            if (Enum.IsDefined(this._RecordType.GetType(), i))
            {
                this._RecordType = (HexFile.eRecordType)i;
                //sum += (Byte)i;
            }
            else
            {
                throw new Exception("unrecognized Recordtype");
            }
            //MessageBox.Show(this._RecordType.ToString());
            switch (this._RecordType)
            {
            case HexFile.eRecordType.DataRecord:
                // Datenbytes einlesen
                this._Data = new System.Byte[this._NumBytes];
                for (i = 0; i < this._NumBytes; i++)
                {
                    this._Data[i] = Convert.ToByte(vsLine.Substring(HexFileLine.DataOffset + i * 2, 2), 16);
                    //sum += (Byte)this._Data[i];
                }
                for (i = 0; i < (vsLine.Length - 3) / 2; i++)
                {
                    sum += Convert.ToByte(vsLine.Substring((i * 2) + 1, 2), 16);
                }
                // Checksum einlesen

                this._Checksum = Convert.ToByte(vsLine.Substring(HexFileLine.DataOffset + this._NumBytes * 2, 2), 16);


                //Checksum prüfen

                twoscompl = (Byte)(~sum + 1);
                //MessageBox.Show(twoscompl.ToString() + "  " + this._Checksum.ToString());
                if (twoscompl != this._Checksum)
                {
                    this._errorflag = 1;
                    //MessageBox.Show(Convert.ToString(twoscompl, 2) + "  " + Convert.ToString(this._Checksum, 2));
                }

                break;

            case HexFile.eRecordType.EndOfFileRecord:
                break;

            //case HexFile.eRecordType.ExtendedSegmentAddressRecord:
            //    this._Address = (System.UInt16)(Convert.ToUInt16(vsLine.Substring(HexFileLine.DataOffset, 4), 16) * 16);
            //    break;
            case HexFile.eRecordType.ExtendedLinearAddressRecord:
                Extended_Flag = true;
                this._32BitAdress_UpperBytes = (System.UInt16)(Convert.ToUInt16(vsLine.Substring(HexFileLine.DataOffset, 4), 16));
                break;

            default:
                throw new Exception(string.Format("Recordtype '{0:X}' nicht unterstützt", this._RecordType));
            }

            /*
             * if (Extended_Flag)
             * {
             *  this._Adress.UpperBytes = _32BitAdress_UpperBytes;
             * }
             * */
        }
Ejemplo n.º 2
0
        // Konstruktor
        // vsLine enthält aus Datei gelesene Textzeile
        public HexFileLine(string vsLine)
        {
            int i;
                uint sum = 0;
                Byte twoscompl = 0;
                bool Extended_Flag = false;
                if (vsLine.Length < 9)
                    throw new Exception("HexZeile zu kurz");
                if (vsLine[0] != ':')
                    throw new Exception("Zeilenbeginn != ':'");
                // Anzahl Bytes
                this._NumBytes = Convert.ToByte(vsLine.Substring(1, 2), 16);

                //sum += (Byte)this._NumBytes;
                // Adresse.

                this._Address = Convert.ToUInt16(vsLine.Substring(3, 4), 16);

                //sum += this._Address;
                //MessageBox.Show(sum.ToString());

                // Recordtype
                i = Convert.ToByte(vsLine.Substring(7, 2), 16);

                if (Enum.IsDefined(this._RecordType.GetType(), i))
                {
                    this._RecordType = (HexFile.eRecordType)i;
                    //sum += (Byte)i;
                }
                else
                    throw new Exception("unrecognized Recordtype");
                //MessageBox.Show(this._RecordType.ToString());
                switch (this._RecordType)
                {
                    case HexFile.eRecordType.DataRecord:
                        // Datenbytes einlesen
                        this._Data = new System.Byte[this._NumBytes];
                        for (i = 0; i < this._NumBytes; i++)
                        {
                            this._Data[i] = Convert.ToByte(vsLine.Substring(HexFileLine.DataOffset + i * 2, 2), 16);
                            //sum += (Byte)this._Data[i];
                        }
                        for(i=0 ; i < (vsLine.Length - 3) / 2 ; i++)
                        {

                         sum += Convert.ToByte(vsLine.Substring((i * 2)+1 , 2), 16);
                        }
                        // Checksum einlesen

                        this._Checksum = Convert.ToByte(vsLine.Substring(HexFileLine.DataOffset + this._NumBytes * 2, 2), 16);

                        //Checksum prüfen

                        twoscompl = (Byte)(~sum + 1) ;
                        //MessageBox.Show(twoscompl.ToString() + "  " + this._Checksum.ToString());
                        if(twoscompl != this._Checksum)
                        {
                            this._errorflag = 1;
                            //MessageBox.Show(Convert.ToString(twoscompl, 2) + "  " + Convert.ToString(this._Checksum, 2));
                        }

                        break;
                    case HexFile.eRecordType.EndOfFileRecord:
                        break;
                    //case HexFile.eRecordType.ExtendedSegmentAddressRecord:
                    //    this._Address = (System.UInt16)(Convert.ToUInt16(vsLine.Substring(HexFileLine.DataOffset, 4), 16) * 16);
                    //    break;
                    case HexFile.eRecordType.ExtendedLinearAddressRecord:
                        Extended_Flag = true;
                        this._32BitAdress_UpperBytes = (System.UInt16)(Convert.ToUInt16(vsLine.Substring(HexFileLine.DataOffset, 4), 16));
                        break;
                    default:
                        throw new Exception(string.Format("Recordtype '{0:X}' nicht unterstützt", this._RecordType));
                }

                /*
                if (Extended_Flag)
                {
                    this._Adress.UpperBytes = _32BitAdress_UpperBytes;
                }
                 * */
        }