123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- using System;
- using System.Collections.Generic;
- using System.Drawing.Imaging;
- using System.ComponentModel;
- using System.Data;
- using System.IO;
- using System.Text.Json;
- using System.Text.Encodings.Web;
- using System.Text.Json.Serialization;
- using System.Text.Unicode;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Uestc.Auto6.Dso.ComModel;
- using Uestc.Auto6.Dso.Hardware.Calibration.Data.Base;
- namespace Uestc.Auto6.Dso.Hardware.Calibration.Tool
- {
- public partial class WaveViewer : UserControl
- {
- public WaveViewer()
- {
- InitializeComponent();
- Wave_ViewInit();
- }
- public IInstrumentSession CurrInstrument = null;
- public WaveDisplayParam[] DisplayParam_Channel
- {
- get => displayParam_Channel;
- }
- public WaveDisplayParam[,] DisplayParam_Adc
- {
- get => displayParam_Adc;
- }
- private string jsonFileName
- {
- get => Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".waveparam";
- }
- public void Run(bool bRun)
- {
- if (bRun)
- timer1.Start();
- else
- timer1.Stop();
- }
- public void SaveSetting()
- {
- List<WaveDisplayParam> waveDisplayParamList = new List<WaveDisplayParam>();
- foreach (var param in DisplayParam_Channel)
- waveDisplayParamList.Add(param);
- for (int adcIndex = 0; adcIndex < Constants.ADC_NUM; adcIndex++)
- {
- for (int coreIndex = 0; coreIndex < ServerDomainConstants.PerAdcCoreCount; coreIndex++)
- waveDisplayParamList.Add(DisplayParam_Adc[adcIndex, coreIndex]);
- }
- if (File.Exists(jsonFileName))
- File.Delete(jsonFileName);
- JsonSerializerOptions options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) };
- File.WriteAllText(jsonFileName, JsonSerializer.Serialize<List<WaveDisplayParam>>(waveDisplayParamList, options), Encoding.UTF8);
- }
- WaveDisplayParam[] displayParam_Channel;
- WaveDisplayParam[,] displayParam_Adc;
- void Wave_ViewInit()
- {
- List<WaveDisplayParam> waveDisplayParamList = new List<WaveDisplayParam>();
- if (File.Exists(jsonFileName))
- waveDisplayParamList = JsonSerializer.Deserialize<List<WaveDisplayParam>>(File.ReadAllText(jsonFileName, Encoding.UTF8));
- int needAddCount = CaliConstants.Fixed_MaxPhysicsChannelCount + CaliConstants.Fixed_PerChannelMergeAdcMaxCount * ServerDomainConstants.PerAdcCoreCount - waveDisplayParamList.Count;
- while (needAddCount > 0)
- {
- waveDisplayParamList.Add(new WaveDisplayParam());
- needAddCount--;
- }
- dataGridViewWaveDataView_Channel.RowCount = CaliConstants.Fixed_MaxPhysicsChannelCount;
- displayParam_Channel = new WaveDisplayParam[CaliConstants.Fixed_MaxPhysicsChannelCount];
- int displayParamIndex = 0;
- for (int channelIndex = 0; channelIndex < CaliConstants.Fixed_MaxPhysicsChannelCount; channelIndex++)
- {
- displayParam_Channel[channelIndex] = waveDisplayParamList[displayParamIndex++];
- dataGridViewWaveDataView_Channel.Rows[channelIndex].Cells[0].Value = $"CH{channelIndex + 1}";
- dataGridViewWaveDataView_Channel.Rows[channelIndex].Cells[1].Value = displayParam_Channel[channelIndex].bDisplay;
- dataGridViewWaveDataView_Channel.Rows[channelIndex].Cells[2].Style.BackColor = displayParam_Channel[channelIndex].GetColor();
- }
- dataGridViewWaveDataView_Adc.RowCount = CaliConstants.Fixed_PerChannelMergeAdcMaxCount * ServerDomainConstants.PerAdcCoreCount;
- int rowIndex = 0;
- displayParam_Adc = new WaveDisplayParam[CaliConstants.Fixed_PerChannelMergeAdcMaxCount, ServerDomainConstants.PerAdcCoreCount];
- for (int adcIndex = 0; adcIndex < CaliConstants.Fixed_PerChannelMergeAdcMaxCount; adcIndex++)
- {
- for (int coreIndex = 0; coreIndex < ServerDomainConstants.PerAdcCoreCount; coreIndex++)
- {
- displayParam_Adc[adcIndex, coreIndex] = waveDisplayParamList[displayParamIndex++];
- dataGridViewWaveDataView_Adc.Rows[rowIndex].Cells[0].Value = $"Adc{adcIndex + 1}-Core[{coreIndex + 1}]";
- dataGridViewWaveDataView_Adc.Rows[rowIndex].Cells[1].Value = displayParam_Adc[adcIndex, coreIndex].bDisplay;
- dataGridViewWaveDataView_Adc.Rows[rowIndex].Cells[2].Style.BackColor = displayParam_Adc[adcIndex, coreIndex].GetColor();
- rowIndex++;
- }
- }
- CalcNeedDisplayCount();
- comboBoxAdcDotLineMode.SelectedIndex = 0;
- buttonADCRunStop.Tag = "stop";
- timer1.Start();
- }
- private void dataGridViewWaveDataView_Channel_CurrentCellDirtyStateChanged(object sender, EventArgs e)
- {
- if (dataGridViewWaveDataView_Channel.CurrentCell is DataGridViewCheckBoxCell)
- {
- for (int channelIndex = 0; channelIndex < CaliConstants.Fixed_MaxPhysicsChannelCount; channelIndex++)
- {
- DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dataGridViewWaveDataView_Channel.Rows[channelIndex].Cells[1];
- displayParam_Channel[channelIndex].bDisplay = (bool)cell.FormattedValue;
- CalcNeedDisplayCount();
- }
- dataGridViewWaveDataView_Channel.CurrentCell = dataGridViewWaveDataView_Channel.Rows[dataGridViewWaveDataView_Channel.CurrentCell.RowIndex].Cells[2];
- }
- }
- int channelNeedDisplayCount = 0;
- int adcNeedDisplayCount = 0;
- private void CalcNeedDisplayCount()
- {
- bool bChanged = false;
- int _channelNeedDisplayCount = displayParam_Channel.Sum<WaveDisplayParam>((o) => o.bDisplay ? 1 : 0);
- if (_channelNeedDisplayCount != channelNeedDisplayCount)
- {
- bChanged = true;
- channelNeedDisplayCount = _channelNeedDisplayCount;
- }
- int _adcNeedDisplayCount = 0;
- for (int adcIndex = 0; adcIndex < CaliConstants.Fixed_PerChannelMergeAdcMaxCount; adcIndex++)
- {
- for (int coreIndex = 0; coreIndex < ServerDomainConstants.PerAdcCoreCount; coreIndex++)
- {
- _adcNeedDisplayCount += displayParam_Adc[adcIndex, coreIndex].bDisplay ? 1 : 0;
- }
- }
- if (_adcNeedDisplayCount != adcNeedDisplayCount)
- {
- bChanged = true;
- adcNeedDisplayCount = _adcNeedDisplayCount;
- }
- if (bChanged && bAtStopMode)
- RedrawAtStopMode();
- }
- private void dataGridViewWaveDataView_Adc_CurrentCellDirtyStateChanged(object sender, EventArgs e)
- {
- if (dataGridViewWaveDataView_Adc.CurrentCell is DataGridViewCheckBoxCell)
- {
- for (int adcIndex = 0; adcIndex < CaliConstants.Fixed_PerChannelMergeAdcMaxCount; adcIndex++)
- {
- for (int coreIndex = 0; coreIndex < ServerDomainConstants.PerAdcCoreCount; coreIndex++)
- {
- DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dataGridViewWaveDataView_Adc.Rows[adcIndex * ServerDomainConstants.PerAdcCoreCount + coreIndex].Cells[1];
- displayParam_Adc[adcIndex, coreIndex].bDisplay = (bool)cell.FormattedValue;
- CalcNeedDisplayCount();
- }
- }
- dataGridViewWaveDataView_Adc.CurrentCell = dataGridViewWaveDataView_Adc.Rows[dataGridViewWaveDataView_Adc.CurrentCell.RowIndex].Cells[2];
- }
- }
- int lastNeedDisplayCount = 0;
- List<ushort[]> lastAllChannelData = null;
- List<ushort[]> lastAllAdcCoreData = null;
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (bAtStopMode || CurrInstrument == null)
- return;
- timer1.Stop();
- int totalSelectCount = channelNeedDisplayCount + adcNeedDisplayCount;
- if (totalSelectCount > 0)
- {
- lastNeedDisplayCount = totalSelectCount;
- Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format24bppRgb);
- Graphics g = Graphics.FromImage(bitmap);
- g.Clear(Color.Black);
- if (channelNeedDisplayCount > 0)
- {
- List<ushort[]> allChannelData = InstrumentInteract.Factory_WaveData_Channel(CurrInstrument);
- DrawChannelWaveMain(allChannelData, bitmap);
- lastAllChannelData = allChannelData;
- }
- if (adcNeedDisplayCount > 0)
- {
- List<ushort[]> allAdcCoreData = InstrumentInteract.Factory_WaveData_Adc(CurrInstrument);
- DrawAdcCoreWaveMain(allAdcCoreData, bitmap);
- lastAllAdcCoreData = allAdcCoreData;
- }
- pictureBox1.Image = bitmap;
- }
- else if (lastNeedDisplayCount != totalSelectCount)
- {
- pictureBox1.Image = null;
- lastNeedDisplayCount = 0;
- }
- timer1.Start();
- }
- private int AdcResolution = (int)(Math.Pow(2, ServerDomainConstants.AdcBits));
- #region DrawWave
- private void DrawChannelWaveMain(List<ushort[]> allChannelData, Bitmap bitmap)
- {
- if (allChannelData == null)
- return;
- Graphics g = Graphics.FromImage(bitmap);
- g.TranslateTransform(0, pictureBox1.Height / 2);
- Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
- bool bTopNeedDisplay = false;
- for (int channelIndex = 0; channelIndex < CaliConstants.Fixed_MaxPhysicsChannelCount; channelIndex++)
- {
- if (displayParam_Channel[channelIndex].bDisplay)
- {
- if (channelIndex != currTopRowIndexofChannel)
- DrawChannelWave(allChannelData, bitmap, rect, channelIndex);
- else
- bTopNeedDisplay = true;
- }
- }
- if (bTopNeedDisplay)
- DrawChannelWave(allChannelData, bitmap, rect, currTopRowIndexofChannel);
- }
- private void DrawAdcCoreWaveMain(List<ushort[]> allAdcCoreData, Bitmap bitmap)
- {
- if (allAdcCoreData == null)
- return;
- Graphics g = Graphics.FromImage(bitmap);
- g.TranslateTransform(0, pictureBox1.Height / 2);
- Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
- int topAdcIndex = currTopRowIndexofAdc / ServerDomainConstants.PerAdcCoreCount;
- int topCoreIndex = currTopRowIndexofAdc % ServerDomainConstants.PerAdcCoreCount;
- bool bTopAdcNeedDisplay = false;
- bool bDisplayByDotMode = comboBoxAdcDotLineMode.SelectedIndex != 0;
- for (int adcIndex = 0; adcIndex < CaliConstants.Fixed_PerChannelMergeAdcMaxCount; adcIndex++)
- {
- for (int coreIndex = 0; coreIndex < ServerDomainConstants.PerAdcCoreCount; coreIndex++)
- {
- if (displayParam_Adc[adcIndex, coreIndex].bDisplay)
- {
- if (topAdcIndex == adcIndex && topCoreIndex == coreIndex)
- bTopAdcNeedDisplay = true;
- else
- {
- if (bDisplayByDotMode)
- DrawAdcDataByDotMode(allAdcCoreData, bitmap, rect, adcIndex, coreIndex);
- else
- DrawAdcDataByLineMode(allAdcCoreData, g, rect, adcIndex, coreIndex);
- }
- }
- }
- }
- if (bTopAdcNeedDisplay)
- {
- if (bDisplayByDotMode)
- DrawAdcDataByDotMode(allAdcCoreData, bitmap, rect, topAdcIndex, topCoreIndex);
- else
- DrawAdcDataByLineMode(allAdcCoreData, g, rect, topAdcIndex, topCoreIndex);
- }
- }
- private void DrawChannelWave(List<ushort[]> dataList, Bitmap bitmap, Rectangle rect, int channelID)
- {
- Graphics g = Graphics.FromImage(bitmap);
- g.TranslateTransform(0, pictureBox1.Height / 2);
- Color waveColor = displayParam_Channel[channelID].GetColor();
- int extractNum = (int)numericUpDownWaveChannelExtractNum.Value;
- short[] currData = Array.ConvertAll<ushort, short>(dataList[channelID], (y) => (short)((AdcResolution / 2 - y) * rect.Height / AdcResolution));
- if (extractNum <= 1)
- {
- List<Point> points = new List<Point>();
- Pen pen = new Pen(waveColor);
- int x = 0;
- foreach (short y in currData)
- {
- points.Add(new Point(x, y));
- x++;
- if (x >= rect.Width)
- break;
- }
- g.DrawLines(pen, points.ToArray());
- }
- else
- {
- Brush brush = new SolidBrush(waveColor);
- int dataIndex = 0;
- int x = 0;
- short max = short.MinValue;
- short min = short.MaxValue;
- for (; ; )
- {
- max = short.MinValue;
- min = short.MaxValue;
- for (int i = 0; i < extractNum; i++)
- {
- if (currData[dataIndex + i] < min)
- min = currData[dataIndex + i];
- if (currData[dataIndex + i] > max)
- max = currData[dataIndex + i];
- }
- dataIndex += extractNum;
- g.FillRectangle(brush, x, min, 1, max - min);
- if (dataIndex > currData.Length - extractNum)
- break;
- x++;
- if (x >= rect.Width)
- break;
- }
- }
- }
- private void DrawAdcDataByDotMode(List<ushort[]> dataList, Bitmap bitmap, Rectangle rect, int adcIndex, int coreIndex)
- {
- Color color = displayParam_Adc[adcIndex, coreIndex].GetColor();
- ushort[] currData_source = dataList[adcIndex * ServerDomainConstants.PerAdcCoreCount + coreIndex];
- List<Point> points = new List<Point>();
- int height = rect.Height;
- short[] currData = Array.ConvertAll<ushort, short>(currData_source, (y) => (short)((AdcResolution / 2 - y) * height / AdcResolution - 1));
- int x = 0;
- int step = (int)numericUpDownAdcZoomCount.Value;
- Pen pen = new Pen(color);
- foreach (short y in currData)
- {
- bitmap.SetPixel(x, y + height / 2, color);
- x += step;
- if (x >= bitmap.Width)
- break;
- }
- }
- private void DrawAdcDataByLineMode(List<ushort[]> dataList, Graphics g, Rectangle rect, int adcIndex, int coreIndex)
- {
- Color color = displayParam_Adc[adcIndex, coreIndex].GetColor();
- ushort[] currData_source = dataList[adcIndex * ServerDomainConstants.PerAdcCoreCount + coreIndex];
- List<Point> points = new List<Point>();
- int height = rect.Height;
- short[] currData = Array.ConvertAll<ushort, short>(currData_source, (y) => (short)((AdcResolution / 2 - y) * height / AdcResolution - 1));
- int x = 0;
- int step = (int)numericUpDownAdcZoomCount.Value;
- foreach (short y in currData)
- {
- points.Add(new Point(x, y));
- x += step;
- if (x >= rect.Width)
- break;
- }
- g.DrawLines(new Pen(color), points.ToArray());
- }
- private void RedrawAtStopMode()
- {
- if (!bAtStopMode)
- return;
- if (channelNeedDisplayCount + adcNeedDisplayCount == 0)
- return;
- if (pictureBox1.Width == 0 || pictureBox1.Height == 0)
- return;
- Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format24bppRgb);
- using (Graphics g = Graphics.FromImage(bitmap))
- {
- g.Clear(Color.Black);
- DrawChannelWaveMain(lastAllChannelData, bitmap);
- DrawAdcCoreWaveMain(lastAllAdcCoreData, bitmap);
- }
- pictureBox1.Image = bitmap;
- }
- #endregion
- public class WaveDisplayParam
- {
- [JsonPropertyName("color")]
- public int IntColor
- {
- get;
- set;
- } = Color.White.ToArgb();
- public Color GetColor() => Color.FromArgb(IntColor);
- [JsonPropertyName("display")]
- public bool bDisplay
- {
- get; set;
- } = false;
- }
- private void tabControl2_SelectedIndexChanged(object sender, EventArgs e)
- {
- }
- private void onAdcSelectCtrl(object sender, EventArgs e)
- {
- bool bSelectAll = bool.Parse((sender as Button).Tag.ToString());
- for (int adcIndex = 0; adcIndex < CaliConstants.Fixed_PerChannelMergeAdcMaxCount; adcIndex++)
- {
- for (int coreIndex = 0; coreIndex < ServerDomainConstants.PerAdcCoreCount; coreIndex++)
- {
- displayParam_Adc[adcIndex, coreIndex].bDisplay = bSelectAll;
- int rowIndex = adcIndex * ServerDomainConstants.PerAdcCoreCount + coreIndex;
- dataGridViewWaveDataView_Adc.Rows[rowIndex].Cells[1].Value = displayParam_Adc[adcIndex, coreIndex].bDisplay;
- }
- }
- CalcNeedDisplayCount();
- }
- private void onChannelSelectCtrl(object sender, EventArgs e)
- {
- bool bSelectAll = bool.Parse((sender as Button).Tag.ToString());
- for (int channelIndex = 0; channelIndex < CaliConstants.Fixed_MaxPhysicsChannelCount; channelIndex++)
- {
- displayParam_Channel[channelIndex].bDisplay = bSelectAll;
- dataGridViewWaveDataView_Channel.Rows[channelIndex].Cells[1].Value = displayParam_Channel[channelIndex].bDisplay;
- }
- CalcNeedDisplayCount();
- }
- private void dataGridViewWaveDataView_Channel_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
- {
- if (e.ColumnIndex != 2)
- return;
- if (colorDialog1.ShowDialog() == DialogResult.OK)
- {
- displayParam_Channel[e.RowIndex].IntColor = colorDialog1.Color.ToArgb();
- dataGridViewWaveDataView_Channel.Rows[e.RowIndex].Cells[2].Style.BackColor = displayParam_Channel[e.RowIndex].GetColor();
- dataGridViewWaveDataView_Channel.CurrentCell = dataGridViewWaveDataView_Channel.Rows[dataGridViewWaveDataView_Channel.CurrentCell.RowIndex].Cells[0];
- }
- }
- private void dataGridViewWaveDataView_Adc_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
- {
- if (e.ColumnIndex != 2)
- return;
- if (colorDialog1.ShowDialog() == DialogResult.OK)
- {
- int adcIndex = e.RowIndex / ServerDomainConstants.PerAdcCoreCount;
- int coreIndex = e.RowIndex % ServerDomainConstants.PerAdcCoreCount;
- displayParam_Adc[adcIndex, coreIndex].IntColor = colorDialog1.Color.ToArgb();
- dataGridViewWaveDataView_Adc.Rows[e.RowIndex].Cells[2].Style.BackColor = displayParam_Adc[adcIndex, coreIndex].GetColor();
- dataGridViewWaveDataView_Adc.CurrentCell = dataGridViewWaveDataView_Adc.Rows[dataGridViewWaveDataView_Adc.CurrentCell.RowIndex].Cells[0];
- }
- }
- private int currTopRowIndexofChannel = 0;
- private int currTopRowIndexofAdc = 0;
- private void dataGridViewWaveDataView_Adc_RowEnter(object sender, DataGridViewCellEventArgs e)
- {
- currTopRowIndexofAdc = e.RowIndex;
- RedrawAtStopMode();
- }
- private bool bAtStopMode => buttonADCRunStop.Tag.ToString() == "stop";
- private void dataGridViewWaveDataView_Channel_RowEnter(object sender, DataGridViewCellEventArgs e)
- {
- currTopRowIndexofChannel = e.RowIndex;
- RedrawAtStopMode();
- }
- private void buttonADCRunStop_Click(object sender, EventArgs e)
- {
- if (buttonADCRunStop.Tag.ToString() == "stop")
- {
- buttonADCRunStop.Text = "Running";
- buttonADCRunStop.Tag = "run";
- buttonADCRunStop.BackColor = Color.Green;
- }
- else
- {
- buttonADCRunStop.Text = "Stopped";
- buttonADCRunStop.Tag = "stop";
- buttonADCRunStop.BackColor = Color.Tomato;
- }
- }
- private void ReadrawAtStopMode(object sender, EventArgs e)
- {
- RedrawAtStopMode();
- }
- private void buttonSaveChannelData_Click(object sender, EventArgs e)
- {
- if (CurrInstrument == null)
- {
- MessageBox.Show("请先连接目标示波器!");
- return;
- }
- int selectCount = 0;
- for (int channelIndex = 0; channelIndex < CaliConstants.Fixed_MaxPhysicsChannelCount; channelIndex++)
- {
- if (displayParam_Channel[channelIndex].bDisplay)
- selectCount++;
- }
- if (selectCount==0)
- {
- MessageBox.Show("请先钩选需要保存的通道!");
- return;
- }
- if (this.folderBrowserDialog1.ShowDialog() != DialogResult.OK)
- return;
- string nowTimeStr = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
- string path = this.folderBrowserDialog1.SelectedPath;
- if (path[path.Length - 1] != '\\')
- path = path + '\\';
- List<ushort[]> allChannelData = InstrumentInteract.Factory_WaveData_Channel(CurrInstrument);
- for (int channelIndex = 0; channelIndex < CaliConstants.Fixed_MaxPhysicsChannelCount; channelIndex++)
- {
- if (displayParam_Channel[channelIndex].bDisplay)
- {
- using (StreamWriter sw = new StreamWriter($"{path}CH{channelIndex + 1}_{nowTimeStr}.txt", false, Encoding.UTF8))
- {
- int len = allChannelData[channelIndex].Length;
- for (int i = 0; i < len; i++)
- sw.WriteLine(allChannelData[channelIndex][i]);
- }
- }
- }
- }
- }
- }
|