Exemple #1
0
 public int calcCRC(HexFile hf)
 {
     int crc=0;
     for (ulong i=hf.getAddrLower(); i <= hf.getAddrUpper(); i++) {
         crc = crc16_update(crc, hf.getByte(i));
     }
     return crc;
 }
Exemple #2
0
        private static bool parseInput(string instr)
        {
            if (instr.Equals("load")) {
                Console.WriteLine("loading...");
                //string filepath = "/home/arune/eclipse/c/AVR-App/main.hex";
                string filepath = argHexfile;
                hf = new HexFile();
                if (hf.loadHex(filepath)) {
                    Console.WriteLine("done!");
                    Console.WriteLine("File "+filepath+" loaded.");
                    Console.WriteLine("Size: " + hf.getLength().ToString() + " bytes, end adress " + hf.getAddrUpper().ToString() + "(0x" + hf.getAddrUpper().ToString("X") + ").");
                }
                else Console.WriteLine("Error loading " + filepath + "!");

            }
            else if (instr.Equals("go")) {
                Console.WriteLine("Connecting..");
                sc = new SerialConnection();
                try {
                    sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
                }
                catch (Exception e) { Console.WriteLine("Error: " + e.Message); return true; }
                if (!sc.open()) { Console.WriteLine("Error opening port."); return true; }
                dl = new Downloader(hf, sc, argReceiverID, false);
                if (!dl.go()) { Console.WriteLine("Error gooing..."); return true; }
                sc.close();
            }
            else if (instr.Equals("go bios")) {
                Console.WriteLine("Connecting..");
                sc = new SerialConnection();
                try {
                    sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
                }
                catch (Exception e) { Console.WriteLine("Error: " + e.Message); return true; }
                if (!sc.open()) { Console.WriteLine("Error opening port."); return true; }
                dl = new Downloader(hf, sc, argReceiverID, true);
                if (!dl.go()) { Console.WriteLine("Error gooing..."); return true; }
                sc.close();
            }
            else if (instr.Equals("start")) {
                Console.WriteLine("Starting..");
                sc = new SerialConnection();
                try {
                    sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
                }
                catch (Exception e) { Console.WriteLine("Error: " + e.Message); return true; }
                if (!sc.open()) { Console.WriteLine("Error opening port."); return true; }
                byte[] data = new byte[8];
                CanPacket cp = new CanPacket(CAN_NMT, CAN_NMT_START_APP, MY_ID, argReceiverID, 0, data);
                sc.writePacket(cp);
                sc.close();
            }
            else if (instr.Equals("reset")) {
                Console.WriteLine("Resetting..");
                sc = new SerialConnection();
                try {
                    sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
                }
                catch (Exception e) { Console.WriteLine("Error: " + e.Message); return true; }
                if (!sc.open()) { Console.WriteLine("Error opening port."); return true; }

                byte[] data = new byte[8];
                CanPacket cp = new CanPacket(CAN_NMT, CAN_NMT_RESET, MY_ID, argReceiverID, 0, data);
                sc.writePacket(cp);
                sc.close();
            }
            else if (instr.Equals("abort")) {
                if (dl != null) dl.abort();
            }
            else if (instr.Equals("exit")) {
                return false;
            }
            else {
                Console.WriteLine("Unknown command.");
            }

            return true;
        }
Exemple #3
0
        private void loadFile(string filepath)
        {
            // If file does not exist, show open file dialog.
            if (!File.Exists(filepath))
            {
                OpenFileDialog fd = new OpenFileDialog();
                fd.Multiselect = false;
                fd.Filter = "inhex32 files (*.hex)|*.hex";
                fd.Title = "Select HEX file to download";
                fd.CheckFileExists = true;
                if (fd.ShowDialog() == DialogResult.OK)
                {
                    FileStream file = new FileStream(fd.FileName, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(file);
                    filepath = fd.FileName;
                    sr.Close();
                }
                else return;
            }

            hf = new HexFile();
            if (dl != null) dl.abort(); dl = null;
            if (hf.loadHex(filepath))
            {
                currentLoadedFile = filepath;

                // Handle recent list.
                if (recentFiles.Contains(filepath))
                {
                    recentFiles.Remove(filepath);
                }
                recentFiles.AddFirst(filepath);
                refreshRecentFiles();
                saveSettings();

                log("");
                log("File " + filepath + " loaded.");
                log("Size: " + hf.getLength().ToString() + " bytes, end adress " + hf.getAddrUpper().ToString() + "(0x" + hf.getAddrUpper().ToString("X") + ").");
            }
            else { log("Error loading " + filepath + "!"); hf = null;  }
        }