public void startAVT()
 {
     this.WriteSerialData(OBD.AVTRestart(), true);
     Thread.Sleep(200);
     this.WriteSerialData(OBD.AVTID(), true);
     this.searchforframe = "ID_AVT";
     this.consolemessage("Connecting to AVT, please wait...");
 }
 public void Disconnect_Click(object sender, RoutedEventArgs e)
 {
     this.breakresponse.Stop();
     this.autorespond.Stop();
     if (!this.UserProgStart.IsEnabled)
     {
         this.WriteSerialData(OBD.ResetPCM(), false);
         Thread.Sleep(100);
     }
     this.WriteSerialData(OBD.AVTRestart(), true);
     MainWindow._serialPort.Close();
     this.resetoptions();
     this.consolemessage("Serial port closed");
 }
 private void ResetPCM_Click(object sender, RoutedEventArgs e)
 {
     if (this.searchforframe != "PCM_READY")
     {
         this.consolemessage("PCM busy/not in flash mode");
         return;
     }
     if (MessageBox.Show("Exit flash mode and reset, are you sure?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) != MessageBoxResult.Yes)
     {
         this.consolemessage("Reset cancelled");
         return;
     }
     this.rxserialbuffer = null;
     this.WriteSerialData(OBD.ResetPCM(), false);
     Thread.Sleep(100);
     this.WriteSerialData(OBD.AVTNormalSpeed(), true);
     this.Serialout.Clear();
     this.searchforframe = "VERIFY_EXIT";
 }
 private void UserProgStart_Click(object sender, RoutedEventArgs e)
 {
     this.consolemessage("Entering flash mode, please wait...");
     this.WriteSerialData(OBD.RequestSecuritySeed(), false);
     this.searchforframe = "SECURITY_SEED";
 }
        private void OBDresponsehandler(string[] serialdata)
        {
            string str;

            byte[] numArray;
            byte[] numArray1;
            int    num   = 2048;
            int    num1  = 0;
            bool   flag  = false;
            bool   flag1 = false;
            bool   flag2 = false;
            bool   flag3 = false;

            MainWindow._serialPort.DataReceived -= new SerialDataReceivedEventHandler(this.SerialPortHasData);
            if (this.searchforframe == "ID_AVT" && serialdata != null && OBD.GetAVTID(serialdata, out str, out flag1))
            {
                this.WriteSerialData(OBD.AVTVPW(), true);
                this.consolemessage(str);
                if (!flag1)
                {
                    this.searchforframe = "1X_OK";
                }
            }
            if (this.searchforframe == "1X_OK" && serialdata != null && OBD.FindAVT1X(serialdata, out str))
            {
                this.consolemessage(str);
                this.searchforframe = "ECHO";
            }
            if (this.searchforframe == "SECURITY_SEED")
            {
                if (OBD.GetSecurityFrameCase15(serialdata, out numArray, out str, out flag2))
                {
                    this.consolemessage(str);
                    if (flag2)
                    {
                        this.searchforframe = "MODE34_OK";
                    }
                    else if (!flag2 && numArray != null)
                    {
                        this.WriteSerialData(numArray, false);
                        this.searchforframe = "SECURITY_OK";
                    }
                    else if (!flag2 && numArray == null)
                    {
                        this.searchforframe = "ECHO";
                    }
                }
                if (OBD.CheckForError(serialdata, out str, out flag3))
                {
                    this.consolemessage(str);
                    if (flag3)
                    {
                        MessageBox.Show("PCM internal hardware failure detected, reflash not possible", "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                        this.searchforframe = "ECHO";
                    }
                    else
                    {
                        this.searchforframe = "MODE34_OK";
                    }
                }
                if (serialdata == null)
                {
                    this.WriteSerialData(OBD.RequestSecuritySeed(), false);
                }
                this.rxserialbuffer = null;
            }
            if (this.searchforframe == "SECURITY_OK" && serialdata != null && OBD.GetSecurityResponse(serialdata, out flag, out str))
            {
                this.consolemessage(str);
                if (!flag)
                {
                    this.rxserialbuffer = null;
                    this.searchforframe = "ECHO";
                }
                else
                {
                    numArray1 = this.readflashkernal();
                    if (numArray1 == null)
                    {
                        this.searchforframe = "ECHO";
                    }
                    else
                    {
                        this.WriteSerialData(OBD.GetMode3436Frame(numArray1, "34"), false);
                        this.searchforframe = "MODE34_OK";
                    }
                }
            }
            if (this.searchforframe == "MODE34_OK" && serialdata != null)
            {
                if (OBD.GetMode3436Response(serialdata, out numArray, out str))
                {
                    this.consolemessage(str);
                    if (numArray == null)
                    {
                        this.searchforframe = "ECHO";
                    }
                    else
                    {
                        this.WriteSerialData(numArray, false);
                        this.searchforframe = "4X_OK";
                    }
                    this.rxserialbuffer = null;
                }
                if (OBD.CheckForError(serialdata, out str, out flag3))
                {
                    this.consolemessage(str);
                    if (flag3)
                    {
                        this.searchforframe = "ECHO";
                    }
                    else
                    {
                        numArray1 = this.readflashkernal();
                        this.WriteSerialData(OBD.GetMode3436Frame(numArray1, "34"), false);
                    }
                    this.rxserialbuffer = null;
                }
            }
            if (this.searchforframe == "4X_OK" && serialdata != null && OBD.GetModeA0Response(serialdata, out numArray, out str))
            {
                this.WriteSerialData(numArray, false);
                this.WriteSerialData(OBD.AVTHighSpeed(), true);
                Thread.Sleep(100);
                numArray1 = this.readflashkernal();
                this.WriteSerialData(OBD.GetMode3436Frame(numArray1, "36"), false);
                this.WriteSerialData(OBD.AVTFilter(), true);
                Thread.Sleep(150);
                this.rxserialbuffer = null;
                this.searchforframe = "CALL_PCM";
            }
            if (this.searchforframe == "CALL_PCM")
            {
                if (!OBD.CallPCM(serialdata, out numArray, out str))
                {
                    this.WriteSerialData(numArray, false);
                }
                else
                {
                    this.breakresponse.Stop();
                    this.UserProgStart.IsEnabled = false;
                    this.Disconnect.IsEnabled    = true;
                    this.consolemessage(str);
                    this.searchforframe = "PCM_READY";
                    this.WriteSerialData(OBD.LockFlash(), false);
                    this.rxserialbuffer = null;
                }
            }
            if (this.searchforframe == "DOWNLOAD")
            {
                num = 2048;
                if (OBD.ModeE1UpdateBIN(serialdata, this.DLBin, out this.DLBin, out flag))
                {
                    this.breakresponse.Stop();
                    if (this.DLBin != null)
                    {
                        this.FlashProgressBar.Value = (double)((int)this.DLBin.Length);
                        if ((int)this.DLBin.Length >= MainWindow.flashend)
                        {
                            this.consolemessage("BIN download complete");
                            this.saveflashbin();
                            this.resetdownload();
                            this.searchforframe = "CALL_PCM";
                        }
                    }
                }
                num      = (!flag ? 128 : 2048);
                numArray = OBD.RequestModeE1Data(this.DLBin, num, MainWindow.flashend);
                this.WriteSerialData(numArray, false);
                this.rxserialbuffer = null;
            }
            if (this.searchforframe == "FLASH_UPLOAD" && serialdata != null)
            {
                if (OBD.CheckForFlashError(serialdata, out str))
                {
                    this.consolemessage(str);
                    MessageBox.Show("Flash program failed", "Flash Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                    this.searchforframe = "CALL_PCM";
                }
                if (OBD.ModeE2UploadBIN(serialdata, this.ULBin, out numArray, out num1))
                {
                    this.breakresponse.Stop();
                    if (numArray != null && num1 != -1)
                    {
                        this.FlashProgressBar.Value = (double)num1;
                        this.WriteSerialData(numArray, false);
                    }
                }
                if (OBD.CheckForFlashComplete(serialdata, out str))
                {
                    this.WriteSerialData(OBD.LockFlash(), false);
                    this.consolemessage(str);
                    this.resetupload();
                    this.searchforframe = "CALL_PCM";
                }
                this.rxserialbuffer = null;
            }
            if (this.searchforframe == "FLASH_ERASE")
            {
                this.breakresponse.Stop();
                this.WriteSerialData(OBD.UnlockFlash(), false);
                Thread.Sleep(1500);
                bool?isChecked = this.FlashOS.IsChecked;
                if ((!isChecked.GetValueOrDefault() ? true : !isChecked.HasValue))
                {
                    this.FlashProgressBar.Minimum = (double)MainWindow.calstart;
                    this.FlashProgressBar.Maximum = (double)MainWindow.calend;
                    this.WriteSerialData(OBD.FlashEraseCal(), false);
                }
                else
                {
                    this.FlashProgressBar.Minimum = (double)MainWindow.calend;
                    this.FlashProgressBar.Maximum = (double)((int)this.ULBin.Length);
                    this.WriteSerialData(OBD.FlashEraseOS(), false);
                }
                this.searchforframe = "FLASH_UPLOAD";
                this.rxserialbuffer = null;
            }
            if (this.searchforframe == "VERIFY_EXIT" && serialdata != null)
            {
                if (OBD.CheckForError(serialdata, out str, out flag3))
                {
                    if (flag3)
                    {
                        MessageBox.Show("FATAL ERROR - PCM BOOT or RAM fault", "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                    }
                    else
                    {
                        MessageBox.Show("Reflash failed, enter Flash Mode and repeat flash cycle", "Error", MessageBoxButton.OK, MessageBoxImage.Hand);
                    }
                    this.consolemessage(str);
                    this.rxserialbuffer = null;
                    this.searchforframe = "ECHO";
                }
                else if (!OBD.CheckAVTExit(serialdata))
                {
                    this.consolemessage("PCM reset...");
                    this.searchforframe = "ECHO";
                }
                if (OBD.CheckAVTExit(serialdata))
                {
                    this.rxserialbuffer = null;
                }
                this.UserProgStart.IsEnabled = true;
            }
            if (this.searchforframe != "ECHO")
            {
                this.autorespond.Start();
            }
            MainWindow._serialPort.DataReceived += new SerialDataReceivedEventHandler(this.SerialPortHasData);
        }