public static void SetLasers(bool L397B1, bool L397B2, bool L729, bool L854, bool P0, bool P1, bool L854P, bool L854F, bool LAux1, bool P2) { if (!bUSBPortIsOpen) { return; } byte Data = 0; Data += (byte)(GetIntFromBool(L854F) << 7); Data += (byte)(GetIntFromBool(L854P) << 6); Data += (byte)(GetIntFromBool(P1) << 5); Data += (byte)(GetIntFromBool(P0) << 4); Data += (byte)(GetIntFromBool(L854) << 3); Data += (byte)(GetIntFromBool(L729) << 2); Data += (byte)(GetIntFromBool(L397B2) << 1); Data += (byte)(GetIntFromBool(L397B1) << 0); byte Data2 = 252; Data2 += (byte)(GetIntFromBool(P2) << 1); Data2 += (byte)(GetIntFromBool(LAux1) << 0); byte[] x = new byte[8]; x[0] = LASER_SETUP; x[1] = LASER_CMD; x[2] = 0xFF; x[3] = 0xFF; x[4] = 0xFF; x[5] = Data2; x[6] = Data; x[7] = 0xFF; FPGA.ResetDevice(10); WriteData(x, 8, 0, true); }
private void OpenUSBPort() { int devices = FPGA.CountDevices(); if (devices > 0) { WriteMessage("Device(s) detected (" + devices + ")"); FPGA.SelectDevice(0); if (FPGA.OpenDevice()) // device successfully connected { FPGA.ResetDevice(0); WriteMessage("Device Opened"); FPGA.bUSBPortIsOpen = true; //SetUSBPortText(true); } else { WriteMessage("Error: Device would not open", true); FPGA.bUSBPortIsOpen = false; //SetUSBPortText(false); } } else { FPGA.bUSBPortIsOpen = false; //SetUSBPortText(false); WriteMessage("Error: No devices detected", true); } }
/// <summary> /// Opens chosen file and sends data to FPGA 2 bytes at a time. /// </summary> void BinarySendMethod() { FileStream F = File.Open(openHexFileDialog.FileName, FileMode.Open); using (BinaryReader b = new BinaryReader(F)) { int FileSize = (int)b.BaseStream.Length; hexFileName = openHexFileDialog.FileName; WriteMessage("File name: " + hexFileName); WriteMessage("Length: " + FileSize.ToString() + " Bytes\r\n"); if (FileSize > 8000000) //should this be 2^23-1 (8 388 607) instead? { //don't bother if >8 MB WriteMessage("File too big!!\r\n", true); return; } else if (FileSize % 2 != 0) //if FileSize is an odd number { WriteMessage("File size (in bytes) is not divisible by two!\r\n", true); //FileText.Text += "File size (in bytes) is not divisible by two!\r\n"; return; } UpdateProgress(0); Byte[] Data = b.ReadBytes(FileSize); FPGA.ResetDevice(); FPGA.SendFileUploadSignal(FileSize / 2); WriteMessage("Sending Data. Approx " + (int)(((float)FileSize) * 0.00002f) + " seconds"); uint NumWritten = 0; FPGA.USBPort.Write(Data, FileSize, ref NumWritten); UpdateProgress(100); } F.Close(); }
public void FPGAReadMethod() { int Frequency = startFreq; int CurrentWindowStep = 0; int FixedCamPos = 0; int CurrentSideband = 0; int CurrentPulseLength = fixed_startLength; // For fixed spectra with a varying pulse length int numberOfFiles = this.myFileName.Length; TextWriter[] myFile; if (useCameraSpectrum != true) { myFile = new TextWriter[1]; myFile[0] = new StreamWriter(myFileName[CurrentSideband], true); } else { myFile = new TextWriter[numOfIons + 1]; for (int i = 0; i < myFile.Length; i++) { myFile[i] = new StreamWriter(myFileName[CurrentSideband + (numberOfFiles / (numOfIons + 1)) * i], 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>(10000); while (FPGAReadThread.IsAlive && bShouldQuitThread == false) { // If flag is set to reset FPGA, do so if (bResetFPGA) { FPGA.SendResetSignal(); bResetFPGA = false; for (int i = 0; i < myFile.Length; i++) { myFile[i].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[0].WriteLine(j.ToString()); } // Only send live data to the viewer if it is open if (this.IsViewerOpen) { if (useCameraSpectrum == true) { while (myCamera.isCamAcquiring() == true) { System.Threading.Thread.Sleep(100); } if (specType == "Fixed") { int[,] camDat = myCamera.getCameraData(FixedCamPos); myViewer.addLiveData(Readings, CurrentWindowStep, 0, CurrentPulseLength); myViewer.addLiveDataCAM(camDat, CurrentWindowStep, 0, CurrentPulseLength); for (int i = 0; i < numOfIons; i++) { for (int j = 0; j < readingsPerDataPoint; j++) { myFile[i + 1].WriteLine(camDat[i, j].ToString()); } } FixedCamPos++; } else { int[,] camDat = myCamera.getCameraData(CurrentWindowStep + (CurrentSideband * (1 + sbWidth))); Console.WriteLine("repeat" + (CurrentWindowStep + CurrentSideband * (1 + sbWidth))); myViewer.addLiveData(Readings, CurrentWindowStep, startFreqArray[CurrentSideband], 0); myViewer.addLiveDataCAM(camDat, CurrentWindowStep, startFreqArray[CurrentSideband], 0); for (int i = 0; i < numOfIons; i++) { for (int j = 0; j < readingsPerDataPoint; j++) { myFile[i + 1].WriteLine(camDat[i, j].ToString()); } } } } else { 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 for (int i = 0; i < myFile.Length; i++) { myFile[i].Flush(); } //myFile[i].Flush(); // Clear list of readings Readings.Clear(); if (specType == "Fixed") { CurrentPulseLength += fixed_stepSize; phase4.Value += phaseStep.Value; // Update the DDS only if there is a phase change. This is to save time in Rabi type experiments. //if (phaseStep.Value != 0) LoadDDS(freq0.Value, freq1.Value, freq2.Value, freq3.Value, freq4.Value, freq5.Value, freq6.Value, freq7.Value, amp0.Value, amp1.Value, amp2.Value, amp3.Value, amp4.Value, amp5.Value, amp6.Value, amp7.Value, phase0.Value, phase1.Value, phase2.Value, phase3.Value, phase4.Value, phase5.Value, phase6.Value, phase7.Value); // else { } } 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; freq0.Value = Frequency; freq4.Value = Frequency; phase4.Value += phaseStep.Value; LoadDDS(freq0.Value, freq1.Value, freq2.Value, freq3.Value, freq4.Value, freq5.Value, freq6.Value, freq7.Value, amp0.Value, amp1.Value, amp2.Value, amp3.Value, amp4.Value, amp5.Value, amp6.Value, amp7.Value, phase0.Value, phase1.Value, phase2.Value, phase3.Value, phase4.Value, phase5.Value, phase6.Value, phase7.Value); SetDDSProfiles.Enabled = false; CurrentWindowStep++; } else if (CurrentWindowStep >= sbWidth) { // This means we have come to the end of one sideband // So flush & close that file for (int i = 0; i < myFile.Length; i++) { myFile[i].Flush(); myFile[i].Close(); } CurrentSideband++; if (includeCarrier == true) { if (CurrentSideband < (sbToScan * 2) + 1) { // New sideband, so open the next file, using filename from array for (int i = 0; i < myFile.Length; i++) { myFile[i] = new StreamWriter(myFileName[CurrentSideband + (numberOfFiles / (numOfIons + 1)) * i], true); } Frequency = startFreqArray[CurrentSideband]; freq0.Value = Frequency; freq4.Value = Frequency; phase4.Value += phaseStep.Value; LoadDDS(freq0.Value, freq1.Value, freq2.Value, freq3.Value, freq4.Value, freq5.Value, freq6.Value, freq7.Value, amp0.Value, amp1.Value, amp2.Value, amp3.Value, amp4.Value, amp5.Value, amp6.Value, amp7.Value, phase0.Value, phase1.Value, phase2.Value, phase3.Value, phase4.Value, phase5.Value, phase6.Value, phase7.Value); SetDDSProfiles.Enabled = false; CurrentWindowStep = 0; } //if we reach end of final sideband, stop experiment (need to test this section) else { if (useCameraSpectrum == true) { myCamera.stopExp(); } MessageBox.Show("Experiment Finished! (Reached final sideband)", "Bang"); bShouldQuitThread = true; // break; // might need this?? } } else { if (CurrentSideband < (sbToScan * 2)) { // New sideband, so open the next file, using filename from array for (int i = 0; i < myFile.Length; i++) { myFile[i] = new StreamWriter(myFileName[CurrentSideband + (numberOfFiles / (numOfIons + 1)) * i], true); } Frequency = startFreqArray[CurrentSideband]; freq0.Value = Frequency; freq4.Value = Frequency; phase4.Value += phaseStep.Value; LoadDDS(freq0.Value, freq1.Value, freq2.Value, freq3.Value, freq4.Value, freq5.Value, freq6.Value, freq7.Value, amp0.Value, amp1.Value, amp2.Value, amp3.Value, amp4.Value, amp5.Value, amp6.Value, amp7.Value, phase0.Value, phase1.Value, phase2.Value, phase3.Value, phase4.Value, phase5.Value, phase6.Value, phase7.Value); SetDDSProfiles.Enabled = false; CurrentWindowStep = 0; } //if we reach end of final sideband, stop experiment (need to test this section) else { if (useCameraSpectrum == true) { myCamera.stopExp(); } MessageBox.Show("Experiment Finished! (Reached final sideband)", "Bang"); bShouldQuitThread = true; // break; // might need this?? } } } } else if (specType == "Continuous") { Frequency += stepSize; freq0.Value = Frequency; freq4.Value = Frequency; phase4.Value += phaseStep.Value; LoadDDS(freq0.Value, freq1.Value, freq2.Value, freq3.Value, freq4.Value, freq5.Value, freq6.Value, freq7.Value, amp0.Value, amp1.Value, amp2.Value, amp3.Value, amp4.Value, amp5.Value, amp6.Value, amp7.Value, phase0.Value, phase1.Value, phase2.Value, phase3.Value, phase4.Value, phase5.Value, phase6.Value, phase7.Value); SetDDSProfiles.Enabled = false; 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) { if (useCameraSpectrum == true) { myCamera.stopExp(); } 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") { for (int i = 0; i < myFile.Length; i++) { myFile[i].Flush(); myFile[i].Close(); } } this.ExperimentFinished(); if (useCameraSpectrum == true) { myCamera.stopExp(); } FPGA.ResetDevice(); }