public RAMLoader(Parameters pars, FTDIdevice USB, string binfilepath, MainForm mf)
 {
     parameters = pars;
     thisUSB = USB;
     filepathtoload = binfilepath;
     mf_parent = mf;
 }
 public void checkFreqValidity(double fine, ref double freq, int coarse, FTDIdevice thisUSB, Device device, SSLInterface ssl)
 {
     //makes sure freq isn't out of range or a duplicate measurement, using the following general procedure: if it's invalid, measure it again; if it's still invalid, throw error.
     if (freq > 12001400 || freq < 11998000) // if out of range of trimming
     {
         //first, try again
         freq = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine));
         //if freq is wildly off, probably unseated
         if (freq > 12010000 || freq < 11990000) throw new Exception_Yellow("Frequency counter range error (coarse " + coarse.ToString() + " fine " + fine.ToString() + " " + freq.ToString() + "). Please ensure that board is properly seated and try again.  If error persists, testing fails.");
         if (freq > 12001400 || freq < 11998000) throw new Exception_Red("Crystal frequency is outside acceptable range (coarse " + FinalCoarse.ToString() + " fine " + FinalFine.ToString() + "). Testing fails. Insert a new board and press \"Run Test\" or Enter.");  // freqency is too far from 12 MHz to be trimmed
     }    //        (make sure that this catches a null freq)
     //if this frequency is more than twice as close as we'd expect to the previous measurement, probably unseated:
     if (previouscoarseforchecking != -12345) //if this is not the first time we're running a check
     {
         if (coarse == previouscoarseforchecking && Convert.ToInt32(fine) != Convert.ToInt32(previousfineforchecking))  //if fine trims can be compared on their own
         {
             if ((freq - previousfreqforchecking) / (finetrimsizeinHz * (fine - previousfineforchecking)) < .5)
             {
                 freq = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine));
                 if ((freq - previousfreqforchecking) / (finetrimsizeinHz * (Convert.ToInt32(fine) - Convert.ToInt32(previousfineforchecking))) < .5) throw new Exception_Yellow("Frequency counter repeat or anomaly error (coarse " + coarse.ToString() + " fine " + fine.ToString() + " " + freq.ToString() + "). Please ensure that board is properly seated and try again.  If error persists, testing fails.");
             }
         }
         else if (Math.Abs(freq - previousfreqforchecking) < .3 * finetrimsizeinHz)
         {
             freq = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine));
             if (Math.Abs(freq - previousfreqforchecking) < .3 * finetrimsizeinHz) throw new Exception_Yellow("Frequency counter repeat or anomaly error (coarse " + coarse.ToString() + " fine " + fine.ToString() + " " + freq.ToString() + "). Please ensure that board is properly seated and try again.  If error persists, testing fails.");
         }
     }
     previousfreqforchecking = freq;
     previousfineforchecking = fine;
     previouscoarseforchecking = coarse;
 }
 public Trimmer(Parameters pars, FTDIdevice USB)
 {
     parameters = pars;
     thisUSB = USB;
     //Frequency counter must be programmed with the following address information: GPIB ID 0, primary address 9, and no secondary address.
     //FREQ_COUNTER = new Device(0, 15, 0); // create an object to hold the frequency counter, with GPIB ID 0, primary addr 9, and no secondary addr.  the primary addr was manually set on the counter itself.
     FREQ_COUNTER = new Device(0, parameters.counter_id, 0); // create an object to hold the frequency counter, with GPIB ID 0, primary addr 9, and no secondary addr.  the primary addr was manually set on the counter itself.
 }
        public static void WaitForZTCResponse(FTDIdevice thisdevice, Parameters parameters)
        {
            byte[] readResult;
            int PLengthInt;
            int i = 1;
            bool responseA = false;
            string cache = string.Empty;
            string readOut = string.Empty;
            string packet = string.Empty;
            char[] charcache;
            byte[] PLength = new Byte[1];

            while (i <parameters.listenlooptimeout && responseA == false)
            {
                readResult = thisdevice.poll();
                readOut = ConvertByteArrayToSpacedHexString(readResult);
                cache = cache + readOut;
                charcache = cache.ToCharArray(0, cache.Length);

                if (cache.Length > 11)
                {
                    PLength = ConvertHexStringToByteArray(cache.Substring(9, 2));
                    byte[] PLengthB = new byte[2];
                    PLengthB[0] = PLength[0];
                    PLengthInt = BitConverter.ToInt16(PLengthB, 0);
                    if (cache.Length > 12 + PLengthInt * 3)
                    {
                        packet = cache.Substring(0, 14 + PLengthInt * 3);
                        cache = cache.Remove(0, 14 + PLengthInt * 3);
                        packet = packet.Remove(0, 2);
                        packet = packet.Remove(packet.Length - 2);
                        //Console.WriteLine("From Port 1: {0}", packet);
                        responseA = true;
                    }
                }

                System.Threading.Thread.Sleep(20);
                i++;
                if (i == parameters.listenlooptimeout)
                    throw new Exception_Yellow("No response received during listen.");
            }
        }
        public static bool WaitForZTCResponseAndIndication(FTDIdevice EndDevice, FTDIdevice Coordinator, string packetsent)
        {
            //StartTestingTimer3();
            //StartTestingTimer2(); string time2 = GetTestingTimestamp2();
            //string TxTime = GetTimestamp();
            //string RxTime = string.Empty;
            //int deltaT = 0;
            int success = 0;

            byte[] readResult;
            int PLengthInt;
            int i = 1;
            bool responseA = false;
            string cache = string.Empty;
            string readOut = string.Empty;
            string packet = string.Empty;
            char[] charcache;
            byte[] PLength = new Byte[1];

            byte[] readResult2;
            int PLengthInt2;
            bool responseB = false;
            string cache2 = string.Empty;
            string readOut2 = string.Empty;
            string packet2 = string.Empty;
            char[] charcache2;
            byte[] PLength2 = new Byte[1];

            while (i < 40 && (responseA == false || responseB == false))
            {
                //time2 = GetTestingTimestamp2();
                if (responseA == false)
                {
                    readResult = EndDevice.poll();                             //read from port of device A
                    readOut = ConvertByteArrayToSpacedHexString(readResult);
                    cache = cache + readOut;                                //add most recent byte from port A to cache
                    charcache = cache.ToCharArray(0, cache.Length);

                    if (cache.Length > 11)                                  //if the cache is long enough to hold a full transmission (at least 4 bytes in string format)
                    {
                        PLength = ConvertHexStringToByteArray(cache.Substring(9, 2));  // packet length = the length stored in the length byte of the transmission
                        byte[] PLengthB = new byte[2];
                        PLengthB[0] = PLength[0];
                        PLengthInt = BitConverter.ToInt16(PLengthB, 0);
                        if (cache.Length > 12 + PLengthInt * 3)             //if transmission is complete (i.e. if the number of bytes specified by the length byte have all been received)
                        {
                            packet = cache.Substring(0, 14 + PLengthInt * 3);
                            cache = cache.Remove(0, 14 + PLengthInt * 3);
                            packet = packet.Remove(0, 2);
                            packet = packet.Remove(packet.Length - 2);      //removing extraneous bytes, like parity byte?
                            //Console.WriteLine("From Port 1: {0}", packet);
                            responseA = true;                               //done listening
                        }
                    }
                }
                //time2 = GetTestingTimestamp2();

                if (responseB == false)
                {
                    readResult2 = Coordinator.poll();
                    readOut2 = ConvertByteArrayToSpacedHexString(readResult2);
                    cache2 = cache2 + readOut2;
                    charcache2 = cache2.ToCharArray(0, cache2.Length);

                    if (cache2.Length > 11)
                    {
                        PLength2 = ConvertHexStringToByteArray(cache2.Substring(9, 2));
                        byte[] PLengthC = new byte[2];
                        PLengthC[0] = PLength2[0];
                        PLengthInt2 = BitConverter.ToInt16(PLengthC, 0);
                        if (cache2.Length > 12 + PLengthInt2 * 3)
                        {
                            packet2 = cache2.Substring(0, 14 + PLengthInt2 * 3);
                            cache2 = cache2.Remove(0, 14 + PLengthInt2 * 3);
                            packet2 = packet2.Remove(0, 2);
                            packet2 = packet2.Remove(packet2.Length - 2);
                            //Console.WriteLine("From Port 2: {0}", packet2);
                            cache2 = string.Empty;

                            //Packet comparing
                            packet2 = packet2.Replace(" ", "");
                            packetsent = packetsent.Replace(" ", "");
                            if (packet2.Length == packetsent.Length + 2)
                            {
                                if (string.Compare(packet2.Substring(packet2.Length - 8, 8), packetsent.Substring(packetsent.Length - 8, 8), true) == 0)
                                {
                                    responseB = true;
                                    success = 1;
                                    //RxTime = GetTimestamp();
                                    //string RxTimeMillisec = RxTime.Substring(12, 6);
                                    //string TxTimeMillisec = TxTime.Substring(12, 6);
                                    //deltaT = int.Parse(RxTimeMillisec) - int.Parse(TxTimeMillisec);
                                }

                            }
                        }
                    }
                }
                //time2 = GetTestingTimestamp2();
                System.Threading.Thread.Sleep(1);
                i++;
                //if (i == 40) timeoutcounter++;
            }
            //if (deltaT <= 0)
            //    deltaT = 0;

            packetsent = packetsent.Replace(" ", "");

            //if (responseA == false)
            //Console.WriteLine("Port 1: No Reply");
            //if (responseB == false)
            //Console.WriteLine("Port 2: No Reply");

            //string time = GetTestingTimestamp3();

            if (responseA == true && responseB == true)
                return true;
            else return false;
        }
 public static void SendZTCCommand(FTDIdevice thisdevice, string commandhex)
 {
     thisdevice.WriteByte(MakePacket(ConvertHexStringToByteArray(commandhex)));
 }
        public byte[] AddtoReceiveList_ListenForNewFWResponse(FTDIdevice thisdevice, Parameters parameters)
        {
            Queue<byte> incomingbytequeue = new Queue<byte>();
            byte ZTCHeader = 0x02;
            int initialqueuelen = incomingbytequeue.Count;
            int indexofZTCheaderinqueue = 0;
            byte[] incomingdata = thisdevice.poll();
            int arraylen = incomingdata.Length;
            for (int i = 0; i < arraylen; i++)
                incomingbytequeue.Enqueue(incomingdata[i]);
            int queuelen = incomingbytequeue.Count;
            byte temp;
            bool packetstarted = false;
            for (int j = 0; j < queuelen; j++)
            {
                temp = incomingbytequeue.Dequeue();
                if (temp == ZTCHeader && incomingbytequeue.Count > 3)
                {
                    packetstarted = true;
                    break;
                }
                else
                {
                    //incomingbytequeue.Enqueue(temp); //this is a problem. we should throw this out.
                    indexofZTCheaderinqueue++;
                }
            }
            if (packetstarted)
            {
                byte tempheader = incomingbytequeue.Dequeue();
                byte tempopcode = incomingbytequeue.Dequeue();
                int payloadlen = incomingbytequeue.Dequeue();
                if (incomingbytequeue.Count < payloadlen + 1)  //if the packet hasn't fully arrived yet, just put the packets we've read so far back on the queue and then exit, to come back and read again later
                {
                    int count = incomingbytequeue.Count;
                    incomingbytequeue.Enqueue(ZTCHeader);
                    incomingbytequeue.Enqueue(tempheader);
                    incomingbytequeue.Enqueue(tempopcode);
                    incomingbytequeue.Enqueue((byte)payloadlen);
                    for (int k = 0; k < count; k++)
                        incomingbytequeue.Enqueue(incomingbytequeue.Dequeue());
                    return null;
                    //throw new Exception_Yellow("incomplete packet received.");
                }
                byte[] packet = new byte[payloadlen + 5];
                packet[0] = ZTCHeader;
                packet[1] = tempheader;
                packet[2] = tempopcode;
                packet[3] = (byte)payloadlen;
                for (int k = 0; k < payloadlen; k++)
                    packet[4 + k] = incomingbytequeue.Dequeue();
                packet[payloadlen + 4] = incomingbytequeue.Dequeue(); //CRC
                //if (packet[payloadlen + 4] == 0) packet[0] = 2; //dummy just to have breakpoint to test theory that 0 is returned sometimes

                return packet;
                //RxPacket rx = new RxPacket();
                //rx.completepacket = packet;
                //RxFIFO.Enqueue(rx);
                //AllRxPacketQueueDebugging.Enqueue(rx);
                ////now figure out what to do with the rest of the packets that are hanging out there.  re-enqueue them probably
            }
            else return null;
            //if(incomingpacket!=null && incomingpacket.Length!=0)
            //RxFIFO.Enqueue(incomingpacket);
        }
 public Trimmer_NI4882(Parameters pars, FTDIdevice USB)
 {
     parameters = pars;
     thisUSB = USB;
     FREQ_COUNTER = new Device(0, 9, 0); // create an object to hold the frequency counter, with GPIB ID 0, primary addr 9, and no secondary addr.  the primary addr was manually set on the counter itself.
 }
        //    newtrimmer(initialcoarse, initialtrim1, initialtrim2)
        public void NewTrimmer(FTDIdevice thisUSB, int coarse, double finetrim, double backupfinetrim)
        {
            SSLInterface ssl = new SSLInterface(parameters, thisUSB);

            thisUSB.poll();  //clear out anything sitting on the port

            double fine1 = finetrim;        //should be the mode of the trimming results
            double fine2 = backupfinetrim;  //if newton's method doesn't get it on the first try we use secant method with this backupfinetrim and finetrim
            bool done = false;
            double temp;
            double freq2 = -12345;  //dummy value to avoid error
            double freq1 = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine1));
            checkFreqValidity(fine1, ref freq1, coarse, thisUSB, FREQ_COUNTER, ssl);
            if (freqWithin8(coarse, fine1, freq1)) done = true;
            TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine1, 3).ToString() + " " + freq1.ToString());
            if (!done) // this if statement is here just in case we got it right the first time
            {
                temp = fine1 - (freq1 - targetfrequency) / finetrimsizeinHz;    //use Newton's method to find the second point, so we finish after 2 measurements more often
                if (temp <= 31 && temp >= 0) fine2 = temp;                      //but only use it if it gives a value between 0 and 31, otherwise revert to input fine2
                freq2 = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine2));         // in the future, could adjust this to save a step when adjusting coarse, but we expect to adjust coarse only very rarely
                checkFreqValidity(fine2, ref freq2, coarse, thisUSB, FREQ_COUNTER, ssl);
                if (freqWithin8(coarse, fine2, freq2)) done = true;
                TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine2,3).ToString() + " " + freq2.ToString());
            }
            while (!done && Math.Abs(fine2 - fine1) > 1)  //while not done and trims are more than 1 apart
            {

                temp = fine2 - ((freq2 - targetfrequency) * (fine2 - fine1)) / (freq2 - freq1);  //use the secant method
                fine1 = fine2;
                fine2 = temp;
                freq1 = freq2;
                int intfine2 = Convert.ToInt16(fine2);
                if (intfine2 <= 31 && intfine2 >= 0)
                {
                    //temp = fine2 - ((freq2 - targetfrequency) * (fine2 - fine1)) / (freq2 - freq1);  //use the secant method
                    //fine1 = fine2;
                    //fine2 = temp;
                    //freq1 = freq2;
                    freq2 = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine2));
                    checkFreqValidity(fine2, ref freq2, coarse, thisUSB, FREQ_COUNTER, ssl);
                    if (freqWithin8(coarse, fine2, freq2)) done = true;
                    TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine2,3).ToString() + " " + freq2.ToString());
                }
                else if (intfine2 > 31)
                {
                    if (intfine2 > 36)
                    {
                        while (freq2 > targetfrequency && coarse < 31)
                        {
                            coarse++; //could replace this with coarse += (intfine2-31)/7 if we ever saw cases of needing to adjust by >1 coarse trim
                            if (coarse == 16) coarse = 28;  //accounting for the overlap in coarse trim values due to the 5th cap being only 4 pf
                            freq2 = FreqAtTrim(thisUSB, ssl, coarse, 31);
                            checkFreqValidity(fine2, ref freq2, coarse, thisUSB, FREQ_COUNTER, ssl);
                            if (freqWithin8(coarse, fine2, freq2)) done = true;
                            TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine2,3).ToString() + " " + freq2.ToString());
                        }
                    }
                    else coarse++;
                    NewTrimmer(thisUSB, coarse, 31, 24);
                    done = true;
                }
                else if (intfine2 < 0)
                {
                    if (intfine2 < -5)
                    {
                        while (freq2 < targetfrequency && coarse > 0)
                        {
                            coarse--; //could replace this with coarse += (intfine2)/7 if we ever saw cases of needing to adjust by >1 coarse trim
                            freq2 = FreqAtTrim(thisUSB, ssl, coarse, 0);
                            checkFreqValidity(fine2, ref freq2, coarse, thisUSB, FREQ_COUNTER, ssl);
                            if (freqWithin8(coarse, fine2, freq2)) done = true;
                            TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine2,3).ToString() + " " + freq2.ToString());
                        }
                    }
                    else coarse--;
                    NewTrimmer(thisUSB, coarse, 0, 7);
                    done = true;
                }
            }
            if (!done)  //(could have become done from coarse changing / recursive call; this code is only reached if we've finished trimming with fine trims 1 away from each other but freqs not within 8)
            {
                //if we've trimmed as well as possible but no frequency was within 8 Hz, choose which one was closer (and if it was fine1, re-set that trim).
                if (Math.Abs(freq1 - targetfrequency) < Math.Abs(freq2 - targetfrequency))
                {
                    //FTinti.Command(FTinti, "95 0A 02 " + FTinti.TrimPacketPrep(Convert.ToInt32(fine1)) + FTinti.TrimPacketPrep(coarse));
                    FinishTrim(coarse, fine1, freq1);
                }
                else FinishTrim(coarse, fine2, freq2);
            }
        }
        //checks frequency over GPIB at given trim values
        public double FreqAtTrim(FTDIdevice thisUSB, SSLInterface ssl, int coarse, int fine)
        {
            double freq = 0;
            //setting trim when talking to SSL:
            try
            {
                ssl.SSL_SetTrims((byte)fine, (byte)coarse);
            }
            catch (Exception_Yellow exc)
            {
                if (!exc.Message.Contains("retry failed")) throw new Exception_Yellow(exc.Message); //if it's some other exception, don't catch it
                else ssl.SSL_SetTrims((byte)fine, (byte)coarse); //retry once, if the exception comes back again, we won't catch it and it'll go on to be displayed as "retry failed"
            }
            //unused code for setting trim if talking to ZTC:
            //Utils.SendZTCCommand(thisUSB, "95 0A 02 " + TrimPacketPrep(fine) + TrimPacketPrep(coarse));
            //    Utils.WaitForZTCResponse(thisUSB, parameters);
            ////System.Threading.Thread.Sleep(as long as settling takes);

            FREQ_COUNTER.Write(":MEASURE:FREQ? 12, 0.0000001");
            //FREQ_COUNTER.Write(":meas:freq? 12,0.0000001");

            //System.Threading.Thread.Sleep(2000);
            freq = Convert.ToDouble(FREQ_COUNTER.ReadString());
            return freq;
        }
        //checks frequency over GPIB at given trim values
        public double FreqAtTrim(FTDIdevice thisUSB, int coarse, int fine)
        {
            double freq = 0;
            Utils.SendZTCCommand(thisUSB, "95 0A 02 " + TrimPacketPrep(fine) + TrimPacketPrep(coarse));
                Utils.WaitForZTCResponse(thisUSB, parameters);
            //System.Threading.Thread.Sleep(as long as settling takes);

            //FREQ_COUNTER.Write(":meas:freq? 12,0.0000001");
                FREQ_COUNTER.Write(":MEASURE:FREQ? 12,0.0000001");
            //System.Threading.Thread.Sleep(2000);
            freq = Convert.ToDouble(FREQ_COUNTER.ReadString());
            return freq;
        }
 //, string imagefilepath)
 //int BIN_FILE_BUFFER_SIZE = 4 * 1024;    //Buffer allocated for reading the SSL binary image
 public FirmwareLoader(Parameters pars, FTDIdevice USB, MainForm mf)
 {
     parameters = pars;
     thisUSB = USB;
     mf_parent = mf;
 }
 public SSLInterface(Parameters pars, FTDIdevice USB)
 {
     parameters = pars;
     thisUSB = USB;
 }
 public void CheckConnections(ref bool counterattached, ref bool refradioattached)
 {
     if (parameters.crystaltrimming)
     {
         try
         {
             Trimmer_NI4882.testtrimmerisattached(parameters.counter_id);
         }
         catch (Exception exc)
         {
             if (exc.Message.Contains("GPIB") || exc.Message.Contains("handle is out of range")) exc = new Exception_Yellow("Error involving the frequency counter or GPIB connection (" + exc.Message + " 1).  Confirm that the GPIB cable is plugged into both the computer and the frequency counter, or turn off crystal trimming in the settings file.");
             UpdateOutputText(exc.Message);
             counterattached = false;
         }
     }
     if (parameters.testing)
     {
         try
         {
             referenceRadio = new FTDIdevice(parameters, this);
             referenceRadio.OpenPort(parameters.reference_radio_pid, 3000);
             parameters.refradioplugged = true;
             UpdateProgressBar_Detail(0);
         }
         catch (Exception exc)
         {
             UpdateOutputText(exc.Message);
             refradioattached = false;
         }
     }
 }
        public void RunButton_Click(object sender, EventArgs e)
        {
            UpdateOutputText("Process has begun.");
            UpdateColorDisplay("white");
            UpdateProgressBar_Overall(0);
            UpdateProgressBar_Detail(0);

            pictureBox1.Visible = false;
            pictureBox2.Visible = false;

            parameters = new Parameters();
            parameters.ReadSettingsFile();  //must happen after new parameters is built in order for settings to be saved as params
            if (parameters.testing) parameters.refradioplugged = true; //we need to re-set this to true because we've made a new set of parameters, and if testing is true but the ref radio isn't there, we would already have seen an error.
            //open dialog box for filename, if option is selected
            if (!parameters.takingFWfilenamefromsettingsfile)
            {
                CancelEventArgs e2 = new CancelEventArgs();
                parameters.FWimagefilepath = openFileDialog1_FileOk(sender, e2);
                int lastslash = parameters.FWimagefilepath.LastIndexOf('\\') + 1;
                parameters.FWimagefilename = parameters.FWimagefilepath.Substring(lastslash, parameters.FWimagefilepath.Length-lastslash);
            }

            thisUSB = new FTDIdevice(parameters, this); //must happen after settings file is read

            MainThread = new Thread(new ThreadStart(MainProcess));
            MainThread.Start();
        }