private void button1_Click(object sender, EventArgs e) { serialPort = new SerialPort("COM12", 115200, Parity.None, 8, StopBits.One); serialPort.ReadTimeout = 1000; serialPort.Open(); try { Send("p " + (Kp.Value / 1000.0).ToString()); Send("i " + (Ki.Value / 1000.0).ToString()); Send("d " + (Kd.Value / 1000.0).ToString()); Send("s " + Setpoint.Value.ToString()); Send("l 500"); Send("g"); string s_Kp = serialPort.ReadLine(); string s_Ki = serialPort.ReadLine(); string s_Kd = serialPort.ReadLine(); string s_SetPoint = serialPort.ReadLine(); string loops = serialPort.ReadLine(); DataBlock db = new DataBlock(); db.m_channels = 2; db.m_samplesPerChannel = 500; db.Alloc(); for (int i = 0; i < 500; i++) { string str = serialPort.ReadLine(); string[] ss = str.Split(' '); int val1; int val2; int.TryParse(ss[0], out val1); int.TryParse(ss[1], out val2); if (val1 < 0) val1 = 0; db.SetVoltage(0, i, (int)(val1 * 128 / Setpoint.Value)); db.SetVoltage(1, i, (int)((val2 / 2) + 128)); } db.m_sampleRate = 25; db.m_channels = 2; db.m_dataType = DataBlock.DATA_TYPE.ANALOG; graphControl1.SetScopeData(db); } catch { graphControl1.SetScopeData(null); } serialPort.Close(); }
public WaveFile( String inFilepath ) { m_Filepath = inFilepath; m_FileInfo = new FileInfo( inFilepath ); m_FileStream = m_FileInfo.OpenRead( ); m_Riff = new RiffBlock( ); m_Fmt = new FmtBlock( ); m_Data = new DataBlock( ); }
public void SetScopeData( DataBlock db ) { lock(this) { ScopeData = db; //reset timeout waitingForTrigger = false; Invalidate(); } }
public void SetDataBlock(DataBlock db) { m_db = new DataBlock(); m_db.Copy(db); int time = 0; bool oldBit = false; for (int i = 0; i < m_db.GetChannelLength(); i++) { bool bit = m_db.GetVoltage(0,i)>10; if ( oldBit == false && bit == true ) { Process(false, time); time=0; } else if (oldBit == true && bit == false) { Process(true, time); time = 0; } else { time++; } oldBit = bit; } textBox1.Text += "histo\r\n"; List<int> keys = new List<int>(); foreach (int i in m_histo.Keys) { keys.Add(i); } keys.Sort(); foreach (int i in keys) { int v = m_histo[i]; textBox1.Text += string.Format("{0:000} {1:000} ", i, v) + "\r\n"; } }
public void SetDataBlock(DataBlock db) { m_db = db; int lastValue=0; int run = 0; int average = m_db.GetAverage( 0 ); textBox1.Text += string.Format("Average {0}\r\n", average); textBox1.Text += string.Format("Sample Rate {0}\r\n", db.m_sampleRate); textBox1.Text += string.Format("\r\nFreq from zeroes:\r\n", db.m_sampleRate); for (int i = 0; i < db.GetChannelLength(); i++) { int value = db.GetVoltage(0, i);; if (value > average && lastValue < average) { textBox1.Text += string.Format("{0} {1} {2}\r\n", i, run, 1.0/((float)run/(float)db.m_sampleRate) ); run = 0; } run++; lastValue = value; } textBox1.Text += string.Format("\r\nFreq from self correlation:\r\n", db.m_sampleRate); double old = 10; for (int i = 0; i < db.GetChannelLength() / 2; i++) { double c = Correlate(db, i); if (c > old) { double waveLength = 2* (double)i / (double)db.m_sampleRate; textBox1.Text += string.Format("{0}\r\n", 1 / waveLength); break; } old = c; } }
public override void Draw(Graphics g, DataBlock db) { DrawSelection(g); DrawHorizontalLines(g); DrawVerticalLines(g); uint bitField = db.m_channelsBitField; int index = 0; for (int ch = 0; ch < db.m_channels; ) { if (( (bitField>>index) & 1)==1) { DrawGraph(g, m_pens[index], db, ch); ch++; } index++; } }
private double Correlate(DataBlock db, int offset) { int length = db.GetChannelLength(); int res = 0; int n1 = 0; int n2 = 0; for (int i = 0; i < length - offset; i++) { int v1 = db.GetVoltage(0, i); int v2 = db.GetVoltage(0, i + offset); n1 += v1 * v1; n2 += v2 * v2; res += v1 * v2; } return (double)res / (double)(Math.Sqrt(n1) * Math.Sqrt(n2)); }
public virtual void Draw(Graphics g, DataBlock db) { }
/// <summary> /// See <see cref="IByteProvider.WriteByte" /> for more information. /// </summary> public void WriteByte(long index, byte value) { try { // Find the block affected. long blockOffset; DataBlock block = GetDataBlock(index, out blockOffset); // If the byte is already in a memory block, modify it. MemoryDataBlock memoryBlock = block as MemoryDataBlock; if (memoryBlock != null) { memoryBlock.Data[index - blockOffset] = value; return; } FileDataBlock fileBlock = (FileDataBlock)block; // If the byte changing is the first byte in the block and the previous block is a memory block, extend that. if (blockOffset == index && block.PreviousBlock != null) { MemoryDataBlock previousMemoryBlock = block.PreviousBlock as MemoryDataBlock; if (previousMemoryBlock != null) { previousMemoryBlock.AddByteToEnd(value); fileBlock.RemoveBytesFromStart(1); if (fileBlock.Length == 0) { _dataMap.Remove(fileBlock); } return; } } // If the byte changing is the last byte in the block and the next block is a memory block, extend that. if (blockOffset + fileBlock.Length - 1 == index && block.NextBlock != null) { MemoryDataBlock nextMemoryBlock = block.NextBlock as MemoryDataBlock; if (nextMemoryBlock != null) { nextMemoryBlock.AddByteToStart(value); fileBlock.RemoveBytesFromEnd(1); if (fileBlock.Length == 0) { _dataMap.Remove(fileBlock); } return; } } // Split the block into a prefix and a suffix and place a memory block in-between. FileDataBlock prefixBlock = null; if (index > blockOffset) { prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset); } FileDataBlock suffixBlock = null; if (index < blockOffset + fileBlock.Length - 1) { suffixBlock = new FileDataBlock( fileBlock.FileOffset + index - blockOffset + 1, fileBlock.Length - (index - blockOffset + 1)); } block = _dataMap.Replace(block, new MemoryDataBlock(value)); if (prefixBlock != null) { _dataMap.AddBefore(block, prefixBlock); } if (suffixBlock != null) { _dataMap.AddAfter(block, suffixBlock); } } finally { OnChanged(EventArgs.Empty); } }
public DataBlock Replace(DataBlock block, DataBlock newBlock) { AddAfterInternal(block, newBlock); RemoveInternal(block); return(newBlock); }
public void AddBefore(DataBlock block, DataBlock newBlock) { AddBeforeInternal(block, newBlock); }
void InvalidateBlock(DataBlock block) { block._map = null; block._nextBlock = null; block._previousBlock = null; }
public void AddFirst(DataBlock block) { if (_firstBlock == null) { AddBlockToEmptyMap(block); } else { AddBeforeInternal(_firstBlock, block); } }
void AddAfterInternal(DataBlock block, DataBlock newBlock) { newBlock._previousBlock = block; newBlock._nextBlock = block._nextBlock; newBlock._map = this; if (block._nextBlock != null) { block._nextBlock._previousBlock = newBlock; } block._nextBlock = newBlock; this._version++; this._count++; }
public DataBlock Replace(DataBlock block, DataBlock newBlock) { AddAfterInternal(block, newBlock); RemoveInternal(block); return newBlock; }
public void Remove(DataBlock block) { RemoveInternal(block); }
public void Clear() { DataBlock block = FirstBlock; while (block != null) { DataBlock nextBlock = block.NextBlock; InvalidateBlock(block); block = nextBlock; } _firstBlock = null; _count = 0; _version++; }
FileDataBlock GetNextFileDataBlock(DataBlock block, long dataOffset, out long nextDataOffset) { // Iterate over the remaining blocks until a file block is encountered. nextDataOffset = dataOffset + block.Length; block = block.NextBlock; while (block != null) { FileDataBlock fileBlock = block as FileDataBlock; if (fileBlock != null) { return fileBlock; } nextDataOffset += block.Length; block = block.NextBlock; } return null; }
public void SetDataBlock(DataBlock db) { }
private void DrawGraph(Graphics g, Pen p, DataBlock db, int channel) { float yy = 0; float xx = 0; int i = 0; int i0 = (int)Math.Floor( lerp(0, db.GetChannelLength(), 0, db.GetTotalTime(), MinXD) ); int i1 = (int)Math.Ceiling( lerp(0, db.GetChannelLength(), 0, db.GetTotalTime(), MaxXD) )+1; if (i1 > db.GetChannelLength()) { i1 = db.GetChannelLength(); } if (db.m_Annotations != null) { for (int an = 0; an < db.m_Annotations.Length; an++) { float time = db.GetTime(db.m_Annotations[an]); float x = ValueXToRect(time); g.DrawLine(Pens.Green, x, 0, x, ValueYToRect(5)); } } try { for (i = i0; i < i1; i++) { int rawvolt = db.GetVoltage(channel, i); float time = db.GetTime(i); float x = ValueXToRect(time); float y = ValueYToRect(rawvolt); if (i > 0) { g.DrawLine(p, xx, yy, x, y); if (showValueTicks) { g.DrawLine(p, x, y-2, x, y+2); g.DrawLine(p, x - 2, y, x + 2, y); } } yy = y; xx = x; } } catch { #if DEBUG Console.WriteLine("{0} {1} {2}", db, m_Bounds, i); #endif } //Cursor.Hide(); if (m_mouse != null) { DrawCross(g, Pens.Blue, m_mouse.X, m_mouse.Y); } { float t = (db.m_triggerPos * db.GetTotalTime()) / (float)db.GetChannelLength(); float x = ValueXToRect(t); g.DrawLine(Pens.Green, x, m_Bounds.Y, x, m_Bounds.Y + m_Bounds.Height); } Point pp = new Point(); pp.X = 0; pp.Y = 32; if (m_mouse != null) { float time = RectToValueX(m_mouse.X); float voltage = RectToValueY(m_mouse.Y); float voltageI = (voltage > 255 ? 255 : (voltage < 0 ? 0 : (int)voltage)); float voltageV = voltageI * 5 / 255; string info = string.Format("{0} ({1}, {2}/255, {3:0.###}v)", db.m_sample, ToEngineeringNotation(time), (int)voltageI, voltageV); g.DrawString(info, parent.Font, Brushes.White, pp); pp.Y += 16; info = string.Format("({0}s/div, {1}Ks/s)", ToEngineeringNotation(DivX), db.m_sampleRate / 1000); g.DrawString(info, parent.Font, Brushes.White, pp); pp.Y += 16; } if (Selected()) { if ((m_selectT0 < db.GetTotalTime()) && (m_selectT1 < db.GetTotalTime())) { g.DrawString(string.Format("({0}, {1}) - ({2}, {3})", ToEngineeringNotation(m_selectT0), db.GetVoltage(0, m_selectT0), ToEngineeringNotation(m_selectT1), db.GetVoltage(0, m_selectT1)), parent.Font, Brushes.White, pp); pp.Y += 16; g.DrawString(string.Format("ΔVoltage = {0}", db.GetVoltage(0, m_selectT1) - db.GetVoltage(0, m_selectT0)), parent.Font, Brushes.White, pp); pp.Y += 16; } string time = string.Format("ΔTime = {0}", ToEngineeringNotation(m_selectT1 - m_selectT0)); if (m_selectT1 - m_selectT0 > 0) { time += string.Format(", {0} Hz", (int)(1.0f / (m_selectT1 - m_selectT0))); } g.DrawString(time, parent.Font, Brushes.White, pp); pp.Y += 16; } }
void AddBeforeInternal(DataBlock block, DataBlock newBlock) { newBlock._nextBlock = block; newBlock._previousBlock = block._previousBlock; newBlock._map = this; if (block._previousBlock != null) { block._previousBlock._nextBlock = newBlock; } block._previousBlock = newBlock; if (_firstBlock == block) { _firstBlock = newBlock; } this._version++; this._count++; }
public void AddLast(DataBlock block) { if (_firstBlock == null) { AddBlockToEmptyMap(block); } else { AddAfterInternal(GetLastBlock(), block); } }
void AddBlockToEmptyMap(DataBlock block) { block._map = this; block._nextBlock = null; block._previousBlock = null; _firstBlock = block; _version++; _count++; }
void RemoveInternal(DataBlock block) { DataBlock previousBlock = block._previousBlock; DataBlock nextBlock = block._nextBlock; if (previousBlock != null) { previousBlock._nextBlock = nextBlock; } if (nextBlock != null) { nextBlock._previousBlock = previousBlock; } if (_firstBlock == block) { _firstBlock = nextBlock; } InvalidateBlock(block); _count--; _version++; }
public void AddAfter(DataBlock block, DataBlock newBlock) { AddAfterInternal(block, newBlock); }
internal Enumerator(DataMap map) { _map = map; _version = map._version; _current = null; _index = -1; }
public override void Draw(Graphics g, DataBlock db) { Rectangle r = new Rectangle(); r = m_Bounds; if (f == null) { f = new FFT(1024); } if (db.m_result != DataBlock.RESULT.OK) { pp.X = 0; pp.Y = 0; g.DrawString(string.Format("Waiting for a chunk of data to analyze"), parent.Font, Brushes.White, pp); return; } if (db.GetChannelLength() < f.GetNumOfSamples()) { pp.X = 0; pp.Y = 0; g.DrawString(string.Format("FFT needs at least 1024 samples to work, got only {0}, try increasing the measurement time",db.GetChannelLength()) , parent.Font, Brushes.White, pp); return; } for (int i = 0; i < f.GetNumOfSamples(); i++) { f.x[i] = db.GetVoltage(0, i); f.y[i] = 0; } f.DoFFT(0); int maxFreq = db.m_sampleRate / 2; int minFreq = 0; //margin at the bottom r.Height -= 20; r.Width = 1024; r.X -= (int)MinXD; if (drawSlidingFFT) { DrawSlidingFFT(g, r, db); r.Y -= 256; DrawFFTBars(g, r); r.Y += 256; } else { DrawFFTBars(g, r); } int freqStep; for (freqStep = 500; freqStep < maxFreq; freqStep += 500) { int ft = lerp(0, f.GetNumOfSamples() / 2, minFreq, maxFreq, freqStep); if (ft > 30) break; } //draw legend for (int i = 0; i < (int)maxFreq; i += freqStep) { int x = lerp(0, 512, minFreq, maxFreq, i); pp.X = r.X + 2 * x; pp.Y = r.Bottom; g.DrawLine(Pens.Gray, pp.X, 0, pp.X, pp.Y); g.DrawString(string.Format("{0}", i), parent.Font, Brushes.White, pp); } if (m_mouse != null) { DrawCross(g, Pens.Blue, m_mouse.X, m_mouse.Y); pp.X = r.X ; pp.Y = r.Y + 40; g.DrawString(string.Format("Freq: {0} Hz", ((m_mouse.X / 2) * maxFreq) / f.GetNumOfSamples() / 2), parent.Font, Brushes.White, pp); } }
/// <summary> /// See <see cref="IByteProvider.InsertBytes" /> for more information. /// </summary> public void InsertBytes(long index, byte[] bs) { try { // Find the block affected. long blockOffset; DataBlock block = GetDataBlock(index, out blockOffset); // If the insertion point is in a memory block, just insert it. MemoryDataBlock memoryBlock = block as MemoryDataBlock; if (memoryBlock != null) { memoryBlock.InsertBytes(index - blockOffset, bs); return; } FileDataBlock fileBlock = (FileDataBlock)block; // If the insertion point is at the start of a file block, and the previous block is a memory block, append it to that block. if (blockOffset == index && block.PreviousBlock != null) { MemoryDataBlock previousMemoryBlock = block.PreviousBlock as MemoryDataBlock; if (previousMemoryBlock != null) { previousMemoryBlock.InsertBytes(previousMemoryBlock.Length, bs); return; } } // Split the block into a prefix and a suffix and place a memory block in-between. FileDataBlock prefixBlock = null; if (index > blockOffset) { prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset); } FileDataBlock suffixBlock = null; if (index < blockOffset + fileBlock.Length) { suffixBlock = new FileDataBlock( fileBlock.FileOffset + index - blockOffset, fileBlock.Length - (index - blockOffset)); } block = _dataMap.Replace(block, new MemoryDataBlock(bs)); if (prefixBlock != null) { _dataMap.AddBefore(block, prefixBlock); } if (suffixBlock != null) { _dataMap.AddAfter(block, suffixBlock); } } finally { _totalLength += bs.Length; OnLengthChanged(EventArgs.Empty); OnChanged(EventArgs.Empty); } }
public bool MoveNext() { if (this._version != _map._version) { throw new InvalidOperationException("Collection was modified after the enumerator was instantiated."); } if (_index >= _map.Count) { return false; } if (++_index == 0) { _current = _map.FirstBlock; } else { _current = _current.NextBlock; } return (_index < _map.Count); }
public void DrawSlidingFFT(Graphics g, Rectangle r, DataBlock db) { if (bmp == null) { bmp = new Bitmap(512, 256); shade = new Color[256]; for (int i = 0; i < 256; i++) { shade[i] = System.Drawing.Color.FromArgb(i, 0, 255 - i); } } for (int i = 0; i < 512; i++) { double power = f.Power(i) / 64; if (power > 255) power = 255; byte p = (byte)power; bmp.SetPixel(i, db.m_sample & 0xff, shade[p]); } int yy = r.Y + r.Height - 256; g.DrawImage(bmp, r.X, yy, 1024, 256); int y = yy + (db.m_sample + 1 & 0xff); g.DrawLine(Pens.Red, r.X, y, 1024, y); }
void IEnumerator.Reset() { if (this._version != this._map._version) { throw new InvalidOperationException("Collection was modified after the enumerator was instantiated."); } this._index = -1; this._current = null; }
public void SetDataBlock(DataBlock db) { m_db = new DataBlock(); m_db.Copy(db); //List<int> PeakOffsets = GetMinMax(m_db); List<int> PeakOffsets = new List<int>(); List<int> deltas = new List<int>(); PeakFinder.InitLoopThoughWave(db); int t = 0; for (; ; ) { int v = PeakFinder.LoopThoughWave(); if (v == 255) break; t = t + v; PeakOffsets.Add(t); deltas.Add(v); } db.m_Annotations = PeakOffsets.ToArray(); int offset = 0; for (; offset < deltas.Count; offset++) { if (deltas[offset] != 0) break; } if (offset >= 0) { //output.Text += string.Format("Clocked signal found: {0}\r\n", offset); string bitstream = ExtractBits(deltas); output.Text += bitstream + "\r\n"; //output.Text += string.Format("Errors as FSK: {0}\r\n", DetectFSK(bitstream)); output.Text += "\r\nText:\r\n"; output.Text += Decode4BitsToString(bitstream, false) + "\r\n"; //output.Text += string.Format("Alternating: \r\n"); //output.Text += GetBitStreamFromAlternating(bitstream) + "\r\n"; } else { output.Text += string.Format("Clocked signal not found, dumping timing values\r\n"); int last = 0; foreach (int i in PeakOffsets) { output.Text += string.Format("{0:0000}: {1}", i, i-last) + "\r\n"; last = i; } } }