示例#1
0
        private static void TestIntelHex(string fileName, byte[] data)
        {
            IntelHex irec = new IntelHex();
            StreamWriter sw = new StreamWriter(fileName);

            // Create a new type-0 record with data
            IntelHexStructure irecs = irec.NewRecord(0, 0, data, data.Length);
            irec.Write(sw);

            // Create another type-0 record with new data
            irec.NewRecord(0, 8, data, data.Length);
            irec.Write(sw);

            // Create another type-0 record with new data
            irec.NewRecord(0, 16, data, data.Length);
            irec.Write(sw);

            // Create an end of record type-1 record
            irec.NewRecord(1, 0, null, 0);
            irec.Write(sw);
            sw.Close();

            Console.WriteLine("Wrote Intel HEX formatted file: " + fileName);
            Console.WriteLine("Reading back Intel HEX file:");

            // Open up the new file and attempt to read the records and print to the console
            StreamReader sr = new StreamReader(fileName);
            irecs = irec.Read(sr);
            Console.WriteLine((irecs != null) ? irec.Print() : "Could not read record!");
            irecs = irec.Read(sr);
            Console.WriteLine((irecs != null) ? irec.Print() : "Could not read record!");
            irecs = irec.Read(sr);
            Console.WriteLine((irecs != null) ? irec.Print() : "Could not read record!");
            irecs = irec.Read(sr);
            Console.WriteLine((irecs != null) ? irec.Print() : "Could not read record!");
            sr.Close();
        }
示例#2
0
        static void Main(string[] args)
        {
            AtmelGeneric generic;
            IntelHex ihex;
            SRecord srec;
            StreamReader sr = null;

            if (args.Length < 2)
            {
                Console.WriteLine("Usage: TestGIS_RecDump.exe <file format> <file>");
                Console.WriteLine("This program will print the records saved in a generic, Intel HEX, or Motorola\nS-Record formatted file.\n");
                Console.WriteLine("  <file format> can be generic, ihex, or srecord.");
                Console.WriteLine("  <file> is the path to the formatted object file.");
                Environment.Exit(-1);
            }

            try
            {
                sr = new StreamReader(args[1]);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error opening file: " + e.Message);
                Environment.Exit(-1);
            }

            if (string.Compare(args[0], "generic") == 0)
            {
                generic = new AtmelGeneric();
                AtmelGenericStructure gen_s;
                while (true)
                {
                    gen_s = generic.Read(sr);
                    if (gen_s != null)
                        Console.WriteLine(generic.Print(true));
                    else
                        break;
                }

            }
            else if (string.Compare(args[0], "ihex") == 0)
            {
                ihex = new IntelHex();
                IntelHexStructure ihex_s;
                while (true)
                {
                    ihex_s = ihex.Read(sr);
                    if (ihex_s != null)
                        Console.WriteLine(ihex.Print(true));
                    else
                        break;
                }
            }
            else if (string.Compare(args[0], "srecord") == 0)
            {
                srec = new SRecord();
                SRecordStructure srec_s;
                while (true)
                {
                    srec_s = srec.Read(sr);
                    if (srec_s != null)
                        Console.WriteLine(srec.Print(true));
                    else
                        break;
                }
            }
            else
            {
                Console.WriteLine("Unknown file format specified!");
                sr.Close();
                Environment.Exit(-1);
            }

            sr.Close();
        }
        private void btnWrite_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _startTime = DateTime.Now;
                DisableButtons();

                if (Path.GetExtension(openFileDialog1.FileName).Equals(".hex"))
                {
                    _HexFileReader = new StreamReader(openFileDialog1.FileName);
                    IntelHex irec = new IntelHex();
                    IntelHexStructure irecs = irec.Read(_HexFileReader);
                    int blocksRead = 0;
                    long totalLength = 0;
                    while (irecs != null)
                    {
                        blocksRead++;
                        if (irecs.type == 1)
                        {
                            //EOF
                        }
                        else if (irecs.type == 0)
                        {
                            //data
                            totalLength += irecs.dataLen;
                        }
                        else if (irecs.type == 4)
                        {
                            //extended linear address
                        }
                        irecs = irec.Read(_HexFileReader);
                    }
                    _HexFileReader.Close();

                    _image = new byte[totalLength];

                    _HexFileReader = new StreamReader(openFileDialog1.FileName);
                    irec = new IntelHex();
                    irecs = irec.Read(_HexFileReader);
                    blocksRead = 0;
                    long extLinearAddr = 0;
                    while (irecs != null)
                    {
                        blocksRead++;
                        if (irecs.type == 1)
                        {
                            //EOF
                        }
                        else if (irecs.type == 0)
                        {
                            //data
                            for (long i = 0; i < irecs.dataLen; i++)
                            {
                                _image[extLinearAddr + irecs.address + i] = irecs.data[i];
                            }
                        }
                        else if (irecs.type == 4)
                        {
                            //extended linear address
                            extLinearAddr = (UInt16)((irecs.data[1] << 8 & 0xFF00) | (irecs.data[0] & 0xFF));
                            extLinearAddr = extLinearAddr << 8;
                        }
                        irecs = irec.Read(_HexFileReader);
                    }
                    _HexFileReader.Close();
                }
                else
                {
                    //int total_size = 1024 * 128;
                    //BinaryReader br = new BinaryReader(File.Open(openFileDialog1.FileName, FileMode.Open));
                    //_image = new byte[total_size];
                    //if (total_size != br.Read(_image, 0, total_size))
                    //{
                        //MessageBox.Show("Incorrect binary file size");
                        //EnableButtons();
                    //}

                    _image = File.ReadAllBytes(openFileDialog1.FileName);

                    //byte[] tmpimage = File.ReadAllBytes(openFileDialog1.FileName);
                    //round up to the whole number of Kb as there on Arduino side writings are done in bursts of 1K
                    //TODO: implement better logic to commit incomplete bursts
                    //int image_sz_kb = tmpimage.Length / 1024;
                    //if (image_sz_kb * 1024 < tmpimage.Length) image_sz_kb+=2;

                    //_image = new byte[image_sz_kb * 1024];
                    //Array.Copy(tmpimage, _image, tmpimage.Length);
                }

                string readFilePath = Path.Combine(
                     Path.GetDirectoryName(openFileDialog1.FileName),
                     "image.bin");
                try
                {
                    if (File.Exists(readFilePath)) File.Delete(readFilePath);
                }
                catch
                {
                    ;
                }

                _BinFile = new BinaryWriter(new FileStream(readFilePath, FileMode.CreateNew));
                _BinFile.Write(_image, 0, _image.Length);
                _BinFile.Flush();
                _BinFile.Close();

                _mode = MODE.WRITE;
                byte[] frame = new byte[4] { (byte)COMMANDS.CMD_CHIP_INFO, 0, 0, 0 };
                serialPort1.Write(frame, 0, frame.Length);

                SetStatusLabel("Getting chip info...");
            }
        }