Esempio n. 1
0
        private void SetRFSB1Button_Click(object sender, EventArgs e)
        {
            int   Frequency = (int)SB1RFFreq.Value;
            float Amplitude = (float)SB1RFAmp.Value;

            GPIB.InitDevice(20);
            GPIB.SetAmplitude(Amplitude);
            GPIB.SetFrequency(Frequency);
            GPIB.CloseDevice();
        }
Esempio n. 2
0
        public void FPGAReadMethod()
        {
            int Frequency         = startFreq;
            int CurrentWindowStep = 0;

            int CurrentSideband = 0;

            int CurrentPulseLength = fixed_startLength;     // For fixed spectra with a varying pulse length

            int numberOfFiles = this.myFileName.Length;

            TextWriter myFile = new StreamWriter(myFileName[CurrentSideband], true);

            if (myFile != null)
            {
                WriteMessage("Opened first file for writing");
            }
            else
            {
                WriteMessage("Couldn't open first file for writing", true);
            }

            // Create list for storing readings, get ready for 2000 data points
            List <int> Readings = new List <int>(2000);

            while (FPGAReadThread.IsAlive && bShouldQuitThread == false)
            {
                // If flag is set to reset FPGA, do so
                if (bResetFPGA)
                {
                    FPGA.SendResetSignal();
                    bResetFPGA = false;

                    myFile.Close();

                    return;
                }

                // If there are exactly 4 bytes in the queue... do nothing?
                // Might be able to restructure this bit. Don't quite understand - it seems to just write a message if there are
                // too many bytes, but otherwise continues as normal. Not sure where the continue statement skips to
                // I think it skips the rest of this function...

                if (FPGA.CheckQueue() == 4)
                {
                }
                else
                {
                    if (FPGA.CheckQueue() > 4)
                    {
                        WriteMessage("Too Many Bytes in Queue!!", true);
                    }
                    else
                    {
                        continue;
                    }
                }



                // Fill array of bytes with however many are in the queue
                byte[] Bytes = FPGA.ReadBytes();


                // So long as there are some bytes there
                // Statement above means that there should always be 4
                if (Bytes != null && Bytes.Length != 0)
                {
                    byte[] Data = Bytes;

                    // Check for certain error codes
                    // This if statement can be left as-is
                    if (Bytes[3] != 181 && Bytes[3] != 77 && Bytes[3] != 173)
                    {
                        // Changed from WriteOutout to WriteMessage
                        WriteMessage("Warning: Received corrupted data!\r\n");

                        int Info1 = FPGA.InfoRequest();
                        int Info2 = FPGA.InfoRequest();
                        int Info3 = FPGA.InfoRequest();
                        int Info4 = FPGA.InfoRequest();

                        while ((Info1 != Info2) || (Info3 != Info4) || (Info1 != Info3))
                        {
                            Info1 = FPGA.InfoRequest();
                            Info2 = FPGA.InfoRequest();
                            Info3 = FPGA.InfoRequest();
                            Info4 = FPGA.InfoRequest();
                        }

                        Data = BitConverter.GetBytes(Info1);

                        // Changed from WriteOutout to WriteMessage
                        WriteMessage("Recovered successfully\r\n");
                    }



                    // Received data, need to deal with it
                    if (Data[3] == 181)
                    {
                        Data[3] = 0;
                        int NumReadings = BitConverter.ToInt32(Data, 0);
                        // Changed from WriteOutout to WriteMessage
                        WriteMessage("Found " + NumReadings.ToString() + " Readings\r\n");

                        uint   rxbytes = FPGA.CheckQueue();
                        uint   NumRead = 0;
                        byte[] buffer  = new byte[rxbytes];

                        if (rxbytes != 0) //clear out any bytes left in the buffer... shouldn't hit this
                        {
                            FPGA.USBPort.Read(buffer, rxbytes, ref NumRead);
                            // Changed from WriteOutout to WriteMessage
                            WriteMessage("Warning: Found extra bytes in FPGA output queue\r\n");
                        }

                        FPGA.FinishInfoRequest();

                        List <byte> DataOut = new List <byte>(NumReadings * 2);

                        rxbytes = FPGA.CheckQueue();

                        while (DataOut.Count < NumReadings * 2)
                        {
                            rxbytes = FPGA.CheckQueue();
                            if (rxbytes > 0)
                            {
                                buffer = new byte[rxbytes];
                                FPGA.USBPort.Read(buffer, rxbytes, ref NumRead);
                                DataOut.AddRange(buffer);
                            }
                        }

                        buffer = DataOut.ToArray();

                        for (int j = 0; j < buffer.Length; j = j + 2)
                        {
                            int Reading = buffer[j] + 256 * buffer[j + 1];
                            Readings.Add(Reading);
                        }

                        foreach (int j in Readings)
                        {
                            myFile.WriteLine(j.ToString());
                        }

                        // Only send live data to the viewer if it is open
                        if (this.IsViewerOpen)
                        {
                            if (specType == "Fixed")
                            {
                                myViewer.addLiveData(Readings, CurrentWindowStep, 0, CurrentPulseLength);
                            }
                            else
                            {
                                myViewer.addLiveData(Readings, CurrentWindowStep, startFreqArray[CurrentSideband], 0);
                            }
                        }
                        // Clear buffers for writing to file, gets ready for writing more data next time
                        myFile.Flush();
                        // Clear list of readings
                        Readings.Clear();

                        if (specType == "Fixed")
                        {
                            CurrentPulseLength += fixed_stepSize;
                        }

                        FPGA.ResetDevice();

                        // If the Pause flag is set
                        while (PauseExperiment)
                        {
                            //sleep for 1ms so we don't use all the CPU cycles
                            System.Threading.Thread.Sleep(1000);
                        }

                        FPGA.SendReadingFinish();
                        //break;
                    }
                    else if (Data[3] == 77)
                    {
                        FPGA.FinishInfoRequest();

                        Data[3] = 0;
                        int ExtraData = BitConverter.ToInt32(Data, 0);
                        if (ExtraData == 0xAB25FC)
                        {
                            WriteMessage("Need to change frequency!\r\n");
                            if (bIsFreqGenEnabled)
                            {
                                //Change Frequency :)
                                if (specType == "Windowed")
                                {
                                    if (CurrentWindowStep < sbWidth)
                                    {
                                        Frequency += stepSize;
                                        GPIB.SetFrequency(Frequency);
                                        CurrentWindowStep++;
                                    }
                                    else if (CurrentWindowStep >= sbWidth)
                                    {
                                        // This means we have come to the end of one sideband
                                        // So flush & close that file
                                        myFile.Flush();
                                        myFile.Close();

                                        CurrentSideband++;

                                        if (CurrentSideband < (sbToScan * 2) + 1)
                                        {
                                            // New sideband, so open the next file, using filename from array
                                            myFile = new StreamWriter(myFileName[CurrentSideband], true);


                                            Frequency = startFreqArray[CurrentSideband];
                                            GPIB.SetFrequency(Frequency);
                                            CurrentWindowStep = 0;
                                        }
                                        //if we reach end of final sideband, stop experiment (need to test this section)
                                        else
                                        {
                                            MessageBox.Show("Experiment Finished! (Reached final sideband)", "Bang");
                                            bShouldQuitThread = true;
                                            // break;       // might need this??
                                        }
                                    }
                                }
                                else if (specType == "Continuous")
                                {
                                    Frequency += stepSize;
                                    GPIB.SetFrequency(Frequency);
                                    CurrentWindowStep++;
                                }
                            }
                            else
                            {
                                WriteMessage("Frequency Generator not enabled!\r\n");
                            }

                            while (PauseExperiment)
                            {
                                //sleep for 1ms so we don't use all the CPU cycles
                                System.Threading.Thread.Sleep(1000);
                            }

                            FPGA.SendFreqChangeFinish();
                        }
                        else
                        {
                            WriteMessage("Received corrupted frequency change command!\r\n");
                        }
                    }
                    else if (Data[3] == 173)
                    {
                        FPGA.FinishInfoRequest();

                        Data[3] = 0;
                        int ExtraData = BitConverter.ToInt32(Data, 0);
                        if (ExtraData == 0xFC32DA)
                        {
                            WriteMessage("Received experiment stop command!\r\n");
                            MessageBox.Show("Experiment Finished!", "Bang");
                            bShouldQuitThread = true;
                        }
                        else
                        {
                            WriteMessage("Received corrupted experiment stop command!\r\n");
                        }
                        break;
                    }
                    else
                    {
                        WriteMessage("Received corrupted data (Unrecoverable)!\r\n");
                    }
                }
            }


            // If it's a windowed spectrum, we will already have closed the file. Otherwise, need to flush & close it
            if (specType != "Windowed")
            {
                myFile.Flush();
                myFile.Close();
            }

            this.ExperimentFinished();

            FPGA.ResetDevice();
        }
Esempio n. 3
0
        // Method to respond to user clicking start button
        private void StartButton_Click(object sender, EventArgs e)
        {
            // If we are restarting the experiment after it being paused, just reset the PauseExperiment flag
            if (PauseExperiment == true)
            {
                PauseExperiment       = false;  // Set flag
                PauseButton.Enabled   = true;   // Re-enable pause button
                StartButton.Enabled   = false;  // Disable start button
                OpenUSBButton.Enabled = false;  // Disable open USB button
            }
            else
            {                 // Otherwise, start experiment
                this.Reset(); // Reset first

                if (FPGA.bUSBPortIsOpen == false)
                {
                    WriteMessage("Can't Send Start Signal to FPGA: USB port is not open", true);
                    return;
                }
                else
                {
                    //Grab all scan and trap parameters from form:
                    specType   = specTypeBox.SelectedItem.ToString();
                    specDir    = specDirBox.SelectedItem.ToString();
                    trapV      = (float)(1000 * trapVBox.Value); //Trap voltage stored in millivolts
                    axFreq     = (int)(1000 * axFreqBox.Value);
                    modcycFreq = (int)(1000 * modcycFreqBox.Value);
                    magFreq    = (int)(1000 * magFreqBox.Value);
                    startFreq  = (int)(1000000 * startFreqBox.Value);
                    carFreq    = (int)(1000000 * carFreqBox.Value);
                    stepSize   = (int)(1000 * stepSizeBox.Value);
                    sbToScan   = (int)sbToScanBox.Value;
                    sbWidth    = (int)sbWidthBox.Value;
                    rfAmp      = (float)rfAmpBox.Value;

                    // Metadata ordering in array:
                    // 0: Date
                    // 1: Spectrum type
                    // 2: 729 direction
                    // 3: Trap voltage
                    // 4: Axial freq (kHz)
                    // 5: Modified cyc freq (kHz)
                    // 6: Magnetron freq (kHz)
                    // 7: AOM start freq (MHz)
                    // 8: Carrier freq (MHz)
                    // 9: Step size (kHz or ticks)
                    // 10: Sidebands/side
                    // 11: Sideband width (steps)
                    // 12: 729 RF amplitude
                    // 13: Number of repeats
                    // 14: Number interleaved
                    // 15: Which sideband
                    // 16: Starting pulse length (fixed)
                    // 17: Number of steps (fixed)
                    // 18 + i: spectrum i name



                    // Create new dialog to get data from user before starting the experiment
                    StartExperimentDialog myExperimentDialog = new StartExperimentDialog();
                    myExperimentDialog.ShowDialog();
                    if (myExperimentDialog.DialogResult != DialogResult.Cancel)
                    {
                        // Create & fill in metadata
                        string[] metadata = new string[23];
                        metadata[0] = DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss");
                        // This is all from the CoreForm
                        metadata[1]  = specType;
                        metadata[2]  = specDir;
                        metadata[3]  = this.trapVBox.Value.ToString();
                        metadata[4]  = this.axFreqBox.Value.ToString();
                        metadata[5]  = this.modcycFreqBox.Value.ToString();
                        metadata[6]  = this.magFreqBox.Value.ToString();
                        metadata[7]  = this.startFreqBox.Value.ToString();
                        metadata[8]  = this.carFreqBox.Value.ToString();
                        metadata[9]  = this.stepSizeBox.Value.ToString();
                        metadata[10] = this.sbToScanBox.Value.ToString();
                        metadata[11] = this.sbWidthBox.Value.ToString();
                        metadata[12] = this.rfAmpBox.Value.ToString();
                        // Fill in remaining metadata from form
                        metadata[13] = myExperimentDialog.NumberOfRepeats.Value.ToString();
                        metadata[14] = myExperimentDialog.NumberOfSpectra.Value.ToString();
                        metadata[15] = "N/A";
                        metadata[16] = "N/A";   // For fixed spectra only
                        metadata[17] = "N/A";   // For fixed spectra only

                        int numberOfSpectra = (int)myExperimentDialog.NumberOfSpectra.Value;
                        for (int i = 0; i < numberOfSpectra; i++)
                        {
                            metadata[i + 18] = myExperimentDialog.SpectrumNames[i].Text;
                        }

                        metadata[18 + numberOfSpectra] = myExperimentDialog.NotesBox.Text;

                        // Retrieve the folder path selected by the user
                        string FolderPath = myExperimentDialog.getFilePath();
                        // Make sure the
                        if (FolderPath != null)
                        {
                            TextWriter[] myFile;        // Declare array of files

                            // If "Continuous" experiment type has been selected
                            if (specType == "Continuous")
                            {
                                //Turn on frequency generator
                                bIsFreqGenEnabled = true;

                                //Start frequency is the value taken directly from the form, no windowing
                                startFreqArray    = new int[1];
                                startFreqArray[0] = startFreq;

                                // Create a single file and put all readings in there
                                myFileName = new string[1];
                                myFile     = new TextWriter[1];

                                // Create empty RabiSelector object to pass to writeMetadataToFile (not used)
                                RabiSelector myRabiSelector = new RabiSelector();

                                // Create the file with appropriate name & write metadata to it
                                writeMetadataToFile(ref myExperimentDialog, ref myRabiSelector, ref FolderPath, ref myFile, 1);
                            }
                            else if (specType == "Windowed")
                            {
                                //Turn on frequency generator
                                bIsFreqGenEnabled = true;

                                //Calculate frequency offset of sideband start frequencies from sideband centres
                                int offsetFreq = (int)stepSize * sbWidth / 2;
                                //Determine window spacing from trap frequencys and the type of spectrum selected

                                int windowSpace = 0;
                                if (specDir == "Axial")
                                {
                                    windowSpace = (int)(axFreq / 2);
                                }
                                else if (specDir == "Radial")
                                {
                                    windowSpace = (int)(modcycFreq / 2);
                                }

                                //Array of start frequencies for each sideband (from furthest red to furthest blue)
                                startFreqArray = new int[sbToScan * 2 + 1];
                                for (int sb = 0; sb < (sbToScan * 2 + 1); sb++)
                                {
                                    startFreqArray[sb] = carFreq - offsetFreq - (windowSpace * (sbToScan - sb));
                                }

                                // We want a file for each sideband with appropriate naming
                                // Calculate how many files we will need - one for each R/B sideband plus one for carrier
                                int numberOfFiles = (int)(sbToScan * 2 + 1);
                                // Create array of filenames & array of files
                                myFileName = new string[numberOfFiles];
                                myFile     = new TextWriter[numberOfFiles];

                                // Create empty RabiSelector object to pass to writeMetadataToFile (not used)
                                RabiSelector myRabiSelector = new RabiSelector();

                                // Generate filenames and actually create files
                                writeMetadataToFile(ref myExperimentDialog, ref myRabiSelector, ref FolderPath, ref myFile, numberOfFiles);
                            }
                            else if (specType == "Fixed")
                            {
                                // Maybe put a box for user to input which pulses are varied in length

                                //Start frequency is the value taken directly from the form, no windowing
                                startFreqArray    = new int[1];
                                startFreqArray[0] = startFreq;

                                // Show form for user to enter details about fixed frequency sequence
                                // (need starting pulse length & step size)
                                RabiSelector myRabiSelector = new RabiSelector();
                                myRabiSelector.generateSequenceButton.Enabled = false;
                                myRabiSelector.pulseSelectBox.Enabled         = false;
                                myRabiSelector.repeatsSelect.Enabled          = false;
                                myRabiSelector.ShowDialog();

                                // Get starting pulse length & step size from user form
                                fixed_startLength = (int)myRabiSelector.startLengthSelect.Value;
                                fixed_stepSize    = (int)myRabiSelector.stepSizeSelect.Value;
                                // Change step size in metadata
                                metadata[9]  = fixed_stepSize.ToString();
                                metadata[16] = fixed_startLength.ToString();
                                metadata[17] = myRabiSelector.stepsSelect.Value.ToString();

                                // Create a single file and put all readings in there
                                myFileName = new string[1];
                                myFile     = new TextWriter[1];

                                writeMetadataToFile(ref myExperimentDialog, ref myRabiSelector, ref FolderPath, ref myFile, 1);

                                bIsFreqGenEnabled = false;
                            }

                            // If myViewer is not open
                            if (IsViewerOpen)
                            {
                                myViewer.Close();
                                IsViewerOpen = false;
                            }
                            // Create new instance of viewer
                            myViewer = new Spectroscopy_Viewer.SpectroscopyViewerForm(ref metadata);
                            // Set up event handler to deal with viewer closing - must be done after it is constructed
                            myViewer.FormClosing += new FormClosingEventHandler(myViewer_FormClosing);
                            // Set up event handler to deal with event raised when pause button on viewer is clicked
                            // This should trigger the pause button in the main window
                            myViewer.PauseEvent += new SpectroscopyViewerForm.PauseEventHandler(PauseButton_Click);
                            // Show viewer
                            myViewer.Show();
                            // Set boolean  to indicate that viewer is open
                            IsViewerOpen = true;
                        }
                        else
                        {
                            MessageBox.Show("Error selecting folder. Please try again.");
                        }

                        // Code required to start the experiment running:
                        bShouldQuitThread = false;

                        GPIB.InitDevice(19);
                        GPIB.SetAmplitude(rfAmp);
                        GPIB.SetFrequency(startFreq);

                        SendSetupFinish();

                        // Start experiment
                        StartReadingData();
                    }
                }
            }
        }