Beispiel #1
0
 private void button4_Click(object sender, EventArgs e)
 {
     if (m_connectedToECU)
     {
         if (checkBox1.Checked)
         {
             KWPHandler.startLogging();
         }
         SaveFileDialog sfd = new SaveFileDialog();
         sfd.Filter = "Bin files|*.bin";
         if (sfd.ShowDialog() == DialogResult.OK)
         {
             // check reading status periodically
             if (sfd.FileName != string.Empty)
             {
                 if (Path.GetFileName(sfd.FileName) != string.Empty)
                 {
                     if (CheckFlashStatus())
                     {
                         // frmFlasherProgress.SetProgress("Starting flash download...");
                         AddToLog("Starting download of flash");
                         tmrReadProcessChecker.Enabled = true;
                         Console.WriteLine("Start reading flash...");
                         flash.readFlash(sfd.FileName);
                         //DisableCANInteractionButtons();
                     }
                 }
             }
         }
     }
 }
Beispiel #2
0
        public string GetE85AdaptionStatus()
        {
            string status;

            KWPHandler.getInstance().getE85AdaptionStatus(out status);
            return(status);
        }
Beispiel #3
0
        public byte[] ReadMapFromSRAM(Int64 sramaddress, Int32 length, out bool _success)
        {
            _success = false;
            byte[] data = new byte[length];
            try
            {
                KWPHandler.getInstance().requestSequrityAccess(false);

                if (canUsbDevice is LPCCANDevice) // or ELM327?
                {
                    Thread.Sleep(1);
                }
                if (KWPHandler.getInstance().sendReadRequest((uint)(sramaddress), (uint)length))
                {
                    Thread.Sleep(0);                  //<GS-11022010>
                    if (canUsbDevice is LPCCANDevice) // or ELM327?
                    {
                        Thread.Sleep(1);
                    }
                    if (KWPHandler.getInstance().sendRequestDataByOffset(out data))
                    {
                        _success = true;
                    }
                }
            }
            catch (Exception E)
            {
                logger.Debug("Failed to read memory: " + E.Message);
            }
            return(data);
        }
Beispiel #4
0
        public float GetE85Percentage()
        {
            float level;

            KWPHandler.getInstance().getE85Level(out level);
            return(level);
        }
Beispiel #5
0
 public static void setKWPHandler(KWPHandler a_kwpHandler)
 {
     if (m_kwpHandler != null)
     {
         throw new Exception("KWPHandler already set");
     }
     m_kwpHandler = a_kwpHandler;
 }
Beispiel #6
0
        public string[] ReadDTC()
        {
            List <string> list;

            KWPHandler.getInstance().requestSequrityAccess(false);
            KWPHandler.getInstance().ReadDTCCodes(out list);
            return(list.ToArray());
        }
Beispiel #7
0
        private void setFlasher()
        {
            KWPHandler.setKWPDevice(kwpDevice);
            kwpHandler = KWPHandler.getInstance();

            T7Flasher.setKWPHandler(kwpHandler);
            flash = new T7Flasher();
            flash.onStatusChanged += flash_onStatusChanged;
        }
Beispiel #8
0
        public byte[] ReadMapfromSRAM(SymbolHelper sh, bool showProgress)
        {
            byte[] completedata = new byte[sh.Length];
            try
            {
                byte[] data;
                int    m_nrBytes     = 64;
                int    m_nrOfReads   = 0;
                int    m_nrOfRetries = 0;
                m_nrOfReads = sh.Length / m_nrBytes;
                if (((sh.Length) % 64) > 0)
                {
                    m_nrOfReads++;
                }
                int bytecount = 0;
                KWPHandler.getInstance().requestSequrityAccess(false);
                for (int readcount = 0; readcount < m_nrOfReads; readcount++)
                {
                    if (showProgress)
                    {
                        CastProgressReadEvent((int)(readcount * 100 / m_nrOfReads));
                    }

                    m_nrOfRetries = 0;
                    int addresstoread = (int)sh.Start_address + (readcount * m_nrBytes);
                    logger.Debug("Reading 64 bytes from address: " + addresstoread.ToString("X6"));

                    while (!KWPHandler.getInstance().sendReadRequest((uint)(addresstoread), 64) && m_nrOfRetries < 20)
                    {
                        m_nrOfRetries++;
                    }
                    logger.Debug("Send command in " + m_nrOfRetries.ToString() + " retries");
                    m_nrOfRetries = 0;
                    Thread.Sleep(1);
                    while (!KWPHandler.getInstance().sendRequestDataByOffset(out data) && m_nrOfRetries < 20)
                    {
                        m_nrOfRetries++;
                    }
                    logger.Debug("Read data in " + m_nrOfRetries.ToString() + " retries");
                    logger.Debug("Read " + data.Length.ToString() + " bytes from CAN interface");
                    foreach (byte b in data)
                    {
                        // Console.Write(b.ToString("X2") + " ");
                        if (bytecount < completedata.Length)
                        {
                            completedata[bytecount++] = b;
                        }
                    }
                }
                //logger.Debug("Reading done");
            }
            catch (Exception E)
            {
                logger.Debug("Failed to read memory: " + E.Message);
            }
            return(completedata);
        }
Beispiel #9
0
        public bool GetSRAMSnapshot(string a_fileName)
        {
            const int blockSize = 0x80;

            byte[] data = new byte[blockSize];
            try
            {
                KWPHandler.getInstance().requestSequrityAccess(false);

                FileStream fs = new FileStream(a_fileName, FileMode.Create);
                using (BinaryWriter br = new BinaryWriter(fs))
                {
                    for (int i = 0; i < 0x10000 / blockSize; i++)
                    {
                        long curaddress = (0xF00000 + i * blockSize);
                        if (canUsbDevice is LPCCANDevice)
                        {
                            Thread.Sleep(1);
                        }
                        if (KWPHandler.getInstance().sendReadRequest((uint)(curaddress), (uint)blockSize))
                        {
                            Thread.Sleep(0);
                            if (canUsbDevice is LPCCANDevice)
                            {
                                Thread.Sleep(1);
                            }
                            if (!KWPHandler.getInstance().sendRequestDataByOffset(out data))
                            {
                                logger.Debug("Failed to read data. sendRequestDataByOffset: " + curaddress.ToString("X8"));
                                CastInfoEvent("Failed to read data. sendRequestDataByOffset: " + curaddress.ToString("X8"), ActivityType.FinishedDownloadingFlash);
                                return(false);
                            }
                        }
                        else
                        {
                            logger.Debug("Failed to read data. sendReadRequest: " + curaddress.ToString("X8"));
                            CastInfoEvent("Failed to read data. sendReadRequest: " + curaddress.ToString("X8"), ActivityType.FinishedDownloadingFlash);
                            return(false);
                        }
                        CastProgressReadEvent((i * 100) / (0x10000 / blockSize));
                        br.Write(data);
                    }
                }
                fs.Close();
                CastProgressReadEvent(100);
                CastInfoEvent("Snapshot downloaded", ActivityType.FinishedDownloadingFlash);
                return(true);
            }
            catch (Exception E)
            {
                logger.Debug("Failed to read memory: " + E.Message);
            }
            return(false);
        }
Beispiel #10
0
        public byte[] ReadMapFromSRAMVarLength(SymbolHelper sh)
        {
            int varlength = 2;

            byte[] completedata = new byte[sh.Length];
            try
            {
                byte[] data;
                int    m_nrBytes     = varlength;
                int    m_nrOfReads   = 0;
                int    m_nrOfRetries = 0;
                m_nrOfReads = sh.Length / m_nrBytes;
                if (((sh.Length) % varlength) > 0)
                {
                    m_nrOfReads++;
                }
                int bytecount = 0;
                KWPHandler.getInstance().requestSequrityAccess(false); // no seq. access <GS-10022010>
                for (int readcount = 0; readcount < m_nrOfReads; readcount++)
                {
                    m_nrOfRetries = 0;
                    int addresstoread = (int)sh.Start_address + (readcount * m_nrBytes);
                    //LogHelper.Log("Reading 64 bytes from address: " + addresstoread.ToString("X6"));

                    while (!KWPHandler.getInstance().sendReadRequest(/*0xF04768*/ (uint)(addresstoread), (uint)varlength) && m_nrOfRetries < 20)
                    {
                        m_nrOfRetries++;
                    }
                    logger.Debug("Send command in " + m_nrOfRetries.ToString() + " retries");
                    m_nrOfRetries = 0;
                    while (!KWPHandler.getInstance().sendRequestDataByOffset(out data) && m_nrOfRetries < 20)
                    {
                        m_nrOfRetries++;
                    }
                    logger.Debug("Read data in " + m_nrOfRetries.ToString() + " retries");
                    logger.Debug("Read " + data.Length.ToString() + " bytes from CAN interface");
                    foreach (byte b in data)
                    {
                        //Console.Write(b.ToString("X2") + " ");
                        if (bytecount < completedata.Length)
                        {
                            completedata[bytecount++] = b;
                        }
                    }
                }
            }
            catch (Exception E)
            {
                logger.Debug("Failed to read memory: " + E.Message);
            }
            return(completedata);
        }
Beispiel #11
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (m_connectedToECU)
     {
         if (checkBox1.Checked)
         {
             KWPHandler.startLogging();
         }
         if (!tmrReadProcessChecker.Enabled)
         {
             OpenFileDialog ofd = new OpenFileDialog();
             ofd.Filter      = "Bin files|*.bin";
             ofd.Title       = "Select binary file to flash...";
             ofd.Multiselect = false;
             if (ofd.ShowDialog() == DialogResult.OK)
             {
                 FileInfo fi = new FileInfo(ofd.FileName);
                 if (fi.Length == 512 * 1024)
                 {
                     // check reading status periodically
                     AddToCanTrace("Starting flash procedure, checking flashing process status");
                     if (CheckFlashStatus())
                     {
                         //frmFlasherProgress = new frmProgress();
                         //frmFlasherProgress.onCancelOperation += new frmProgress.CancelEvent(frmFlasherProgress_onCancelOperation);
                         //frmFlasherProgress.Show();
                         //frmFlasherProgress.SetProgress("Starting ECU flash...");
                         tmrWriteProcessChecker.Enabled = true;
                         AddToLog("Flashing: " + ofd.FileName);
                         AddToCanTrace("Calling flash.writeFlash with filename: " + ofd.FileName);
                         flash.writeFlash(ofd.FileName);
                         //DisableCANInteractionButtons();
                     }
                 }
                 else
                 {
                     MessageBox.Show("File has incorrect length, should be 512 kB exactly!");
                 }
             }
         }
     }
     else
     {
         AddToLog("Connection to ECU not established, please restart the application");
     }
 }
Beispiel #12
0
 public byte[] ReadSymbolNumber(uint symbolnumber, out bool _success)
 {
     _success = false;
     byte[] data = new byte[0];
     try
     {
         if (KWPHandler.getInstance().setSymbolRequest((uint)symbolnumber))
         {
             Thread.Sleep(0);//<GS-11022010>
             if (KWPHandler.getInstance().sendRequestDataByOffset(out data))
             {
                 _success = true;
             }
         }
     }
     catch (Exception E)
     {
         logger.Debug("Failed to read SymbolNumber: " + E.Message);
     }
     return(data);
 }
Beispiel #13
0
        public void WriteMapToSRAM(string symbolname, byte[] completedata, bool showProgress, uint sramAddress, int symbolindex)
        {
            logger.Debug("Writing " + symbolindex.ToString() + " " + symbolname + " SRAM: " + sramAddress.ToString("X8"));
            // if data length > 64 then split the messages
            const uint m_nrBytes    = 64;
            uint       m_nrOfWrites = 0;

            m_nrOfWrites = (uint)completedata.Length / m_nrBytes;
            if (((completedata.Length) % 64) > 0)
            {
                m_nrOfWrites++;
            }
            uint bytecount = 0;


            KWPHandler.getInstance().requestSequrityAccess(false);

            for (uint readcount = 0; readcount < m_nrOfWrites; readcount++)
            {
                if (showProgress)
                {
                    CastProgressWriteEvent((int)(readcount * 100 / m_nrOfWrites));
                }

                uint   addresstowrite = sramAddress + (readcount * m_nrBytes);
                byte[] dataToSend     = new byte[64];
                if (readcount == m_nrOfWrites - 1) // only the last part
                {
                    dataToSend = new byte[completedata.Length - bytecount];
                }
                for (int t = 0; t < dataToSend.Length; t++)
                {
                    dataToSend[t] = completedata[bytecount++];
                }
                if (!KWPHandler.getInstance().writeSymbolRequestAddress(addresstowrite, dataToSend))
                {
                    logger.Debug("Failed to write data to the ECU");
                }
            }
        }
Beispiel #14
0
 public static void setKWPHandler(KWPHandler a_kwpHandler)
 {
     m_kwpHandler = a_kwpHandler;
 }
Beispiel #15
0
        /// <summary>
        /// The run method handles writing and reading. It waits for a command to start read
        /// or write and handles this command until it's completed, stopped or until there is
        /// a failure.
        /// </summary>
        void run()
        {
            while (true)
            {
                logger.Debug("Running T7Flasher");
                m_nrOfRetries   = 0;
                m_nrOfBytesRead = 0;
                m_resetEvent.WaitOne(-1, true);
                gotSequrityAccess = false;
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        return;
                    }
                }
                NotifyStatusChanged(this, new StatusEventArgs("Starting session..."));

                m_kwpHandler.startSession();
                logger.Debug("Session started");
                NotifyStatusChanged(this, new StatusEventArgs("Session started, requesting security access to ECU"));
                if (!gotSequrityAccess)
                {
                    logger.Debug("No security access");

                    for (int nrOfSequrityTries = 0; nrOfSequrityTries < 5; nrOfSequrityTries++)
                    {
                        if (!KWPHandler.getInstance().requestSequrityAccess(true))
                        {
                            logger.Debug("No security access granted");
                        }
                        else
                        {
                            gotSequrityAccess = true;
                            logger.Debug("Security access granted");

                            break;
                        }
                    }
                }
                if (!gotSequrityAccess)
                {
                    SetFlashStatus(FlashStatus.NoSequrityAccess);
                    logger.Debug("No security access granted after 5 retries");
                    NotifyStatusChanged(this, new StatusEventArgs("Failed to get security access after 5 retries"));
                }
                //Here it would make sense to stop if we didn't ge security access but
                //let's try anyway. It could be that we don't get a possitive reply from the
                //ECU if we alredy have security access (from a previous, interrupted, session).
                if (m_command == FlashCommand.ReadCommand)
                {
                    ReadCommand();
                }
                else if (m_command == FlashCommand.ReadMemoryCommand)
                {
                    ReadMemoryCommand();
                }
                else if (m_command == FlashCommand.ReadSymbolMapCommand)
                {
                    ReadSymbolMapCommand();
                }
                else if (m_command == FlashCommand.WriteCommand)
                {
                    WriteCommand();
                }

                if (m_endThread)
                {
                    return;
                }
                NotifyStatusChanged(this, new StatusEventArgs("Flasing procedure completed"));
                logger.Debug("T7Flasher completed");
                SetFlashStatus(FlashStatus.Completed);
            }
        }
Beispiel #16
0
 public bool WriteMapToSRAM(uint addresstowrite, byte[] dataToSend)
 {
     KWPHandler.getInstance().requestSequrityAccess(false);
     return(KWPHandler.getInstance().writeSymbolRequestAddress(addresstowrite, dataToSend));
 }
Beispiel #17
0
 public bool WriteSymbolToSRAM(uint symbolnumber, byte[] bytes)
 {
     KWPHandler.getInstance().requestSequrityAccess(false);
     return(KWPHandler.getInstance().writeSymbolRequest(symbolnumber, bytes));
 }
Beispiel #18
0
        private bool CheckCANConnectivity()
        {
            // write log information to "CanTrace.txt"
            if (flash == null)
            {
                AddToCanTrace("Initializing CANbus interface");
                System.Windows.Forms.Application.DoEvents();
                AddToLog("Initializing CAN interface, please stand by...");
                AddToCanTrace("Creating new connection to CANUSB device!");

                if (useCombiAdapter)
                {
                    // connect to adapter
                    LPCCANDevice_T7 lpc = (LPCCANDevice_T7)this.canUsbDevice;
                    if (!lpc.connect())
                    {
                        return(false);
                    }
                    //<GS-09122010>
                    // kwpCanDevice.setCANDevice(canUsbDevice);
                    // KWPHandler.setKWPDevice(kwpCanDevice);
                    //<GS-09122010>
                    // get flasher object
                    Console.WriteLine("Created combiadpater flasher object");
                    this.flash         = lpc.createFlasher();
                    flash.EnableCanLog = checkBox1.Checked;

                    AddToCanTrace("T7CombiFlasher object created");
                    AddToLog("CombiAdapter ready");
                }
                else
                {
                    //flash = new T7.Flasher.T7Flasher();
                    //AddToCanTrace("Object T7.Flasher.T7Flasher created");
                    kwpCanDevice.setCANDevice(canUsbDevice);
                    kwpCanDevice.EnableCanLog = checkBox1.Checked;
                    KWPHandler.setKWPDevice(kwpCanDevice);
                    if (checkBox1.Checked)
                    {
                        KWPHandler.startLogging();
                    }
                    kwpHandler = KWPHandler.getInstance();
                    try
                    {
                        T7.Flasher.T7Flasher.setKWPHandler(kwpHandler);
                    }
                    catch (Exception E)
                    {
                        Console.WriteLine(E.Message);
                        AddToCanTrace("Failed to set flasher object to KWPHandler");
                    }
                    flash = T7.Flasher.T7Flasher.getInstance();
                    flash.onStatusChanged += new T7.Flasher.IFlasher.StatusChanged(flash_onStatusChanged);
                    //AddToLog("Set callback event on flasher");
                    flash.EnableCanLog = checkBox1.Checked;

                    if (kwpHandler.openDevice())
                    {
                        AddToLog("Canbus channel opened");
                    }
                    else
                    {
                        AddToLog("Unable to open canbus channel");
                        kwpHandler.closeDevice();
                        return(false);
                    }
                    if (kwpHandler.startSession())
                    {
                        AddToLog("Session started");
                    }
                    else
                    {
                        AddToLog("Unable to start session");
                        kwpHandler.closeDevice();
                        return(false);
                    }
                }
            }
            m_connectedToECU = true;
            return(m_connectedToECU);
        }
Beispiel #19
0
 public void ResumeAlivePolling()
 {
     KWPHandler.getInstance().ResumeAlivePolling();
 }
Beispiel #20
0
        private void button3_Click(object sender, EventArgs e)
        {
            string vin;
            string immo;
            string engineType;
            string swVersion;

            if (m_connectedToECU)
            {
                if (useCombiAdapter)
                {
                    // begin comms session
                    T7.Flasher.T7CombiFlasher cf =
                        (T7.Flasher.T7CombiFlasher) this.flash;
                    cf.beginSession();

                    // NB: does not work, but it's a firmware problem
                    //vin = cf.getHeaderString(T7.Flasher.T7HeaderField.vin);
                    //engineType = cf.getHeaderString(T7.Flasher.T7HeaderField.engtype);
                    //swVersion = cf.getHeaderString(T7.Flasher.T7HeaderField.swversion);

                    // end session
                    cf.endSession();
                }
                else
                {
                    if (checkBox1.Checked)
                    {
                        KWPHandler.startLogging();
                    }
                    KWPResult res = kwpHandler.getVIN(out vin);
                    if (res == KWPResult.OK)
                    {
                        AddToLog("VIN: " + vin);
                    }
                    else if (res == KWPResult.DeviceNotConnected)
                    {
                        AddToLog("VIN: not connected");
                    }
                    else
                    {
                        AddToLog("VIN: timeout");
                    }
                    res = kwpHandler.getImmo(out immo);
                    if (res == KWPResult.OK)
                    {
                        AddToLog("Immo: " + immo);
                    }
                    res = kwpHandler.getEngineType(out engineType);
                    if (res == KWPResult.OK)
                    {
                        AddToLog("Engine type: :" + engineType);
                    }
                    res = kwpHandler.getSwVersion(out swVersion);
                    if (res == KWPResult.OK)
                    {
                        AddToLog("Software version: " + swVersion);
                    }
                }
            }
        }
Beispiel #21
0
 public void SuspendAlivePolling()
 {
     KWPHandler.getInstance().SuspendAlivePolling();
 }
Beispiel #22
0
 public bool ClearDTCCodes()
 {
     KWPHandler.getInstance().requestSequrityAccess(false);
     return(KWPHandler.getInstance().ClearDTCCodes());
 }
Beispiel #23
0
        /// <summary>
        /// The run method handles writing and reading. It waits for a command to start read
        /// or write and handles this command until it's completed, stopped or until there is
        /// a failure.
        /// </summary>
        void run()
        {
            while (true)
            {
                //AddToCanTrace("Running T7Flasher", gotSequrityAccess);
                m_nrOfRetries   = 0;
                m_nrOfBytesRead = 0;
                m_resetEvent.WaitOne(-1, true);
                gotSequrityAccess = false;
                lock (m_synchObject)
                {
                    if (m_endThread)
                    {
                        //AddToCanTrace("Thread ended", gotSequrityAccess);
                        return;
                    }
                }
                if (onStatusChanged != null)
                {
                    onStatusChanged(this, new StatusEventArgs("Starting session..."));
                }

                m_kwpHandler.startSession();
                AddToCanTrace("Session started", gotSequrityAccess);
                if (onStatusChanged != null)
                {
                    onStatusChanged(this, new StatusEventArgs("Session started, requesting security access to ECU"));
                }
                if (!gotSequrityAccess)
                {
                    AddToCanTrace("No security access", gotSequrityAccess);

                    for (int nrOfSequrityTries = 0; nrOfSequrityTries < 5; nrOfSequrityTries++)
                    {
                        if (/*!m_kwpHandler.requestSequrityAccess()*/ !KWPHandler.getInstance().requestSequrityAccess(true))
                        {
                            AddToCanTrace("No security access granted", gotSequrityAccess);
                        }
                        else
                        {
                            gotSequrityAccess = true;
                            AddToCanTrace("Security access granted", gotSequrityAccess);

                            break;
                        }
                    }
                }
                if (!gotSequrityAccess)
                {
                    SetFlashStatus(FlashStatus.NoSequrityAccess);
                    AddToCanTrace("No security access granted after 5 retries", gotSequrityAccess);
                    if (onStatusChanged != null)
                    {
                        onStatusChanged(this, new StatusEventArgs("Failed to get security access after 5 retries"));
                    }
                }
                //Here it would make sense to stop if we didn't ge security acces but
                //let's try anyway. It could be that we don't get a possitive reply from the
                //ECU if we alredy have security access (from a previous, interrupted, session).
                if (m_command == FlashCommand.ReadCommand)
                {
                    int    nrOfBytes = 64;//512;
                    byte[] data;
                    AddToCanTrace("Reading flash content to file: " + m_fileName, gotSequrityAccess);
                    if (onStatusChanged != null)
                    {
                        onStatusChanged(this, new StatusEventArgs("Reading data from ECU..."));
                    }


                    if (File.Exists(m_fileName))
                    {
                        File.Delete(m_fileName);
                    }
                    FileStream fileStream = File.Create(m_fileName, 1024);
                    AddToCanTrace("File created", gotSequrityAccess);
                    SetFlashStatus(FlashStatus.Reading);
                    AddToCanTrace("Flash status is reading", gotSequrityAccess);

                    for (int i = 0; i < 512 * 1024 / nrOfBytes; i++)
                    {
                        lock (m_synchObject)
                        {
                            if (m_command == FlashCommand.StopCommand)
                            {
                                continue;
                            }
                            if (m_endThread)
                            {
                                return;
                            }
                        }

                        while (!m_kwpHandler.sendReadRequest((uint)(nrOfBytes * i), (uint)nrOfBytes))
                        {
                            m_nrOfRetries++;
                        }

                        while (!m_kwpHandler.sendRequestDataByOffset(out data))
                        {
                            m_nrOfRetries++;
                        }
                        fileStream.Write(data, 0, nrOfBytes);
                        m_nrOfBytesRead += nrOfBytes;
                    }
                    fileStream.Close();
                    AddToCanTrace("Closed file", gotSequrityAccess);

                    m_kwpHandler.sendDataTransferExitRequest();
                    AddToCanTrace("Done reading", gotSequrityAccess);
                }
                else if (m_command == FlashCommand.ReadMemoryCommand)
                {
                    int    nrOfBytes = /*512*/ 64;
                    byte[] data;
                    //Console.WriteLine("Reading: " + m_length.ToString() + " bytes");
                    if (onStatusChanged != null)
                    {
                        onStatusChanged(this, new StatusEventArgs("Reading data from ECU..."));
                    }

                    if (File.Exists(m_fileName))
                    {
                        File.Delete(m_fileName);
                    }
                    FileStream fileStream = File.Create(m_fileName, 1024);
                    int        nrOfReads  = (int)m_length / nrOfBytes;
                    for (int i = 0; i < nrOfReads; i++)
                    {
                        lock (m_synchObject)
                        {
                            if (m_command == FlashCommand.StopCommand)
                            {
                                continue;
                            }
                            if (m_endThread)
                            {
                                return;
                            }
                        }
                        SetFlashStatus(FlashStatus.Reading);

                        if (i == nrOfReads - 1)
                        {
                            nrOfBytes = (int)m_length - nrOfBytes * i;
                        }
                        while (!m_kwpHandler.sendReadRequest((uint)m_offset + (uint)(nrOfBytes * i), (uint)nrOfBytes))
                        {
                            m_nrOfRetries++;
                        }

                        while (!m_kwpHandler.sendRequestDataByOffset(out data))
                        {
                            m_nrOfRetries++;
                        }
                        Console.WriteLine("Writing data to file: " + m_length.ToString() + " bytes");
                        fileStream.Write(data, 0, nrOfBytes);
                        m_nrOfBytesRead += nrOfBytes;
                    }
                    fileStream.Close();
                    Console.WriteLine("Done reading");
                    m_kwpHandler.sendDataTransferExitRequest();
                }
                else if (m_command == FlashCommand.ReadSymbolMapCommand)
                {
                    byte[] data;
                    string swVersion = "";
                    m_nrOfBytesRead = 0;
                    if (onStatusChanged != null)
                    {
                        onStatusChanged(this, new StatusEventArgs("Reading symbol map from ECU..."));
                    }

                    if (File.Exists(m_fileName))
                    {
                        File.Delete(m_fileName);
                    }
                    FileStream fileStream = File.Create(m_fileName, 1024);
                    if (m_kwpHandler.sendUnknownRequest() != KWPResult.OK)
                    {
                        if (onStatusChanged != null)
                        {
                            onStatusChanged(this, new StatusEventArgs("Failed to read data from ECU..."));
                        }
                        SetFlashStatus(FlashStatus.ReadError);
                        continue;
                    }
                    SetFlashStatus(FlashStatus.Reading);
                    m_kwpHandler.getSwVersionFromDR51(out swVersion);

                    if (m_kwpHandler.sendReadSymbolMapRequest() != KWPResult.OK)
                    {
                        if (onStatusChanged != null)
                        {
                            onStatusChanged(this, new StatusEventArgs("Failed to read data from ECU..."));
                        }

                        SetFlashStatus(FlashStatus.ReadError);
                        continue;
                    }
                    m_kwpHandler.sendDataTransferRequest(out data);
                    while (data.Length > 0x10)
                    {
                        fileStream.Write(data, 1, data.Length - 3);
                        m_nrOfBytesRead += data.Length - 3;
                        lock (m_synchObject)
                        {
                            if (m_command == FlashCommand.StopCommand)
                            {
                                continue;
                            }
                            if (m_endThread)
                            {
                                return;
                            }
                        }
                        m_kwpHandler.sendDataTransferRequest(out data);
                    }
                    fileStream.Flush();
                    fileStream.Close();
                }
                else if (m_command == FlashCommand.WriteCommand)
                {
                    AddToCanTrace("Write command seen", gotSequrityAccess);
                    int    nrOfBytes = 128;
                    int    i         = 0;
                    byte[] data      = new byte[nrOfBytes];
                    if (!gotSequrityAccess)
                    {
                        SetFlashStatus(FlashStatus.Completed);
                        continue;
                    }
                    if (!File.Exists(m_fileName))
                    {
                        SetFlashStatus(FlashStatus.NoSuchFile);
                        AddToCanTrace("No such file found: " + m_fileName, gotSequrityAccess);
                        if (onStatusChanged != null)
                        {
                            onStatusChanged(this, new StatusEventArgs("Failed to find file to flash..."));
                        }

                        continue;
                    }
                    AddToCanTrace("Start erasing", gotSequrityAccess);
                    if (onStatusChanged != null)
                    {
                        onStatusChanged(this, new StatusEventArgs("Erasing flash..."));
                    }

                    SetFlashStatus(FlashStatus.Eraseing);
                    if (m_kwpHandler.sendEraseRequest() != KWPResult.OK)
                    {
                        if (onStatusChanged != null)
                        {
                            onStatusChanged(this, new StatusEventArgs("Failed to erase flash..."));
                        }
                        SetFlashStatus(FlashStatus.EraseError);
                        AddToCanTrace("Erase error occured", gotSequrityAccess);
                        // break;
                    }
                    AddToCanTrace("Opening file for reading", gotSequrityAccess);

                    FileStream fs = new FileStream(m_fileName, FileMode.Open, FileAccess.Read);

                    SetFlashStatus(FlashStatus.Writing);
                    AddToCanTrace("Set flash status to writing", gotSequrityAccess);
                    if (onStatusChanged != null)
                    {
                        onStatusChanged(this, new StatusEventArgs("Writing flash... 0x00000-0x7B000"));
                    }

                    //Write 0x0-0x7B000
                    AddToCanTrace("0x0-0x7B000", gotSequrityAccess);
                    Thread.Sleep(100);
                    if (m_kwpHandler.sendWriteRequest(0x0, 0x7B000) != KWPResult.OK)
                    {
                        if (onStatusChanged != null)
                        {
                            onStatusChanged(this, new StatusEventArgs("Failed to write data to flash..."));
                        }

                        SetFlashStatus(FlashStatus.WriteError);
                        AddToCanTrace("Write error occured", gotSequrityAccess);

                        continue;
                    }
                    for (i = 0; i < 0x7B000 / nrOfBytes; i++)
                    {
                        fs.Read(data, 0, nrOfBytes);
                        m_nrOfBytesRead = i * nrOfBytes;
                        AddToCanTrace("sendWriteDataRequest " + m_nrOfBytesRead.ToString(), gotSequrityAccess);
                        if (m_kwpHandler.sendWriteDataRequest(data) != KWPResult.OK)
                        {
                            if (onStatusChanged != null)
                            {
                                onStatusChanged(this, new StatusEventArgs("Failed to write data to flash..."));
                            }
                            SetFlashStatus(FlashStatus.WriteError);
                            AddToCanTrace("Write error occured " + m_nrOfBytesRead.ToString(), gotSequrityAccess);

                            continue;
                        }
                        lock (m_synchObject)
                        {
                            if (m_command == FlashCommand.StopCommand)
                            {
                                AddToCanTrace("Stop command seen", gotSequrityAccess);
                                continue;
                            }
                            if (m_endThread)
                            {
                                AddToCanTrace("Thread ended", gotSequrityAccess);
                                return;
                            }
                        }
                    }

                    //Write 0x7FE00-0x7FFFF
                    AddToCanTrace("Write 0x7FE00-0x7FFFF", gotSequrityAccess);
                    if (onStatusChanged != null)
                    {
                        onStatusChanged(this, new StatusEventArgs("Writing flash... 0x7FE00-0x7FFFF"));
                    }

                    if (m_kwpHandler.sendWriteRequest(0x7FE00, 0x200) != KWPResult.OK)
                    {
                        if (onStatusChanged != null)
                        {
                            onStatusChanged(this, new StatusEventArgs("Failed to write data to flash..."));
                        }
                        SetFlashStatus(FlashStatus.WriteError);
                        AddToCanTrace("Write error occured", gotSequrityAccess);
                        continue;
                    }
                    fs.Seek(0x7FE00, System.IO.SeekOrigin.Begin);
                    for (i = 0x7FE00 / nrOfBytes; i < 0x80000 / nrOfBytes; i++)
                    {
                        fs.Read(data, 0, nrOfBytes);
                        m_nrOfBytesRead = i * nrOfBytes;
                        AddToCanTrace("sendWriteDataRequest " + m_nrOfBytesRead.ToString(), gotSequrityAccess);

                        if (m_kwpHandler.sendWriteDataRequest(data) != KWPResult.OK)
                        {
                            if (onStatusChanged != null)
                            {
                                onStatusChanged(this, new StatusEventArgs("Failed to write data to flash..."));
                            }
                            SetFlashStatus(FlashStatus.WriteError);
                            AddToCanTrace("Write error occured " + m_nrOfBytesRead.ToString(), gotSequrityAccess);
                            continue;
                        }
                        lock (m_synchObject)
                        {
                            if (m_command == FlashCommand.StopCommand)
                            {
                                AddToCanTrace("Stop command seen", gotSequrityAccess);
                                continue;
                            }
                            if (m_endThread)
                            {
                                AddToCanTrace("Thread ended", gotSequrityAccess);
                                return;
                            }
                        }
                    }
                }
                if (onStatusChanged != null)
                {
                    onStatusChanged(this, new StatusEventArgs("Flasing procedure completed"));
                }
                AddToCanTrace("T7Flasher completed", gotSequrityAccess);
                SetFlashStatus(FlashStatus.Completed);
            }
        }
Beispiel #24
0
 public bool ForceE85Adaption()
 {
     return(KWPHandler.getInstance().forceE85Adaption() == KWPResult.OK);
 }
Beispiel #25
0
 public bool SetE85Percentage(int level)
 {
     return(KWPHandler.getInstance().setE85Level(level) == KWPResult.OK);
 }