public void UploadWaveformData(double[] voltageArray, double sampleRate, double DCOffset, double phase, string memoryLocation) { double lowLevel = voltageArray.Min(); double highLevel = voltageArray.Max(); using (StreamWriter streamWriter = new StreamWriter(this.filePath, true)) { streamWriter.WriteLine("UploadWaveformData(double[] voltageArray, " + memoryLocation + ", " + sampleRate + ", " + lowLevel + ", " + highLevel + ", " + DCOffset + ", " + phase + ", " + memoryLocation + ") " // can't forget that space + string.Format("{0:yyyy-MM-dd_hh-mm-ss-fff}", DateTime.Now)); } WaveformParam temp = new WaveformParam { amplitude = highLevel - lowLevel, DCOffset = DCOffset, frequency = sampleRate / voltageArray.Length, lowLevel = lowLevel, highLevel = highLevel, phase = phase, waveformType = WaveformType.ARB // uploaded waveforms are always of the arbitrary type }; string filePath = directoryPath + "\\SFG_DATA\\" + memoryLocation + ".bin"; if (File.Exists(filePath)) // if the waveform already exists { File.Delete(filePath); // delete it } Stream ms = File.OpenWrite(filePath); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, temp); // serialize the object, and then write it to the file. ms.Flush(); ms.Close(); ms.Dispose(); // then clean up }
/// <summary> /// The two argument constructor. Creates a StubFunctionGenerator object with log files saved in the given directory path /// and with timestamped log files, if that option is set /// </summary> /// <param name="directoryPath">The path to the directory to save log files to</param> /// <param name="timeStampedLogFiles">Whether or not to save log files with timestamps, or overwrite previous ones</param> public StubFunctionGenerator(string directoryPath, bool timeStampedLogFiles, int numMemoryLocations, byte numChannels) { this.numMemoryLocations = numMemoryLocations; this.numChannels = numChannels; channelWaveData = new WaveformParam[numChannels]; // init with a length of however many channels there are. string filePath; this.directoryPath = directoryPath; // set the directory to write log files to. if (timeStampedLogFiles) // create a file with a timestamp in the name { string timeStamp = string.Format("{0:yyyy-MM-dd_hh-mm-ss-fff}", DateTime.Now); // get the current time and // format it into a nice string filePath = directoryPath + "\\log-" + timeStamp + ".txt"; // then append it to the directory path and // make it a .txt file. } else // just use log.txt and overwrite what was there previously { // No multithread lock here, so yeah, clients don't mess with "log.txt" I guess filePath = directoryPath + "\\log.txt"; } if (File.Exists(filePath)) // if it already exists (with the timestamp ones this should be impossible) { File.Delete(filePath); // delete it } //File.Create(filePath); // then init the filestream after creating the new file using (StreamWriter streamWriter = new StreamWriter(filePath, true)) { streamWriter.WriteLine("StubFunctionGenerator log file, generated at " + string.Format("{0:yyyy-MM-dd_hh-mm-ss-fff}", DateTime.Now)); } validMemLocations = new List <string>(); // create the valid memory locations for (int i = 1; i <= numMemoryLocations; i++) // WAVE1-WAVE(whatever), no zero based indexing { validMemLocations.Add("WAVE" + i); } for (int i = 0; i < numChannels; i++) { channelWaveData[i] = new WaveformParam(); } this.filePath = filePath; string dataDirectory = directoryPath + "\\SFG_DATA"; // set directory path Directory.CreateDirectory(dataDirectory); // creates the directory if it doesn't exist, and does nothing otherwise. }
public void LoadWaveform(string name, int channel) { using (StreamWriter streamWriter = new StreamWriter(this.filePath, true)) { streamWriter.WriteLine("LoadWaveform(" + name + ", " + channel + ") " + string.Format("{0:yyyy-MM-dd_hh-mm-ss-fff}", DateTime.Now)); } string filePath = directoryPath + "\\SFG_DATA\\" + name + ".bin"; // the path to where the memory location's // bin file is stored. // now we deserialize the binary-encoded object BinaryFormatter formatter = new BinaryFormatter(); FileStream fs = File.Open(filePath, FileMode.Open); object obj = formatter.Deserialize(fs); // it returns an object WaveformParam temp = (WaveformParam)obj; // we need to cast it so we can access the fields fs.Flush(); fs.Close(); fs.Dispose(); // clean up channelWaveData[channel - 1] = temp; // just set the waveform param entry for the selected channel to // the one we just deserialized from the file. }