static public StreamSample[] ReadVoltageStream() { StreamSample[] samples = new StreamSample[12]; lock (UsbLockObj) { if (Msp430 != null) { // Request stream of 48 bytes (12 samples) if (USBSendData(new byte[] { 0x04, 0x00 })) { byte[] wordBuf; if (USBRecvData(out wordBuf)) { for (int i = 0; i < wordBuf.Length; i += 4) { StreamSample sample = new StreamSample(); sample.SequenceId = wordBuf[i + 0]; if ((wordBuf[i + 1] & 0x80) > 0) { // Sign extend wordBuf[i + 0] = 0xff; } else { wordBuf[i + 0] = 0; } Int32 data = (wordBuf[i + 0] << 24) + (wordBuf[i + 1] << 16) + (wordBuf[i + 2] << 8) + wordBuf[i + 3]; sample.Value = data; samples[i >> 2] = sample; } return(samples); } } } } IsConnected = false; return(null); }
/// <summary> /// This thread is spun up when logging is turned on. If there's a problem during logging /// of any kind (file access, bad communication with hardware, etc) then this thread will exit. /// </summary> private void LoggingThreadBinary() { float dt; int sample = 0; byte LastSequence = 0; bool firstRead = true; Debug.WriteLine("Binary logging thread started"); FileStream fs = null; BinaryWriter sw = null; try { dt = (float)(1.0 / GetSampleRate()); fs = new FileStream(LogFile, FileMode.Create, FileAccess.Write); sw = new BinaryWriter(fs); // Barker sw.Write(0xCAFE8224); sw.Write(GetSampleRate()); while (LoggingEnabled) { StreamSample[] buffer = new StreamSample[0]; try { while ((Hardware.GetFifoDepth() < 12) && (LoggingEnabled)) { Thread.Sleep(5); } if (LoggingEnabled) { buffer = Hardware.ReadVoltageStream(); } } catch { break; } // Sync the sequence ID on the first read if (firstRead) { firstRead = false; LastSequence = (byte)(buffer[0].SequenceId - (byte)1); } // If nothing read, then bail if (buffer.Length == 0) { break; } // If we're missing a block of data, write null samples to indicate such. while ((byte)(LastSequence + 1) != buffer[0].SequenceId) { //sw.Write(sample++ * dt); sw.Write(float.NaN); ++LastSequence; } for (int i = 0; i < buffer.Length; i++) { bool ovf = false; //sw.WriteLine("-,{0:0.000},{1:N6}", sample++ * dt, ConvertCountsToVoltage(buffer[i].Value, ref ovf)); //sw.Write(sample++ * dt); sw.Write((float)ConvertCountsToVoltage(buffer[i].Value, ref ovf)); LastSequence = buffer[i].SequenceId; } } } catch (Exception ex) { MessageBox.Show("Binary logging has stopped: " + ex.Message); } if (sw != null) { sw.Flush(); sw.Close(); } if (fs != null) { fs.Close(); } LoggingEnabled = false; Debug.WriteLine("Binary logging thread exited. {0} samples written.", sample); LoggingDone(); }
private void LoggingThreadText() { float dt; int sample = 0; byte LastSequence = 0; bool firstRead = true; Debug.WriteLine("Text logging thread started"); StreamWriter sw = null; try { dt = (float)(1.0 / GetSampleRate()); sw = new StreamWriter(LogFile); // Barker sw.WriteLine("Logging file Created on " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString()); sw.WriteLine("Sample Rate {0} sps", GetSampleRate()); sw.WriteLine("Sample Time (seconds), Value"); while (LoggingEnabled) { StreamSample[] buffer = new StreamSample[0]; try { while ((Hardware.GetFifoDepth() < 12) && (LoggingEnabled)) { Thread.Sleep(5); } if (LoggingEnabled) { buffer = Hardware.ReadVoltageStream(); } } catch { break; } // Sync the sequence ID on the first read if (firstRead) { firstRead = false; LastSequence = (byte)(buffer[0].SequenceId - (byte)1); } // If nothing read, then bail if (buffer.Length == 0) { break; } // If we're missing a block of data, write null samples to indicate such. while ((byte)(LastSequence + 1) != buffer[0].SequenceId) { //sw.Write(sample++ * dt); sw.WriteLine("{0}, {1}", sample++ *dt, "MISSED DATA"); ++LastSequence; } for (int i = 0; i < buffer.Length; i++) { bool ovf = false; sw.WriteLine("{0:0.000},{1:N6}", sample++ *dt, ConvertCountsToVoltage(buffer[i].Value, ref ovf)); LastSequence = buffer[i].SequenceId; } } } catch (Exception ex) { MessageBox.Show("Text logging has stopped: " + ex.Message); } if (sw != null) { sw.Flush(); sw.Close(); } LoggingEnabled = false; Debug.WriteLine("Text logging thread exited. {0} samples written.", sample); LoggingDone(); }