using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Threading; using ZedGraph; using System.Collections; using System.Linq; using DataCollectionSystem.CommonLib.Debug; using CommonLib.DAQ; using CommonLib.Config; using CommonLib.LOG; using System.Diagnostics; using System.Threading.Tasks; using System.IO; using Newtonsoft.Json; using CommonLib.Http; using System.Net.Http; using NodaTime; using System.Text; using System.Net.Http.Headers; using CommonLib.DB; using static CommonLib.Config.ConfigValue; using CommonLib.WavFile; namespace DataCollectionSystem { public partial class MainForm : Form { public static WriteFluxDB fluxDB = new WriteFluxDB(); public static List ListDAQCards { get; private set; } = new List(4); /// /// 用来存储绘图的点Y1 /// private PointPairList list1 = new PointPairList(); private PointPairList list2_1 = new PointPairList(); LineItem curve1; LineItem curve2_1; /// /// 采集数据队列,用来显示曲线 /// public static Queue drawLinedataQueue = new Queue(); public static Queue drawBarChartdataQueue = new Queue(); public static List PowerQueues = new List() { new Queue(), new Queue(), new Queue(), new Queue(), new Queue(), new Queue() }; Thread threadDraw = null; /// /// 当前选择的采集卡 /// public static int _selectCard=0; public static int SelectCardID = 0; public static int SelectCardChannel = 0; public static Object FFT_Amp = 0; /// /// 最大的显示点数 /// private static int _maxPointNumber = 1000; public static ConfigValue configValue = ConfigValue.InitConfig("DeviceConfig.json"); public MainForm() { InitializeComponent(); this.WindowState = FormWindowState.Maximized; } private async Task Scan() { await Task.Run(() => { Thread.Sleep(12000); //等待板卡启动 DAQCard.ScanDevice(); LoadDeviceConfig(); }); string info = $"采集卡搜索完成,发现{DAQCard.dictCard.Count}个设备"; this.notifyIcon.Visible = true;//设置提示控件可见 this.notifyIcon.ShowBalloonTip(1000, "发现设备:", info, ToolTipIcon.Info); 联机CToolStripMenuItem.Enabled = true; 断开DToolStripMenuItem.Enabled = false; return true; } static volatile Boolean bShutdown = false; //断开采集,休息一段时间后重连 Boolean RescanAndConnect() { if (!bShutdown) { Task.Run(async () => { bShutdown = true; //断开连接,发出定时请求,到时候自动重新扫描并重连 disconnectAll();//断开连接 //断开所有板卡 DAQCard.CloseAllDevices(); /* RelayCard.RelayCtrlOffAll(); //断开TCP server Thread.Sleep(25000); RelayCard.RelayCtrlOnAll(); */ Thread.Sleep(1000); //延时一段时间 //重新扫描 await Scan(); //连接 联机CToolStripMenuItem_Click(this, new EventArgs()); bShutdown = false; }); } return bShutdown; } private void DoReScanAndConnect(object sender, EventArgs e) { RescanAndConnect(); } private void Form1_Load(object sender, EventArgs e) { LoadDevices(); Scan(); tabControl1.Alignment = TabAlignment.Left; Control.CheckForIllegalCrossThreadCalls = false; //加在配置信息 //初始化绘图 ShowZedLine(); ShowZedLine2(); //InitBarChart(); InitChartPower(); // SetLineShowParam(1000, 100); //SetLineShowParam(5000,100); this.treeDevices.ExpandAll(); InitLog(); fluxDB.Init(configValue.InflexDBServers); } private string[] BarName = { "能量特征1", "能量特征2", "融合特征1", "融合特征2", "融合特征3", "融合特征4" }; Color[] Barcolors = { Color.Blue, Color.Green, Color.Yellow, Color.YellowGreen, Color.Gold, Color.DeepPink }; private PointPairList[] barPointLists = new PointPairList[6]; /// /// 初始化柱状图 融合柱状图 /// private void InitBarChart() { zedPower8.GraphPane.CurveList.Clear(); zedPower8.GraphPane.GraphObjList.Clear(); // clearing not teste // 画图面版标题 GraphPane myPane = zedPower8.GraphPane; myPane.Title.Text = "多源融合特征值"; // 画图面版X标题 myPane.XAxis.Title.Text = ""; myPane.XAxis.Title.IsVisible = false; myPane.XAxis.Scale.Min = 0; // 画图面版Y标题 myPane.YAxis.Title.Text = ""; myPane.YAxis.Title.IsVisible = false; //初始化数据 int i = 0; for (i = 0; i < BarName.Length; i++) { barPointLists[i] = new PointPairList(); barPointLists[i].Add(0, i + 1); myPane.AddBar(BarName[i], barPointLists[i], Barcolors[i]); } // myPane.XAxis.Scale.TextLabels = BarName; //myPane.XAxis.Type = AxisType.Text; string[] Labels = {"1","2"}; myPane.XAxis.Scale.TextLabels = Labels; myPane.XAxis.Type = AxisType.Text; } private List listPoints = null; private ZedGraphControl[] ZedPowers = null; /// /// 六个故障提示 /// private void InitChartPower() { // List listPoints = new List() { new PointPairList(), new PointPairList(), new PointPairList(), new PointPairList(), new PointPairList(), new PointPairList() }; string[] Titiles = { "倒谱特征1", "倒谱特征2", "短时能量", "过零率", "质心", "谱熵", }; string[] YTitiles = { "值", "值", "值", "%", "Hz", "值" }; //极坐标 double[] Amptitudes = new double[6] { 1, 1, 0.5, 0.5, 5000, 2 }; // ZedGraphControl[] ZedPowers = new ZedGraphControl[] { zedRMS,zedPower4, zedPower5, zedPower6, zedPower7, zedPower8 }; ZedPowers = new ZedGraphControl[] { zedRMS,zedPower4, zedPower5, zedPower6, zedPower7, zedPower8 }; for (int i =0;i< ZedPowers.Length;i++) { //foreach (in ZedPowers) var zed = ZedPowers[i]; //获取引用 GraphPane myPane = zed.GraphPane; curve1 = myPane.AddCurve("", listPoints[i], Color.LimeGreen, SymbolType.None); curve1.Line.Width = 2.0f; myPane.IsPenWidthScaled = true; myPane.Fill = new Fill(Color.DarkSeaGreen, Color.DarkSeaGreen);//(Color.Blue,Color.FromArgb(0, 1, 65)); //zedPower4.MasterPane.Fill.IsVisible = false; //this.zedGraphControl1.GraphPane.Chart.Fill.IsVisible = false; zed.MasterPane.Fill.IsVisible = true; zed.GraphPane.Chart.Fill.IsVisible = true; myPane.Title.Text = Titiles[i]; myPane.Title.IsVisible = true; myPane.YAxis.Title.Text = YTitiles[i]; //Titiles[i]; ; myPane.YAxis.Color = Color.White; myPane.Y2Axis.Title.Text = ""; // Titiles[i]; ; myPane.Chart.Fill = new Fill(Color.FromArgb(0, 1, 65), Color.FromArgb(0, 1, 65), 45.0f); myPane.YAxis.IsVisible = true; myPane.YAxis.CrossAuto = true; myPane.YAxis.Scale.FontSpec.Size = 9; myPane.XAxis.Scale.FontSpec.Size = 9; myPane.Y2Axis.Scale.FontSpec.Size = 9; myPane.YAxis.Scale.MajorStep = 0.05; //左边刻度步长 myPane.YAxis.Scale.Min = -0.5; myPane.YAxis.Scale.Max = Amptitudes[i];// 0.5; //左边刻度最大值 myPane.YAxis.Scale.MinorStepAuto = true; myPane.Y2Axis.Scale.MinorStepAuto = true; myPane.Y2Axis.Scale.MajorStep = 0.1; //左边刻度步长 myPane.Y2Axis.Scale.Min = -0.5; myPane.Y2Axis.Scale.Max = 0.5; //左边刻度最大值 myPane.Y2Axis.Scale.MinorStepAuto = true; myPane.Y2Axis.IsVisible = false; myPane.Y2Axis.Scale.MinorStep = 0.1; myPane.YAxis.Scale.MinorStep = 0.5; myPane.Y2Axis.MajorTic.IsOpposite = false; myPane.YAxis.MajorTic.IsOpposite = false; myPane.XAxis.Type = AxisType.Date;// AxisType.Text; // myPane.XAxis.Type = AxisType.Text; // myPane.XAxis.Type = ZedGraph.AxisType.Linear; myPane.XAxis.Title.Text = "";// 100ms/格"; myPane.XAxis.Title.FontSpec.FontColor = Color.White; myPane.YAxis.Title.FontSpec.FontColor = Color.White; myPane.Y2Axis.Title.FontSpec.FontColor = Color.White; //myPane.XAxis.IsVisible = false; // myPane.Y2Axis.MinorGrid.IsVisible = false; myPane.XAxis.Scale.Min = 0; //X轴最小值0 myPane.XAxis.Scale.Max = 3600; //X轴最大30 myPane.XAxis.Scale.MinorStep = 1000; myPane.XAxis.Scale.MajorStep = 1000; myPane.XAxis.Scale.IsVisible = true; myPane.XAxis.Scale.FontSpec.Size = 12.0f; myPane.YAxis.Scale.FontSpec.Size = 12.0f; myPane.Y2Axis.Scale.FontSpec.Size = 12.0f; //myPane.XAxis.Scale.MinorStep = 1;//X轴小步长1,也就是小间隔 // myPane.XAxis.Scale.MajorStep = 1;//X轴大步长为5,也就是显示文字的大间隔 myPane.XAxis.MajorGrid.IsVisible = true;//设置X虚线 myPane.XAxis.MajorGrid.Color = Color.White; myPane.YAxis.MajorGrid.IsVisible = true;//设置Y虚线 myPane.XAxis.MajorGrid.Color = Color.FromArgb(100, 120, 120); myPane.YAxis.MajorGrid.Color = Color.FromArgb(120, 120, 120); myPane.Chart.Border.Color = Color.White; myPane.XAxis.Scale.FontSpec.FontColor = Color.White; myPane.XAxis.Scale.MajorStepAuto = true; myPane.XAxis.Scale.MinorStepAuto = true; myPane.YAxis.Scale.MajorStepAuto = true; myPane.YAxis.Scale.MinorStepAuto = true; myPane.XAxis.Type = AxisType.Date; //myPane.XAxis.Scale.Format = "yyyy-MM-dd HH:mm:ss"; myPane.XAxis.Scale.Format = "HH:mm:ss"; myPane.YAxis.Scale.FontSpec.FontColor = Color.White; myPane.Y2Axis.Scale.FontSpec.FontColor = Color.White; myPane.YAxis.MajorGrid.DashOff = 3.0f; myPane.Y2Axis.MajorGrid.DashOff = 3.0f; zed.AxisChange(); zed.IsShowPointValues = false; #region TestWave /* //myPane.XAxis.Type = AxisType.Text; DateTime dtNow = DateTime.Now; TimeSpan halfHour = new TimeSpan(0, 30, 0); string[] XAxisLabels = new string[3]; int begin = 0; int half = 1; int end = 2; XAxisLabels[begin] = (dtNow -halfHour - halfHour).ToString("HH:mm:ss"); XAxisLabels[half] = (dtNow- halfHour).ToString("HH:mm:ss"); XAxisLabels[end] = dtNow.ToString("HH:mm:ss"); PointPairList pointpair = listPoints[i]; pointpair.Clear(); DateTime dt = DateTime.Now; TimeSpan _1Hour = new TimeSpan(1, 0, 0); dt = dt - _1Hour; TimeSpan TimeGap = new TimeSpan(0, 0, 1); Random rand = new Random(); for (int j=0;j<100;j++) { double Y = rand.Next(100)*0.001f; double t2 = dt.ToOADate(); dt = dt + TimeGap; pointpair.Add(new PointPair(t2, Y)); } //myPane.XAxis.Scale.TextLabels = XAxisLabels; */ #endregion zed.AxisChange(); zed.Invalidate(); } } private void DrawPowerChart() { DateTime dtNow = DateTime.Now; try { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0;i< PowerQueues.Count;i++) { DateTime dt = dtNow; var ZedPower = ZedPowers[i]; var queue = PowerQueues[i]; if (queue.Count > 0) { var pointpair = listPoints[i]; pointpair.Clear(); double[] data = queue.Dequeue() as double[]; TimeSpan timeGapAll = new TimeSpan(0, 0, data.Count()); dt = dt - timeGapAll; TimeSpan TimeGap1s = new TimeSpan(0, 0, 1); double MaxValue = data.Max(); double MinValue = data.Min(); GraphPane myPane = ZedPower.GraphPane; myPane.YAxis.Scale.Max = MaxValue * 1.2f; if(MinValue<0) myPane.YAxis.Scale.Min = MinValue * 1.1f; else myPane.YAxis.Scale.Min = MinValue * 0.9f; myPane.XAxis.Scale.Min = dt.ToOADate(); ; //打时间戳 for (int j = 0; j < data.Length; j++) { double Y = data[j]; double t2 = dt.ToOADate(); dt = dt + TimeGap1s; pointpair.Add(new PointPair(t2, Y)); } myPane.XAxis.Scale.Min = dt.ToOADate(); ; ZedPower.AxisChange(); ZedPower.Refresh(); } } watch.Stop(); double t = watch.ElapsedMilliseconds; string info = string.Format("显示消耗时间{0}ms", t.ToString("F3")); //LogStorage.Logs.Add(LogLevel.Trace, info); } catch (Exception ex) { LogStorage.Logs.Add(LogLevel.Error, ex.Message + ex.StackTrace); } } private void DrawBarChart() { try { int i = 0; Stopwatch watch = new Stopwatch(); watch.Start(); if (drawBarChartdataQueue.Count > 0) { GraphPane myPane = zedPower8.GraphPane; /* lock (MainForm.FFT_Amp) { myPane.Title.Text = "K-Means聚类" + DateTime.Now.ToString("") + " " + FFT_Amp.ToString(); } */ double[] data = drawBarChartdataQueue.Dequeue() as double[]; for (i = 0; i < BarName.Length; i++) { barPointLists[i].Clear(); barPointLists[i].Add(0, data[i]) ; } zedPower8.AxisChange(); zedPower8.Refresh();//这句话非常重要,否则不会立即显示 } else { } watch.Stop(); double t = watch.ElapsedMilliseconds; string info = string.Format("显示消耗时间{0}ms", t.ToString("F3")); //LogStorage.Logs.Add(LogLevel.Trace, info); } catch(Exception ex) { LogStorage.Logs.Add(LogLevel.Error, ex.Message + ex.StackTrace); } } /// /// 根据最大点与单步刻度动态的调整显示曲线 /// /// 最大点 /// 单步值 private void SetLineShowParam(double maxPoint, double stepPoint) { GraphPane myPane = zedGraphControl_Amp.GraphPane; //myPane.XAxis.IsVisible = false; myPane.XAxis.Scale.Min = 0; //X轴最小值0 myPane.XAxis.Scale.Max = maxPoint; //X轴最大30 myPane.XAxis.Scale.MinorStep = stepPoint / 5;//X轴小步长1,也就是小间隔 myPane.XAxis.MinorTic.IsInside = true; myPane.XAxis.MajorGrid.DashOff = 1.0f; myPane.XAxis.Scale.MajorStep = stepPoint; myPane.XAxis.Scale.Mag = 0; ClearLine(); } public static ListMonitorDevices monitorDevices = null; private void LoadDevices() { treeDevices.Nodes.Clear(); string JsonFile = "MonitorDevices.json"; string Json = File.ReadAllText(JsonFile); monitorDevices = JsonConvert.DeserializeObject(Json); if(monitorDevices.listMonitorDevices.Count>0) { MonitorDevices monitorDevice = monitorDevices.listMonitorDevices[0]; TreeNode nodeType = new TreeNode(monitorDevice.Name); treeDevices.Nodes.Add(nodeType); List devices = monitorDevice.devices; foreach (var device in devices) { TreeNode nodeDevice = new TreeNode(device.Name); nodeType.Nodes.Add(nodeDevice); var deviceChannels = device.DeviceChannels; foreach (var deviceChannel in deviceChannels) { TreeNode NodeChannel = new TreeNode(deviceChannel.Name); nodeDevice.Nodes.Add(NodeChannel); } } } } /// /// 加载配置文件 /// private void LoadDeviceConfig() { try { int cnt = configValue.cardInfos.Count; for (int i = 0; i < cnt; i++) { string HostName = configValue.cardInfos[i].HostName; DAQCard card = DAQCard.GetCard(HostName); if (card != null) { card.Threashold = configValue.cardInfos[i].threashold; card.EventscanAndConnect+= DoReScanAndConnect; //card.chlCnt = configValue.cardInfos[i].FileCnt; ListDAQCards.Add(card); } treeDevices.Nodes[0].Nodes[i].Tag = configValue.cardInfos[i].HostName; treeDevices.Nodes[0].Nodes[i].Text += string.Format("-{0}", configValue.cardInfos[i].HostName); treeDevices.Nodes[0].Nodes[i].Tag = configValue.cardInfos[i].HostName; } treeDevices.Nodes[0].Nodes[0].ImageIndex = 0; } catch(Exception ex) { LogStorage.Logs.Add(LogLevel.Error, ex.Message + ex.StackTrace); } } Boolean bRunDraw = false; private void DrawChart() { drawLinedataQueue.Clear(); bRunDraw = true; while (bRunDraw) { Thread.Sleep(1); DrawLineFun(); //DrawBarChart(); DrawPowerChart(); } } /// /// 绘图线程 /// private void DrawLineFun() { double[] line1Point; double[] line2Point; double[] PointCount; try { if (drawLinedataQueue.Count > 0) { Console.WriteLine("入队列中数据数:{0}", drawLinedataQueue.Count); DisplayPointArray data; lock (this) { data = drawLinedataQueue.Dequeue() as DisplayPointArray; } Console.WriteLine("出队列中数据数:{0}", drawLinedataQueue.Count); // Console.WriteLine("--------------------时间={0}秒{1}毫秒", DateTime.Now.Second, DateTime.Now.Millisecond); if (data != null) { line1Point = new double[data.Line1Display.Length]; line2Point = new double[data.Line1FFTDisplay.Length]; PointCount = new double[data.Line1Display.Length]; for (int i = 0; i < data.Line1Display.Length; i++) { line1Point[i] = data.Line1Display[i]; line2Point[i] = data.Line1FFTDisplay[i]; PointCount[i] = data.Line1Display.Length - i; } DrawLineByData(line1Point, PointCount); DrawLineByData2(line2Point, PointCount); } // Console.WriteLine("--------------------时间={0}秒{1}毫秒", DateTime.Now.Second, DateTime.Now.Millisecond); } } catch { } } #region 显示电流曲线 private void ShowZedLine() { try { //获取引用 GraphPane myPane = zedGraphControl_Amp.GraphPane; curve1 = myPane.AddCurve("", list1, Color.LimeGreen, SymbolType.None); curve1.Line.Width = 2.0f; myPane.IsPenWidthScaled = true; myPane.Fill = new Fill(Color.FromArgb(0, 1, 65)); this.zedGraphControl_Amp.MasterPane.Fill.IsVisible = false; this.zedGraphControl_Amp.GraphPane.Chart.Fill.IsVisible = false; myPane.Title.Text = "实时信号"; myPane.Title.IsVisible = false; myPane.YAxis.Title.Text = " 实时信号"; myPane.YAxis.Color = Color.White; myPane.Y2Axis.Title.Text = "实时信号 "; myPane.Chart.Fill = new Fill(Color.FromArgb(0, 1, 65), Color.FromArgb(0, 1, 65), 45.0f); myPane.YAxis.IsVisible = true; myPane.YAxis.CrossAuto = true; myPane.YAxis.Scale.FontSpec.Size = 9; myPane.XAxis.Scale.FontSpec.Size = 9; myPane.Y2Axis.Scale.FontSpec.Size = 9; myPane.YAxis.Scale.MajorStep = 0.005; //左边刻度步长 myPane.YAxis.Scale.Min = -0.005; myPane.YAxis.Scale.Max = 0.005; //左边刻度最大值 myPane.YAxis.Scale.MinorStepAuto = true; myPane.Y2Axis.Scale.MinorStepAuto = true; myPane.Y2Axis.Scale.MajorStep = 0.001; //左边刻度步长 myPane.Y2Axis.Scale.Min = -0.005; myPane.Y2Axis.Scale.Max = 0.005; //左边刻度最大值 myPane.Y2Axis.Scale.MinorStepAuto = true; myPane.Y2Axis.IsVisible = false; myPane.Y2Axis.Scale.MinorStep = 0.001; myPane.YAxis.Scale.MinorStep = 0.005; myPane.Y2Axis.MajorTic.IsOpposite = false; myPane.YAxis.MajorTic.IsOpposite = false; //myPane.XAxis.Scale.Format = "HH:mm:ss"; //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // myPane.XAxis.Type = ZedGraph.AxisType.Linear; myPane.XAxis.Title.Text = " 100ms/格"; myPane.XAxis.Title.FontSpec.FontColor = Color.White; myPane.YAxis.Title.FontSpec.FontColor = Color.White; myPane.Y2Axis.Title.FontSpec.FontColor = Color.White; //myPane.XAxis.IsVisible = false; // myPane.Y2Axis.MinorGrid.IsVisible = false; myPane.XAxis.Scale.Min = 0; //X轴最小值0 myPane.XAxis.Scale.Max = 10000; //X轴最大30 myPane.XAxis.Scale.MinorStep = 1000; myPane.XAxis.Scale.MajorStep = 1000; myPane.XAxis.Scale.IsVisible = true; myPane.XAxis.Scale.FontSpec.Size = 12.0f; myPane.YAxis.Scale.FontSpec.Size = 12.0f; myPane.Y2Axis.Scale.FontSpec.Size = 12.0f; //myPane.XAxis.Scale.MinorStep = 1;//X轴小步长1,也就是小间隔 // myPane.XAxis.Scale.MajorStep = 1;//X轴大步长为5,也就是显示文字的大间隔 myPane.XAxis.MajorGrid.IsVisible = true;//设置X虚线 myPane.XAxis.MajorGrid.Color = Color.White; myPane.YAxis.MajorGrid.IsVisible = true;//设置Y虚线 myPane.XAxis.MajorGrid.Color = Color.FromArgb(100, 120, 120); myPane.YAxis.MajorGrid.Color = Color.FromArgb(120, 120, 120); myPane.Chart.Border.Color = Color.White; myPane.XAxis.Scale.FontSpec.FontColor = Color.White; myPane.YAxis.Scale.FontSpec.FontColor = Color.White; myPane.Y2Axis.Scale.FontSpec.FontColor = Color.White; myPane.YAxis.MajorGrid.DashOff = 3.0f; myPane.Y2Axis.MajorGrid.DashOff = 3.0f; zedGraphControl_Amp.AxisChange(); zedGraphControl_Amp.IsShowPointValues = false; // zedGraphControl1.PointValueEvent += new ZedGraphControl.PointValueHandler(MyPointValueHandler); } catch { // Logger.WriteLog("初始化图形错误" + et.Message + "||", et.Source, Logger..DrawLineError); } } private void ShowZedLine2() { try { //获取引用 GraphPane myPane = zedGraphControl_Freq.GraphPane; curve2_1 = myPane.AddCurve("", list2_1, Color.LimeGreen, SymbolType.None); curve2_1.Line.Width = 2.0f; myPane.IsPenWidthScaled = true; myPane.Fill = new Fill(Color.FromArgb(0, 1, 65)); this.zedGraphControl_Amp.MasterPane.Fill.IsVisible = false; this.zedGraphControl_Amp.GraphPane.Chart.Fill.IsVisible = false; myPane.Title.Text = "频谱曲线"; myPane.Title.IsVisible = false; myPane.YAxis.Title.Text = " 频谱曲线"; myPane.YAxis.Color = Color.White; myPane.Y2Axis.Title.Text = " 频谱曲线"; myPane.Chart.Fill = new Fill(Color.FromArgb(0, 1, 65), Color.FromArgb(0, 1, 65), 45.0f); myPane.YAxis.IsVisible = true; myPane.YAxis.CrossAuto = true; myPane.YAxis.Scale.FontSpec.Size = 9; myPane.XAxis.Scale.FontSpec.Size = 9; myPane.Y2Axis.Scale.FontSpec.Size = 9; myPane.YAxis.Scale.MajorStep = 0.001; //左边刻度步长 myPane.YAxis.Scale.Min = -0.001; myPane.YAxis.Scale.Max = 0.005; //左边刻度最大值 myPane.YAxis.Scale.MinorStepAuto = true; myPane.Y2Axis.Scale.MajorStep = 10; //左边刻度步长 myPane.Y2Axis.Scale.Min = -100; myPane.Y2Axis.Scale.Max = 100; //左边刻度最大值 myPane.Y2Axis.Scale.MinorStepAuto = true; myPane.Y2Axis.IsVisible = false; myPane.Y2Axis.Scale.MinorStep = 100; myPane.YAxis.Scale.MinorStep = 10; myPane.Y2Axis.MajorTic.IsOpposite = false; myPane.YAxis.MajorTic.IsOpposite = false; // myPane.XAxis.Scale.Format = "HH:mm:ss"; //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") //myPane.XAxis.Type = ZedGraph.AxisType.; myPane.XAxis.Title.Text = "频率/Hz"; myPane.XAxis.Title.FontSpec.FontColor = Color.White; myPane.YAxis.Title.FontSpec.FontColor = Color.White; myPane.Y2Axis.Title.FontSpec.FontColor = Color.White; //myPane.XAxis.IsVisible = false; myPane.Y2Axis.MinorGrid.IsVisible = false; myPane.XAxis.Scale.Min = 0; //X轴最小值0 // myPane.XAxis.Scale.Max = 10000; //X轴最大30 myPane.XAxis.Scale.Max = 5000; //X轴最大30 myPane.XAxis.Scale.MinorStep = 1000; myPane.XAxis.Scale.MajorStep = 1000; myPane.XAxis.Scale.IsVisible = true; myPane.XAxis.Scale.FontSpec.Size = 12.0f; myPane.YAxis.Scale.FontSpec.Size = 12.0f; myPane.Y2Axis.Scale.FontSpec.Size = 12.0f; //myPane.XAxis.Scale.MinorStep = 1;//X轴小步长1,也就是小间隔 // myPane.XAxis.Scale.MajorStep = 1;//X轴大步长为5,也就是显示文字的大间隔 myPane.XAxis.MajorGrid.IsVisible = true;//设置X虚线 myPane.XAxis.MajorGrid.Color = Color.White; myPane.YAxis.MajorGrid.IsVisible = true;//设置Y虚线 myPane.XAxis.MajorGrid.Color = Color.FromArgb(100, 120, 120); myPane.YAxis.MajorGrid.Color = Color.FromArgb(120, 120, 120); myPane.Chart.Border.Color = Color.White; myPane.XAxis.Scale.FontSpec.FontColor = Color.White; myPane.YAxis.Scale.FontSpec.FontColor = Color.White; myPane.Y2Axis.Scale.FontSpec.FontColor = Color.White; myPane.YAxis.MajorGrid.DashOff = 3.0f; myPane.Y2Axis.MajorGrid.DashOff = 3.0f; zedGraphControl_Freq.AxisChange(); zedGraphControl_Freq.IsShowPointValues = false; // zedGraphControl1.PointValueEvent += new ZedGraphControl.PointValueHandler(MyPointValueHandler); } catch { // Logger.WriteLog("初始化图形错误" + et.Message + "||", et.Source, Logger..DrawLineError); } } #endregion double x = _maxPointNumber; private void DrawLineByData(double[] _vol, double[] pointNumber) { try { if (zedGraphControl_Amp.GraphPane.CurveList.Count <= 0) { return; } //取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem LineItem curve = zedGraphControl_Amp.GraphPane.CurveList[0] as LineItem; if (curve == null) { return; } //第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据 IPointListEdit list = curve.Points as IPointListEdit; if (list == null) { return; } list1.Clear(); list1.Add(pointNumber, _vol); AutoRangeYAxis1(_vol, zedGraphControl_Amp); // zedGraphControl_Amp.GraphPane.YAxis.Scale.MaxAuto = true; //for (int i = 0; i < _vol.Length; i++) //{ // LineAutoFromSystem(_vol[i], _curr[i]); //} // Console.WriteLine("绘点时间:[{0}],绘点数据--时间[{1}:{4}]-电压[{2}],电流[{3}]", DateTime.Now, dtNow, _vol, _curr, dtNow.Millisecond); //string outMsg = string.Format("绘点时间:[{0}],绘点数据--时间[{1}:{4}]-电压[{2}],电流[{3}]", DateTime.Now, dtNow, _vol, _curr, dtNow.Millisecond); ///SetrichTextBox(outMsg + Environment.NewLine); zedGraphControl_Amp.AxisChange(); zedGraphControl_Amp.Invalidate(); // x--; //System.Threading.Thread.Sleep(waitTime); // } catch(Exception ex) { LogStorage.Logs.Add(LogLevel.Error, ex.Message + ex.StackTrace); } } private void DrawLineByData2(double[] _vol, double[] pointNumber) { if (zedGraphControl_Freq.GraphPane.CurveList.Count <= 0) { return; } //取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem LineItem curve = zedGraphControl_Freq.GraphPane.CurveList[0] as LineItem; if (curve == null) { return; } //第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据 IPointListEdit list = curve.Points as IPointListEdit; if (list == null) { return; } list2_1.Clear(); list2_1.Add(pointNumber, _vol); // zedGraphControl_Freq.GraphPane.YAxis.Scale.MaxAuto = true; AutoRangeYAxis2(_vol, zedGraphControl_Freq); zedGraphControl_Freq.AxisChange(); zedGraphControl_Freq.Invalidate(); // x--; //System.Threading.Thread.Sleep(waitTime); // } void AutoYaxis(double max, double min, ZedGraphControl zed) { double stepPoint = (max - min) / 10; GraphPane myPane = zed.GraphPane; //myPane.XAxis.IsVisible = false; myPane.YAxis.Scale.Min = min; //X轴最小值0 myPane.YAxis.Scale.Max = max; //X轴最大30 myPane.YAxis.Scale.MinorStep = stepPoint / 5;//X轴小步长1,也就是小间隔 myPane.YAxis.MinorTic.IsInside = true; myPane.YAxis.MajorGrid.DashOff = 1.0f; myPane.YAxis.Scale.MajorStep = stepPoint; myPane.YAxis.Scale.Mag = 0; } void AutoRangeYAxis1(double[] YValues , ZedGraphControl zed) { double max = YValues.AsEnumerable().Max(); double min = YValues.AsEnumerable().Min(); /* double space = Math.Round((max - min) * 0.1f, 1); zed.GraphPane.YAxis.Scale.Max = max + space; zed.GraphPane.YAxis.Scale.Min = min - space; if (max > 0.6) { max = 3.2f; min = -2.2f; } else { max = 0.5f; min = -0.5f; } */ AutoYaxis(max, min, zed); } void AutoRangeYAxis2(double[] YValues, ZedGraphControl zed) { double max = YValues.AsEnumerable().Max(); double min = YValues.AsEnumerable().Min(); /* double space = Math.Round((max - min) * 0.1f, 1); zed.GraphPane.YAxis.Scale.Max = max + space; zed.GraphPane.YAxis.Scale.Min = min - space; */ /* if (max > 0.6) { max = 3.2f; min = 0; } else { max = 0.5f; min = 0; } */ AutoYaxis(max, min, zed); //zed.GraphPane.YAxis.Scale.Max = max; //zed.GraphPane.YAxis.Scale.Min = min; //0.632 3.16 } private int GetNearInt(double number) { int tempNumber = Int32.Parse(Math.Truncate(number * 1.2).ToString()); int result = 0; for (int i = 0; i < 10; i++) { if ((tempNumber + i) % 10 == 0) { result = tempNumber + i; break; } } return result; } private void ClearLine() { try { if (list1.Count > 0) { int listCount = list1.Count; for (int i = 0; i < listCount; i++) { list1.RemoveAt(0); } } // xtag = 0; // zedGraphControl1.GraphPane.XAxis.Scale.Max = 10; zedGraphControl_Amp.GraphPane.YAxis.Scale.Min = 0; zedGraphControl_Amp.GraphPane.YAxis.Scale.MajorStep = 1; zedGraphControl_Amp.GraphPane.YAxis.Scale.Max = 10; zedGraphControl_Amp.GraphPane.Y2Axis.Scale.Min = 0; zedGraphControl_Amp.GraphPane.Y2Axis.Scale.MajorStep = 1; zedGraphControl_Amp.GraphPane.Y2Axis.Scale.Max = 10; } catch { } } private void toolStripButton5_Click(object sender, EventArgs e) { //Thread t3 = new Thread(InsertDataToDataBase); //t3.Start(); } List tbInertData = new List(); private void toolStripButton7_Click(object sender, EventArgs e) { } private void toolStripButton3_Click(object sender, EventArgs e) { } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { // ClearLine(); try { if(e.Node.Level==2) { string CardName = e.Node.Parent.Tag as string; var cardInfos = ListDAQCards.Where(DAQCard => DAQCard.HostName == CardName).ToArray(); if (cardInfos.Count() > 0) { SelectCardID = cardInfos[0].CardNO; SelectCardChannel = e.Node.Index; string Info = string.Format("选中{0}#卡的{1}通道", SelectCardID + 1, SelectCardChannel + 1);// $"选中{SelectCardID + 1}#卡的{SelectCardChannel + 1}通道"; LogStorage.Logs.Add(LogLevel.Trace, Info); } else { SelectCardID = -1; } } } catch { } } private void toolStripButton4_Click(object sender, EventArgs e) { } private double GetMaxVolt() { double maxNumber = 0.0; for (int i = 0; i < list1.Count; i++) { if (maxNumber < list1[i].Y) { maxNumber = list1[i].Y; } } return maxNumber; } private void SetY2Off() { curve1.IsY2Axis = false; } private void MainForm_SizeChanged(object sender, EventArgs e) { groupBox1.Width = this.Width - 40; groupBox1.Height = this.Height - 100 - this.menu.Height -pictureBox1.Height; treeDevices.Height = groupBox1.Height - 20; tabControl1.Width = groupBox1.Width - treeDevices.Width - 20; zedGraphControl_Amp.Width = tabControl1.Width - 40; zedGraphControl_Freq.Width = tabControl1.Width - 40; tabControl1.Height = groupBox1.Height - 20; zedGraphControl_Amp.Height = (tabControl1.Height - 50)/2; zedGraphControl_Freq.Location = new Point(zedGraphControl_Freq.Location.X, zedGraphControl_Amp.Location.Y + zedGraphControl_Amp.Height+10); zedGraphControl_Freq.Height = zedGraphControl_Amp.Height; // groupBox3.Location = new Point(groupBox1.Width - groupBox2.Width - groupBox2.Width - 20, zedGraphControl1.Location.Y + zedGraphControl1.Height); // groupBox2.Location = new Point(groupBox2.Location.X, zedGraphControl1.Location.Y + zedGraphControl1.Height + zedGraphControl2.Height); } private void zedGraphControl1_ContextMenuBuilder(ZedGraphControl sender, ContextMenuStrip menuStrip, Point mousePt) { foreach (ToolStripMenuItem item in menuStrip.Items) { switch (item.Name) { case "copied_to_clip": item.Text = @"复制到剪贴板"; break; case "copy": item.Text = @"复制"; break; case "page_setup": item.Text = @"页面设置..."; break; case "print": item.Text = @"打印..."; break; case "save_as": item.Text = @"另存图表..."; break; case "set_default": item.Visible = false; break; case "show_val": item.Text = @"显示节点数值"; break; //case "title_def": // item.Text = @"标题"; // break; case "undo_all": item.Text = @"恢复默认大小"; break; //case "unpan": // item.Text = @"还原移动"; // break; case "unzoom": item.Text = @"还原缩放"; break; case "x_title_def": item.Text = @"X 轴"; break; case "y_title_def": item.Text = @"Y 轴"; break; } } } private void 系统设置ToolStripMenuItem_Click(object sender, EventArgs e) { try { using (FormSystemSetting form = new FormSystemSetting()) { if (form.ShowDialog() == DialogResult.OK) { } } } catch(Exception ex) { LogStorage.Logs.Add(LogLevel.Error, $"{ex.Message} {ex.StackTrace}"); } } private void 联机CToolStripMenuItem_Click(object sender, EventArgs e) { try { int cnt = ListDAQCards.Count; if(cnt > 0) { int i = 0; var Cards = ListDAQCards.Count; // Task.Run(() => { for (i = 0; i < cnt; i++) { DAQCard card = ListDAQCards[i]; var cardInfos = configValue.cardInfos.AsEnumerable() .Where(cardInfo => cardInfo.HostName == card.HostName).ToArray(); Boolean en = cardInfos[0].Enable; int chlcnt = cardInfos[0].FileCnt; if (en) { if (!card.Enable) { card.Enable = en; } } } if ((threadDraw == null)||(threadDraw.IsAlive==false)) { threadDraw = new Thread(DrawChart); threadDraw.Start(); } 联机CToolStripMenuItem.Enabled = false; 断开DToolStripMenuItem.Enabled = true; } } catch (Exception ex) { LogStorage.Logs.Add(LogLevel.Error, $"{ex.Message} {ex.StackTrace}"); } } private void disconnectAll() { bRunDraw = false; //停止重绘 int cnt = ListDAQCards.Count; int[] FileCntArray = { 2, 3, 3, 4 }; for (int i = 0; i < cnt; i++) { ListDAQCards[i].Enable = false; } ListDAQCards.Clear(); 联机CToolStripMenuItem.Enabled = true; 断开DToolStripMenuItem.Enabled = false; } private void 断开DToolStripMenuItem_Click(object sender, EventArgs e) { disconnectAll(); } private void 查看历史数据ToolStripMenuItem_Click(object sender, EventArgs e) { NewShow_FIFO_Line_Form fifoShowFrm = new NewShow_FIFO_Line_Form(); fifoShowFrm.ShowDialog(); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("您确定要退出程序吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { e.Cancel = true; } Thread.Sleep(100); 断开DToolStripMenuItem_Click(this, new EventArgs()); } private void 重新记录测试数据ToolStripMenuItem_Click(object sender, EventArgs e) { } private void 重新记录测试数据ToolStripMenuItem1_Click(object sender, EventArgs e) { 断开DToolStripMenuItem_Click(null, null); Thread.Sleep(1000); 联机CToolStripMenuItem_Click(null, null); } private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { if (tabControl1.SelectedIndex == 1) { //MessageBox.Show("aa"); //DrawBarChart(); } } private void InitLog() { TabPage PageAllLog = MakeLogTabPage(-1); PageAllLog.Text = "All"; tabLog.Controls.Add(PageAllLog); /////////////////////////////////////////////////// TabPage PageErrLog = MakeLogTabPage(0); PageErrLog.Text = "Error"; tabLog.Controls.Add(PageErrLog); /////////////////////////////////////////////////// TabPage PageWarnLog = MakeLogTabPage(1); PageWarnLog.Text = "Warning"; tabLog.Controls.Add(PageWarnLog); /////////////////////////////////////////////////// TabPage PageCommLog = MakeLogTabPage(4); PageCommLog.Text = "Network"; tabLog.Controls.Add(PageCommLog); TabPage PageMotorLog = MakeLogTabPage(7); PageMotorLog.Text = "故障报告"; tabLog.Controls.Add(PageMotorLog); tabLog.Dock = DockStyle.Fill; // CustomWindows.SetColumnAttrib(view, "Resizable", DataGridViewTriState.False); } private TabPage MakeLogTabPage(int LogLevel) { LogView view = new LogView(); //开启双缓冲 view.Interval = 100; view.Dock = DockStyle.Fill; view.Logs = LogStorage.Logs; view.Level = LogLevel;// int.Parse((string)tabpage.Tag); view.AutoScroll = false;//!this.ScrollLock; view.BringToFront(); view.Columns[0].Resizable = DataGridViewTriState.False; view.Columns[1].Resizable = DataGridViewTriState.False; view.Columns[1].SortMode = DataGridViewColumnSortMode.Automatic; //panelLog //listLogView.Add(view); TabPage PageLog = new TabPage(); PageLog.Dock = DockStyle.Fill; PageLog.Controls.Add(view); return PageLog; } private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) { // } class Person { public string Name { get; set; } public string Occupation { get; set; } public override string ToString() { return $"{Name}: {Occupation}"; } }; class Contributor { public string Login { get; set; } public short Contributions { get; set; } public override string ToString() { return $"{Login,20}: {Contributions} contributions"; } } private async void testActionToolStripMenuItem_Click(object sender, EventArgs e) { /* int SampleRate = 320000; int freq = 5; AutoRange autoRange = new AutoRange(2); double[] channel0 = new double[SampleRate]; AutoRange.GenArray(channel0, 310, 1, freq); double[] channel1 = new double[SampleRate]; AutoRange.GenArray(channel1, 310, 0.2, freq); //需要测试的方法,直接传值 autoRange.AddData(0, channel0); autoRange.AddData(1, channel1); double[] outdata = autoRange.GetOutPutData(); channel0 = new double[SampleRate]; AutoRange.GenArray(channel0, 310, 1, freq); channel1 = new double[SampleRate]; AutoRange.GenArray(channel1, 310, 0.2, freq); autoRange.AddData(0, channel0); autoRange.AddData(1, channel1); outdata = autoRange.GetOutPutData(); channel0 = new double[SampleRate]; AutoRange.GenArray(channel0, 310, 1, freq); channel1 = new double[SampleRate]; AutoRange.GenArray(channel1, 310, 0.2, freq); autoRange.AddData(0, channel0); autoRange.AddData(1, channel1); outdata = autoRange.GetOutPutData(); */ try { /* HttpClientProcess client = new HttpClientProcess(); //var content = await client.GetStringAsync("http://127.0.0.1:8086"); var result = await client.GetAsync("http://127.0.0.1:8086"); Console.WriteLine(result.StatusCode); Console.WriteLine(result.Content); */ /* var url = "http://webcode.me"; var client = new WebClient(); var result = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url)); string content = Convert.ToString(result); Console.WriteLine(content); */ /* var person = new Person(); person.Name = "John Doe"; person.Occupation = "gardener"; var json = JsonConvert.SerializeObject(person); var data = new StringContent(json, Encoding.UTF8, "application/json"); var url = "https://httpbin.org/post"; var client = new HttpClient(); var response = await client.PostAsync(url, data); string result = response.Content.ReadAsStringAsync().Result; Console.WriteLine(result); */ /* //JSON var client = new HttpClient(); client.BaseAddress = new Uri("https://api.github.com"); client.DefaultRequestHeaders.Add("User-Agent", "C# console program"); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); var url = "repos/symfony/symfony/contributors"; HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); var resp = await response.Content.ReadAsStringAsync(); List contributors = JsonConvert.DeserializeObject>(resp); contributors.ForEach(Console.WriteLine); */ /* InfluxDBHelper client = new InfluxDBHelper("http://127.0.0.1:8086", "admin", "admin", "test"); //InfluxDBHelper client = new InfluxDBHelper("http://192.168.48.103:8086", "admin","admin","test","127.0.0.1",8888); string s = "test1,code=code001,mac=aaa v1=100,v2=200,v3=300"; client.Write(s); string Text1 = client.Query("select * from test1"); */ /* fluxDB.Write_features(false,4, 11, 11.8f, 11.5f, "192.168.1.101", "1-1"); fluxDB.Write_spectrogram("192.168.1.101", "1-1"); fluxDB.Wirte_spike_events("192.168.1.101", "1-1", 12.3f); fluxDB.Wirte_waveform("192.168.1.101", "1-1", 12.1f); */ string wavFileName = @"C:\Users\xukai\Music\100Hz.wav"; string wavFileName2 = @"C:\Users\xukai\Music\100Hz2.wav"; WaveFile wavfile = new WaveFile(1000,32000); wavfile.readWav(wavFileName); double[,] points; wavfile.GetWavePoints(out points); WaveFile waveFile2 = new WaveFile(44,32000); waveFile2.SetWavePoints(points); waveFile2.writeWav(wavFileName2); } catch (Exception ex) { LogStorage.Logs.Add(LogLevel.Error, ex.Message + ex.StackTrace); } } } }