In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use C# to achieve serial oscilloscope", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "how to use C# serial oscilloscope" this article.
The details are as follows
Development tools
Visual studio2019
C # serial port oscilloscope, real-time refresh port number, dynamically draw multiple broken lines, obtain serial port data and output to text box
It was difficult for me to realize serial oscilloscope with Java before, and the effect was not satisfactory, so I developed it with C #.
C # can freely arrange the interface, drag the control directly to the form in the toolbar, and then double-click the control to add events, which is very convenient
The end result is achieved, but there is a problem of data loss.
There are three steps from data processing to drawing:
1. Get the data sent by serial port
2. Extract the data into the intermediate container collection list1 as needed
3. Extract the y value of the drawing from the collection
The result of the test is that there is no loss of data before it is put into the collection, and there is no problem in the process of taking the data out of the collection. So the problem lies in the second step, which has been solved (the rules for extracting data on demand should be set in as much detail as possible).
Code:
Namespace CommPortsDesigner {public partial class Form1: Form {public Form1 () {InitializeComponent () } private void Form1_Load (object sender, EventArgs e) / / the form is loaded after running, and the event {} / / will trigger the event {} / / to pass in the original data datas and the string r that needs to be matched Get the required data list list private List DealData (string datas, Regex r, string split) {if (string.IsNullOrEmpty (datas)) {throw new ArgumentException ($"" {nameof (datas)} "cannot be Null or empty." , nameof (datas);} List list1=new List {}; string S1 = ""; MatchCollection mc = r.Matches (datas); / / extract the string for (int I = 0; I) that meets the requirements
< mc.Count; i++) { textBox2.AppendText(mc[i].Value + "\r\n"); //测试数据是否正确输出 s1 = s1 + mc[i].Value; } string[] d = s1.Split( new string[]{split}, StringSplitOptions.RemoveEmptyEntries); for (int i=0;i 0) { chart1.ChartAreas[0].AxisX.LabelStyle.Format = "hh:mm:ss.ff"; chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Milliseconds; chart1.ChartAreas[0].AxisX.MajorGrid.IntervalType = DateTimeIntervalType.Milliseconds; try { inbuffer = new byte[serialPort1.BytesToRead]; serialPort1.Read(inbuffer, 0, serialPort1.BytesToRead); string strRaad = ASCIIEncoding.ASCII.GetString(inbuffer, 0, inbuffer.Length); textBox1.AppendText(strRaad); chart1.ChartAreas[0].AxisX.Minimum = DateTime.Now.AddSeconds(-5).ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = DateTime.Now.ToOADate(); chart1.ChartAreas[0].AxisX.Interval = 500; if (btn_start.Text.Equals("停止")) { //LP1 if (checkBox2.Checked) { foreach (int y in DealData(strRaad, r2, "LP2:")) { chart1.Series[1].Points.AddXY(DateTime.Now.ToOADate(), y); //count2 = count2 + 5; } } else { chart1.Series[1].Points.Clear(); } //LP2 if (checkBox2.Checked) { foreach (int y in DealData(strRaad, r2, "LP2:")) { chart1.Series[1].Points.AddXY(DateTime.Now.ToOADate(), y); //count2 = count2 + 5; } } else { chart1.Series[1].Points.Clear(); } //LP3 if (checkBox3.Checked) { foreach (int y in DealData(strRaad, r3, "LP3:")) { chart1.Series[2].Points.AddXY(DateTime.Now.ToOADate(), y); //count3 = count3 + 5; } } else { chart1.Series[2].Points.Clear(); } //LP4 if (checkBox4.Checked) { foreach (int y in DealData(strRaad, r4, "LP4:")) { chart1.Series[3].Points.AddXY(DateTime.Now.ToOADate(), y); //count4 = count4 + 5; } } else { chart1.Series[3].Points.Clear(); } //LP5 if (checkBox5.Checked) { foreach (int y in DealData(strRaad, r5, "LP5:")) { chart1.Series[4].Points.AddXY(DateTime.Now.ToOADate(), y); //count5 = count5 + 5; } } else { chart1.Series[4].Points.Clear(); } //CC1 if (checkBox6.Checked) { foreach (int y in DealData(strRaad, r6, "CC1:")) { chart1.Series[5].Points.AddXY(DateTime.Now.ToOADate(), y); //count6 = count6 + 5; } } else { chart1.Series[5].Points.Clear(); } //CC2 if (checkBox7.Checked) { foreach (int y in DealData(strRaad, r7, "CC2:")) { chart1.Series[6].Points.AddXY(DateTime.Now.ToOADate(), y); //count7 = count7 + 5; } } else { chart1.Series[6].Points.Clear(); } //CC3 if (checkBox8.Checked) { foreach (int y in DealData(strRaad, r8, "CC3:")) { chart1.Series[7].Points.AddXY(DateTime.Now.ToOADate(), y); //count8 = count8 + 5; } } else { chart1.Series[8].Points.Clear(); } //CC4 if (checkBox9.Checked) { foreach (int y in DealData(strRaad, r9, "CC4:")) { chart1.Series[8].Points.AddXY(DateTime.Now.ToOADate(), y); //count9 = count9 + 5;} } else { chart1.Series[8].Points.Clear(); } //CC5 if (checkBox10.Checked) { foreach (int y in DealData(strRaad, r10, "CC5:")) { chart1.Series[9].Points.AddXY(DateTime.Now.ToOADate(), y); //count10 = count10 + 5; } } else { chart1.Series[9].Points.Clear(); } } } catch { } } } private void btn_open_Click(object sender, EventArgs e) { if (serialPort1.IsOpen||btn_open.Text.Equals("关闭")) { try { serialPort1.Close(); btn_open.Text = "打开"; } catch { } } else { try { serialPort1.PortName = comboBox1.Text; serialPort1.Open(); btn_open.Text = "关闭"; } catch { MessageBox.Show("串口打开失败!", "错误"); } } } private void timer1_Tick(object sender, EventArgs e) { if (!serialPort1.IsOpen) { searchPort(); } else{} getDatas(); } private void btn_clear_Click(object sender, EventArgs e) { textBox1.Clear(); } private void btn_save_Click(object sender, EventArgs e) { if (this.textBox1.TextLength >0) {string path = "D:\\ log.txt"; using (StreamWriter sw = new StreamWriter (path, true)) {String time = DateTime.Now.ToLocalTime () .ToString (); sw.WriteLine ("\ n" + time); sw.Write (this.textBox1.Text) } MessageBox.Show ("saved to D:\\ log.txt!") }} private void btn_start_Click (object sender, EventArgs e) {if (serialPort1.IsOpen) {if (btn_start.Text.Equals (start)) {try {btn_start.Text = "stop" } catch {} else {try {btn_start.Text = "start" } catch {} else {if (btn_start.Text.Equals (stop)) {try {btn_start.Text = "start" } catch {} else {MessageBox.Show ("Serial port is not open!") ;}
Because of the first development, the functional codes to be implemented are all in the same class, so let's just take a look at it.
The final effect
The above is all the contents of this article "how to use C# to realize serial oscilloscope". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.