功能实现 实现单片机与电脑通讯,通过串口接收。并显示通过串口过来的数据波形
主界面
在主界面设置串口号以及波特率,点击打开串口,再点击波形显示,即可显示通过串口过来的数据波形。
主界面源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace _3 { public delegate void ShowWindow ( ) ; public delegate void HideWindow ( ) ; public delegate void OpenPort ( ) ; public delegate void ClosePort ( ) ; public delegate Point GetMainPos ( ) ; public delegate int GetMainWidth ( ) ; public partial class MainForm : Form { Drawer Displayer; public MainForm ( ) { InitializeComponent(); System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false ; } private void serialPort1_DataReceived (object sender, System.IO.Ports.SerialDataReceivedEventArgs e ) { byte [] data = new byte [serialPort1.BytesToRead]; serialPort1.Read(data, 0 , data.Length); if (Displayer != null ) Displayer.AddData(data); } private void CreateNewDrawer ( ) { Displayer = new Drawer(); Displayer.Show(); } private void CreateDisplayer ( ) { this .Left = 0 ; CreateNewDrawer(); Rectangle Rect = Screen.GetWorkingArea(this ); } private void button2_Click (object sender, EventArgs e ) { try { serialPort1.PortName = comboBox1.Text; serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text); serialPort1.Open(); button2.Enabled = false ; button3.Enabled = true ; } catch { MessageBox.Show("端口错误" , "错误" ); } } private void button3_Click (object sender, EventArgs e ) { try { serialPort1.Close(); button2.Enabled = true ; button3.Enabled = false ; } catch { } } private void Form1_Load (object sender, EventArgs e ) { for (int i = 1 ; i < 6 ; i++) { comboBox1.Items.Add("COM" + i.ToString()); } comboBox1.Text = "COM1" ; comboBox2.Text = "4800" ; } private void button1_Click (object sender, EventArgs e ) { if (Displayer == null ) { CreateDisplayer(); } else { if (Displayer.IsDisposed) { CreateDisplayer(); } } } private void comboBox1_SelectedIndexChanged (object sender, EventArgs e ) { } } }
绘制界面
绘制界面源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace _3 { public partial class Drawer : Form { private const int Unit_length = 22 ; private int DrawStep = 5 ; private const int Y_Max = 255 ; private const int MaxStep = 30 ; private const int MinStep = 1 ; private const int StartPrint = 32 ; private List <byte > DataList = new List<byte >(); private List <byte > DataListZH = new List<byte >(); private Pen LinesPen = new Pen(Color.FromArgb(0xaa , 0x00 , 0x00 )); private bool KeyShift, KeyStepUp, KeyStepDown; public Drawer ( ) { this .SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true ); this .UpdateStyles(); InitializeComponent(); } public void AddData (byte [] Data ) { for (int i = 0 ; i < Data.Length; i++) DataList.Add(Data[i]); Invalidate(); } private void Drawer_Load (object sender, EventArgs e ) { } private void Drawer_Paint (object sender, PaintEventArgs e ) { e.Graphics.FillRectangle(Brushes.White, e.Graphics.ClipBounds); if (DataList.Count - 1 >= (this .ClientRectangle.Width - StartPrint) / DrawStep) { DataList.RemoveRange(0 , DataList.Count - (this .ClientRectangle.Width - StartPrint) / DrawStep - 1 ); } byte [] DataListt = DataList.ToArray(); int [] D = new int [DataListt.Length]; for (int i = 0 ; i < DataListt.Length; i++) { int m = DataListt[i]; D[i] = m; } int [] D2 = new int [DataListt.Length]; double [] B = new double [3 ] { 0.96897915136010271 , -1.5678412012906751 , 0.96897915136010271 }; double [] A = new double [3 ] { 1 , -1.5678412012906751 , 0.93795830272020542 }; double W1 = 0 ; double W2 = 0 ; double W3 = 0 ; for (int i = 0 ; i < DataListt.Length; i++) { W1 = A[0 ] * D[i] - A[1 ] * W2 - A[2 ] * W3; double s = Math.Floor(B[0 ] * W1 + B[1 ] * W2 + B[2 ] * W3); D2[i] = (int )s; W3 = W2; W2 = W1; } W1 = 0 ; W2 = 0 ; W3 = 0 ; int [] D3 = new int [DataListt.Length]; D3 = D2; for (int i = 2 ; i < DataListt.Length - 3 ; i++) { int s = D2[i - 2 ] + D2[i - 1 ] + D2[i] + D2[i + 1 ] + D2[i + 2 ] ; D3[i] = s / 5 ; } for (int i = 0 ; i < D3.Length; i++) { byte [] M = BitConverter.GetBytes(D3[i]); DataListZH.Add(M[0 ]); Invalidate(); } if (DataListZH.Count - 1 >= (this .ClientRectangle.Width - StartPrint) / DrawStep) { DataListZH.RemoveRange(0 , DataListZH.Count - (this .ClientRectangle.Width - StartPrint) / DrawStep - 1 ); } for (int i = 0 ; i < DataListZH.Count - 1 ; i++) { e.Graphics.DrawLine(LinesPen, StartPrint + i * DrawStep, 17 * Unit_length - DataListZH[i]*3 , StartPrint + (i + 1 ) * DrawStep, 17 * Unit_length - DataListZH[i + 1 ]*3 ); } } private void Drawer_KeyDown (object sender, KeyEventArgs e ) { if (e.Shift) KeyShift = true ; switch (e.KeyCode) { case Keys.Up: KeyStepUp = true ; break ; case Keys.Down: KeyStepDown = true ; break ; default : break ; } } private void Drawer_KeyUp (object sender, KeyEventArgs e ) { if (KeyShift) { if (KeyStepUp) { if (DrawStep < MaxStep) DrawStep++; Invalidate(); KeyStepUp = false ; } else { if (KeyStepDown) { if (DrawStep > MinStep) DrawStep--; Invalidate(); KeyStepDown = false ; } } } else { KeyStepUp = false ; KeyStepDown = false ; } KeyShift = false ; } } }
使用说明 问题一:复制程序后没有效果 A:需要对事件进行绑定!在设计窗口下,点击事件按钮(带闪电图标),然后分别将对应函数和事件联系起来 KeyDown->Drawer_KeyDown、KeyUp->Drawer_KeyUp、Paint->Drawer_Paint,而后 Invalidate 刷新显示才有效
相关链接
C#实现串口通讯实时显示波形并实现平滑滤波和50Hz滤波
=================我是分割线=================
欢迎到公众号来唠嗑: