private void btnOpenAndLoadScript_Click(object sender, EventArgs e)
        {
            string temp;
            P18_P24Script.ScriptData [] myScriptData;
            txtStatus.Clear();

            SetStatusString("Pinging Bus Pirate...\r\n");
            if(myPIC24Program.ExitICSPMode()==false)
            {
            SetStatusString("Error Pinging Bus Pirate!\r\n");
            MessageBox.Show("No Response from Bus Pirate!","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            return;
            }

            if(ofdScipt.ShowDialog()==DialogResult.OK)
            {
            temp=String.Format("Running Script {0} ...\r\n",ofdScipt.FileName);
            SetStatusString(temp);
            P18_P24Script myScript=new P18_P24Script(ofdScipt.FileName);

            try
                {
                myScriptData= myScript.GetScriptData();
                }
            catch(Exception ex)
                {
                SetStatusString("Error!\r\n");
                MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
                }

            for(int ctr=0;ctr<myScriptData.Length;ctr++)
                {
                Application.DoEvents();
                if(myScriptData[ctr].Command==P18_P24Script.Commands.ENTERICSPNORMAL)
                    {
                    myPIC24Program.EnterICSPMode(PIC24Program.SECURE_ID_NORMAL_ICSP);
                    SetStatusString("Entering ICSP Normal Mode!\r\n");
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.EXITICSP)
                    {
                    myPIC24Program.ExitICSPMode();
                    SetStatusString("Exit ICSP Mode!\r\n");
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.FORCEDSIX)
                    {
                    temp=String.Format("Sending ForcedSix Command with Data: 0x{0:x6}!\r\n",myScriptData[ctr].HexData);
                    SetStatusString(temp);
                    //myPIC24Program.SendSixSerialExec(true,myScriptData[ctr].HexData);
                    myPIC24Program.SendSixSerialExec(true,true,myScriptData[ctr].HexData);
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.SIX)
                    {
                    temp=String.Format("Sending Six Command with Data: 0x{0:x6}!\r\n",myScriptData[ctr].HexData);
                    SetStatusString(temp);
                    myPIC24Program.SendSixSerialExec(true,false,myScriptData[ctr].HexData);
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.SIX_AFTER_REGOUT)
                    {
                    temp=String.Format("Sending Six After Regout Command with Data: 0x{0:x6}!\r\n",myScriptData[ctr].HexData);
                    SetStatusString(temp);
                    myPIC24Program.SendSixSerialExecAfterRegout(myScriptData[ctr].HexData);
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.REGOUT)
                    {
                    uint ? MyReading=myPIC24Program.ReadRegOut();
                    temp=String.Format("Data Read: 0x{0:x6}!\r\n",MyReading);
                    SetStatusString(temp);
                    }

                // PIC18
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.PIC18ENTERICSP)
                    {
                    myPIC18Program.EnterICSPMode();
                    SetStatusString("Entering PIC18 ICSP Mode!\r\n");
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.SEND4BITCOMMANDLSBFIRST)
                    {
                    temp=String.Format("Sending 4-bit Command with Data: 0x{0:x2}!\r\n",myScriptData[ctr].HexData);
                    SetStatusString(temp);
                    myPIC18Program.Send4BitCommandLSBFirst((byte)myScriptData[ctr].HexData);
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.SEND16BITDATALSBFIRST)
                    {
                    temp=String.Format("Sending 16-bit data: 0x{0:x4}!\r\n",myScriptData[ctr].HexData);
                    SetStatusString(temp);
                    //myPIC18Program.Send16BitDataLSBFirst(myScriptData[ctr].HexData);
                    myPIC18Program.Send16BitLSBFirst(myScriptData[ctr].HexData);
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.READ8BITLSBFIRST)
                    {
                    uint ? MyReading=myPIC18Program.Read8BitLSBFirst();
                    temp=String.Format("8-bit Data Read: 0x{0:x2}!\r\n",MyReading);
                    SetStatusString(temp);
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.SEND8BITDATALSBFIRST)
                    {
                    temp=String.Format("Sending 8-bit data: 0x{0:x2}!\r\n",myScriptData[ctr].HexData);
                    SetStatusString(temp);
                    myPIC18Program.Send8BitLSBFirst(myScriptData[ctr].HexData);
                    }
                else if(myScriptData[ctr].Command==P18_P24Script.Commands.DELAYCLKON4THBIT)
                    {
                    temp=String.Format("Sending 4-bit clock with delay 5 msec\r\n");
                    SetStatusString(temp);
                    myPIC18Program.DelayOn4thClk(myScriptData[ctr].Delay);
                    myPIC18Program.Send8BitLSBFirst(myScriptData[ctr].HexData);
                    continue;
                    }
                Thread.Sleep(myScriptData[ctr].Delay);
                }
            SetStatusString("Done!");
            }
            else
            {
            SetStatusString("Aborted!\r\n");
            }
        }
        private void btnOpenAndLoadScript_Click(object sender, EventArgs e)
        {
            string temp;

            P18_P24Script.ScriptData [] myScriptData;
            txtStatus.Clear();

            SetStatusString("Pinging Bus Pirate...\r\n");
            if (myPIC24Program.ExitICSPMode() == false)
            {
                SetStatusString("Error Pinging Bus Pirate!\r\n");
                MessageBox.Show("No Response from Bus Pirate!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (ofdScipt.ShowDialog() == DialogResult.OK)
            {
                temp = String.Format("Running Script {0} ...\r\n", ofdScipt.FileName);
                SetStatusString(temp);
                P18_P24Script myScript = new P18_P24Script(ofdScipt.FileName);

                try
                {
                    myScriptData = myScript.GetScriptData();
                }
                catch (Exception ex)
                {
                    SetStatusString("Error!\r\n");
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                for (int ctr = 0; ctr < myScriptData.Length; ctr++)
                {
                    Application.DoEvents();
                    if (myScriptData[ctr].Command == P18_P24Script.Commands.ENTERICSPNORMAL)
                    {
                        myPIC24Program.EnterICSPMode(PIC24Program.SECURE_ID_NORMAL_ICSP);
                        SetStatusString("Entering ICSP Normal Mode!\r\n");
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.EXITICSP)
                    {
                        myPIC24Program.ExitICSPMode();
                        SetStatusString("Exit ICSP Mode!\r\n");
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.FORCEDSIX)
                    {
                        temp = String.Format("Sending ForcedSix Command with Data: 0x{0:x6}!\r\n", myScriptData[ctr].HexData);
                        SetStatusString(temp);
                        //myPIC24Program.SendSixSerialExec(true,myScriptData[ctr].HexData);
                        myPIC24Program.SendSixSerialExec(true, true, myScriptData[ctr].HexData);
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.SIX)
                    {
                        temp = String.Format("Sending Six Command with Data: 0x{0:x6}!\r\n", myScriptData[ctr].HexData);
                        SetStatusString(temp);
                        myPIC24Program.SendSixSerialExec(true, false, myScriptData[ctr].HexData);
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.SIX_AFTER_REGOUT)
                    {
                        temp = String.Format("Sending Six After Regout Command with Data: 0x{0:x6}!\r\n", myScriptData[ctr].HexData);
                        SetStatusString(temp);
                        myPIC24Program.SendSixSerialExecAfterRegout(myScriptData[ctr].HexData);
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.REGOUT)
                    {
                        uint?MyReading = myPIC24Program.ReadRegOut();
                        temp = String.Format("Data Read: 0x{0:x6}!\r\n", MyReading);
                        SetStatusString(temp);
                    }

                    // PIC18
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.PIC18ENTERICSP)
                    {
                        myPIC18Program.EnterICSPMode();
                        SetStatusString("Entering PIC18 ICSP Mode!\r\n");
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.SEND4BITCOMMANDLSBFIRST)
                    {
                        temp = String.Format("Sending 4-bit Command with Data: 0x{0:x2}!\r\n", myScriptData[ctr].HexData);
                        SetStatusString(temp);
                        myPIC18Program.Send4BitCommandLSBFirst((byte)myScriptData[ctr].HexData);
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.SEND16BITDATALSBFIRST)
                    {
                        temp = String.Format("Sending 16-bit data: 0x{0:x4}!\r\n", myScriptData[ctr].HexData);
                        SetStatusString(temp);
                        //myPIC18Program.Send16BitDataLSBFirst(myScriptData[ctr].HexData);
                        myPIC18Program.Send16BitLSBFirst(myScriptData[ctr].HexData);
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.READ8BITLSBFIRST)
                    {
                        uint?MyReading = myPIC18Program.Read8BitLSBFirst();
                        temp = String.Format("8-bit Data Read: 0x{0:x2}!\r\n", MyReading);
                        SetStatusString(temp);
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.SEND8BITDATALSBFIRST)
                    {
                        temp = String.Format("Sending 8-bit data: 0x{0:x2}!\r\n", myScriptData[ctr].HexData);
                        SetStatusString(temp);
                        myPIC18Program.Send8BitLSBFirst(myScriptData[ctr].HexData);
                    }
                    else if (myScriptData[ctr].Command == P18_P24Script.Commands.DELAYCLKON4THBIT)
                    {
                        temp = String.Format("Sending 4-bit clock with delay 5 msec\r\n");
                        SetStatusString(temp);
                        myPIC18Program.DelayOn4thClk(myScriptData[ctr].Delay);
                        myPIC18Program.Send8BitLSBFirst(myScriptData[ctr].HexData);
                        continue;
                    }
                    Thread.Sleep(myScriptData[ctr].Delay);
                }
                SetStatusString("Done!");
            }
            else
            {
                SetStatusString("Aborted!\r\n");
            }
        }